本文整理匯總了Java中org.apache.curator.framework.recipes.cache.PathChildrenCacheListener類的典型用法代碼示例。如果您正苦於以下問題:Java PathChildrenCacheListener類的具體用法?Java PathChildrenCacheListener怎麽用?Java PathChildrenCacheListener使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PathChildrenCacheListener類屬於org.apache.curator.framework.recipes.cache包,在下文中一共展示了PathChildrenCacheListener類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: listen
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
@PostConstruct
public void listen() throws Exception {
MasterSlaveApiFactory masterSlaveApiFactory = new MasterSlaveApiFactoryImpl(client);
PathChildrenCache pathChildrenCache = new PathChildrenCache(client, masterSlaveApiFactory.pathApi().getJobPath(), true);
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public synchronized void childEvent(CuratorFramework clientInner, PathChildrenCacheEvent event) throws Exception {
if (!EventHelper.isChildUpdateEvent(event) && !EventHelper.isChildAddEvent(event)) {
return;
}
MasterSlaveJobData masterSlaveJobData = new MasterSlaveJobData(event.getData());
if (!masterSlaveJobData.getData().isOperated()) {
return;
}
LoggerHelper.info("begin update master-slave job summary " + masterSlaveJobData.getData());
masterSlaveJobSummaryService.updateJobSummary(masterSlaveJobData.getData());
masterSlaveJobLogService.updateJobLog(masterSlaveJobData.getData());
LoggerHelper.info("update master-slave job summary successfully " + masterSlaveJobData.getData());
}
});
pathChildrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
}
示例2: addChildPathCache
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
public static void addChildPathCache( String path ,PathChildrenCacheListener listener )
{
NameableExecutor businessExecutor = MycatServer.getInstance().getBusinessExecutor();
ExecutorService executor = businessExecutor ==null?Executors.newFixedThreadPool(5):
businessExecutor;
try {
/**
* 監聽子節點的變化情況
*/
final PathChildrenCache childrenCache = new PathChildrenCache(getConnection(), path, true);
childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
childrenCache.getListenable().addListener(listener,executor);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例3: createPathCache
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
@Override
public PathChildrenCache createPathCache(String type, boolean cacheData, PathChildrenCacheListener listener,
StartMode startMode)
{
try
{
PathChildrenCache cache = new PathChildrenCache(client, getParentPath(type), cacheData);
if( listener != null )
{
cache.getListenable().addListener(listener);
}
cache.start(startMode);
return cache;
}
catch( Exception e )
{
throw Throwables.propagate(e);
}
}
示例4: listen
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
@PostConstruct
public void listen() throws Exception {
StandbyApiFactory standbyApiFactory = new StandbyApiFactoryImpl(client);
PathChildrenCache pathChildrenCache = new PathChildrenCache(client, standbyApiFactory.pathApi().getJobPath(), true);
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public synchronized void childEvent(CuratorFramework clientInner, PathChildrenCacheEvent event) throws Exception {
if (!EventHelper.isChildUpdateEvent(event) && !EventHelper.isChildAddEvent(event)) {
return;
}
StandbyJobData standbyJobData = new StandbyJobData(event.getData());
if (!standbyJobData.getData().isOperated()) {
return;
}
LoggerHelper.info("begin update standby job summary " + standbyJobData.getData());
standbyJobSummaryService.updateJobSummary(standbyJobData.getData());
standbyJobLogService.updateJobLog(standbyJobData.getData());
LoggerHelper.info("update standby job summary successfully " + standbyJobData.getData());
}
});
pathChildrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
}
示例5: pathChildrenCacheListener
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
private PathChildrenCacheListener pathChildrenCacheListener(final ClusterListener listener) {
return (client, event) -> {
log.debug("Event {} generated on cluster", event);
switch (event.getType()) {
case CHILD_ADDED:
log.info("Node {} added to cluster", getServerName(event));
listener.onEvent(HOST_ADDED, (Host) SerializationUtils.deserialize(event.getData().getData()));
break;
case CHILD_REMOVED:
log.info("Node {} removed from cluster", getServerName(event));
listener.onEvent(HOST_REMOVED, (Host) SerializationUtils.deserialize(event.getData().getData()));
break;
case CHILD_UPDATED:
log.warn("Invalid usage: Node {} updated externally for cluster", getServerName(event));
break;
case CONNECTION_LOST:
log.error("Connection lost with Zookeeper");
listener.onEvent(ERROR, null);
break;
//$CASES-OMITTED$
default:
log.warn("Received unknown event {}", event.getType());
}
};
}
示例6: getDefaultListener
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
private static PathChildrenCacheListener getDefaultListener() {
return
(client, event) -> {
switch (event.getType()) {
case CHILD_ADDED : {
LOGGER.info("Node added: [{}]", ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}
case CHILD_REMOVED : {
LOGGER.info("Node removed: [{}]", ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}
default :
break;
}
};
}
示例7: newListener
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
private PathChildrenCacheListener newListener(final EventType eventType, final GroupChangedListener delegate) {
return
(client, event) -> {
switch (event.getType()) {
case CHILD_ADDED : {
String addedMemberId = ZKPaths.getNodeFromPath(event.getData().getPath());
delegate.memberAdded(eventType, addedMemberId);
break;
}
case CHILD_REMOVED : {
final String removedMemberId = ZKPaths.getNodeFromPath(event.getData().getPath());
delegate.memberRemoved(eventType, removedMemberId);
break;
}
default :
break;
}
};
}
示例8: testStoreRegistersDispatcherAndStartsItsClient
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
/**
* This test ensures store subscribes to receive events from underlying client. Dispatcher tests ensures listeners
* are fired on incoming events. These two sets of tests ensure observer pattern in {@code TransientStore} works fine.
*/
@Test
public void testStoreRegistersDispatcherAndStartsItsClient() throws Exception {
final StoreWithMockClient<String> store = new StoreWithMockClient<>(config, curator);
final PathChildrenCache cache = Mockito.mock(PathChildrenCache.class);
final ZookeeperClient client = store.getClient();
Mockito
.when(client.getCache())
.thenReturn(cache);
final ListenerContainer<PathChildrenCacheListener> container = Mockito.mock(ListenerContainer.class);
Mockito
.when(cache.getListenable())
.thenReturn(container);
store.start();
Mockito
.verify(container)
.addListener(store.dispatcher);
Mockito
.verify(client)
.start();
}
示例9: createPathChildrenCacheMock
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
/**
* Create mock {@link PathChildrenCache} using given controller ID and DPIDs.
*
* @param controllerId Controller ID to represent current data.
* @param paths List of HexString indicating switch's DPID.
* @param listener Callback object to be set as Listenable.
* @return Mock PathChildrenCache object
* @throws Exception
*/
private PathChildrenCache createPathChildrenCacheMock(
final String controllerId,
final String[] paths,
ListenerContainer<PathChildrenCacheListener> listener)
throws Exception {
PathChildrenCache pathChildrenCache = createMock(PathChildrenCache.class);
expect(pathChildrenCache.getListenable()).andReturn(listener).anyTimes();
pathChildrenCache.start(anyObject(StartMode.class));
expectLastCall().anyTimes();
List<ChildData> childs = new ArrayList<ChildData>();
for (String path : paths) {
childs.add(createChildDataMockForCurrentData(controllerId, path));
}
expect(pathChildrenCache.getCurrentData()).andReturn(childs).anyTimes();
pathChildrenCache.rebuild();
expectLastCall().anyTimes();
replay(pathChildrenCache);
return pathChildrenCache;
}
示例10: addDataListener
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
@Override
public void addDataListener(String path, DataListener listener) {
try {
// 第一步:獲取-校驗-創建監聽器
PathChildrenCacheListener pathChildrenCacheListener = dataListenerMap.get(listener);
if(pathChildrenCacheListener != null){//已監聽
return;
} else {
// 添加外部監聽器
Set<DataListener> dataListenerSet = dataListenersMap.get(path);
if(dataListenerSet == null){
dataListenersMap.put(path, dataListenerSet = new ConcurrentHashSet<DataListener>());
}
dataListenerSet.add(listener);
dataListenerMap.put(listener, pathChildrenCacheListener = new PathChildrenCacheListenerImpl(path));
}
// 第二步:獲取-校驗-創建子節點緩存連接
PathChildrenCache pathChildrenCache = pathChildrenCacheMap.get(path);
if(pathChildrenCache == null){
pathChildrenCacheMap.put(path, pathChildrenCache = new PathChildrenCache(client, path, true));
// 第三步:啟動監聽
pathChildrenCache.start(StartMode.POST_INITIALIZED_EVENT);
}
// 第四步:添加監聽器
pathChildrenCache.getListenable().addListener(pathChildrenCacheListener);
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
示例11: removeDataListener
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
@Override
public void removeDataListener(String path, DataListener listener) {
try {
// 第一步:移除dataListenerMap中的數據
PathChildrenCacheListener pathChildrenCacheListener = dataListenerMap.get(listener);
if(pathChildrenCacheListener == null){
return;
} else {
dataListenerMap.remove(listener);
// 第二步:移除Set<DataListener>中的數據
Set<DataListener> dataListenerSet = dataListenersMap.get(path);
if(dataListenerSet != null && dataListenerSet.contains(listener)){
dataListenerSet.remove(listener);
}
// 第三步:移除dataListenersMap和childDataMap中的數據
if(dataListenerSet == null || dataListenerSet.isEmpty()){
dataListenersMap.remove(path);
childDataMap.remove(path);
}
}
// 第四步:取消監聽,並移除pathChildrenCacheMap中的數據
PathChildrenCache pathChildrenCache = pathChildrenCacheMap.get(path);
if(pathChildrenCache != null){
pathChildrenCache.getListenable().removeListener(pathChildrenCacheListener);
((PathChildrenCacheListenerImpl)listener).unwatch();
if(pathChildrenCache.getListenable().size() == 0){
pathChildrenCacheMap.remove(path);
pathChildrenCache.close();
}
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
示例12: addChildPathCache
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
public static void addChildPathCache(String path, PathChildrenCacheListener listener) {
try {
//watch the child status
final PathChildrenCache childrenCache = new PathChildrenCache(getConnection(), path, true);
childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
childrenCache.getListenable().addListener(listener);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例13: addViewPathCache
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
public static void addViewPathCache(String path, PathChildrenCacheListener listener) {
try {
//watch the child status
final PathChildrenCache childrenCache = new PathChildrenCache(getConnection(), path, true);
childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
childrenCache.getListenable().addListener(listener);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例14: addListener
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
private static void addListener(PathChildrenCache cache)
{
// a PathChildrenCacheListener is optional. Here, it's used just to log changes
PathChildrenCacheListener listener = new PathChildrenCacheListener()
{
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception
{
switch ( event.getType() )
{
case CHILD_ADDED:
{
System.out.println("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}
case CHILD_UPDATED:
{
System.out.println("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}
case CHILD_REMOVED:
{
System.out.println("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}
}
}
};
cache.getListenable().addListener(listener);
}
示例15: registerBucketOwnershipListener
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; //導入依賴的package包/類
@Override
@SneakyThrows
public void registerBucketOwnershipListener(BucketOwnershipListener listener) {
Preconditions.checkNotNull(listener);
PathChildrenCacheListener bucketListener = (client, event) -> {
switch (event.getType()) {
case CHILD_ADDED:
// no action required
break;
case CHILD_REMOVED:
int bucketId = Integer.parseInt(ZKPaths.getNodeFromPath(event.getData().getPath()));
listener.notify(new BucketNotification(bucketId, BucketNotification.NotificationType.BucketAvailable));
break;
case CONNECTION_LOST:
listener.notify(new BucketNotification(Integer.MIN_VALUE, BucketNotification.NotificationType.ConnectivityError));
break;
default:
log.warn("Received unknown event {}", event.getType());
}
};
bucketOwnershipCacheRef.compareAndSet(null, new PathChildrenCache(storeHelper.getClient(), ZKStoreHelper.BUCKET_OWNERSHIP_PATH, true));
bucketOwnershipCacheRef.get().getListenable().addListener(bucketListener);
bucketOwnershipCacheRef.get().start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
log.info("bucket ownership listener registered");
}