use of org.apache.curator.framework.recipes.cache.TreeCacheListener in project Saturn by vipshop.
the class ShardingTreeCacheService method addTreeCacheListenerIfAbsent.
public void addTreeCacheListenerIfAbsent(String path, int depth, TreeCacheListener treeCacheListener) throws ShardingException {
synchronized (isShutdownFlag) {
if (isShutdownFlag.get()) {
throw new ShardingException("ShardingTreeCacheService has been shutdown");
}
String fullPath = namespace + path;
TreeCacheListener treeCacheListenerOld = shardingTreeCache.addTreeCacheListenerIfAbsent(path, depth, treeCacheListener);
if (treeCacheListenerOld == null) {
LOGGER.info("add {}, full path is {}, depth is {}", treeCacheListener.getClass().getSimpleName(), fullPath, depth);
}
}
}
use of org.apache.curator.framework.recipes.cache.TreeCacheListener in project Saturn by vipshop.
the class ShardingTreeCache method addTreeCacheListenerIfAbsent.
public TreeCacheListener addTreeCacheListenerIfAbsent(String path, int depth, TreeCacheListener treeCacheListener) {
synchronized (this) {
TreeCacheListener treeCacheListenerOld = null;
String key = getKey(path, depth);
TreeCache treeCache = treeCacheMap.get(key);
if (treeCache == null) {
logger.error("The TreeCache is not exists, cannot add TreeCacheListener, path is {}, depth is {}", path, depth);
} else {
List<TreeCacheListener> treeCacheListeners = treeCacheListenerMap.get(treeCache);
boolean included = false;
for (TreeCacheListener tmp : treeCacheListeners) {
Class<? extends TreeCacheListener> tmpClass = tmp.getClass();
Class<? extends TreeCacheListener> treeCacheListenerClass = treeCacheListener.getClass();
if (tmpClass.equals(treeCacheListenerClass)) {
treeCacheListenerOld = tmp;
included = true;
break;
}
}
if (included) {
logger.info("The TreeCache has already included the instance of listener, will not be added, path is {}, depth is {}, listener is {}", path, depth, treeCacheListener.getClass());
} else {
treeCacheListeners.add(treeCacheListener);
treeCache.getListenable().addListener(treeCacheListener);
}
}
return treeCacheListenerOld;
}
}
use of org.apache.curator.framework.recipes.cache.TreeCacheListener in project metron by apache.
the class ZKCache method start.
/**
* Start the cache.
* @throws Exception If unable to be started.
*/
public void start() throws Exception {
if (cache == null) {
if (ownClient) {
client.start();
}
TreeCache.Builder builder = TreeCache.newBuilder(client, zkRoot);
builder.setCacheData(true);
cache = builder.build();
for (TreeCacheListener l : listeners) {
cache.getListenable().addListener(l);
}
cache.start();
}
}
use of org.apache.curator.framework.recipes.cache.TreeCacheListener in project coprhd-controller by CoprHD.
the class DistributedLockQueueManagerImpl method addLoggingCacheEventListener.
/**
* For logging purposes only, this will cause all nodes to log received TreeCache events.
*/
private void addLoggingCacheEventListener() {
TreeCacheListener listener = new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
switch(event.getType()) {
case NODE_ADDED:
log.debug("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
case NODE_UPDATED:
log.debug("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
case NODE_REMOVED:
log.debug("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}
}
};
treeCache.getListenable().addListener(listener);
}
use of org.apache.curator.framework.recipes.cache.TreeCacheListener in project flink by apache.
the class ZooKeeperUtilsTreeCacheTest method testCallbackNotCalledOnConnectionOrInitializationEvents.
@Test
public void testCallbackNotCalledOnConnectionOrInitializationEvents() throws Exception {
final TreeCacheListener treeCacheListener = ZooKeeperUtils.createTreeCacheListener(() -> {
throw new AssertionError("Should not be called.");
});
treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.INITIALIZED, null));
treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.CONNECTION_RECONNECTED, null));
treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.CONNECTION_LOST, null));
treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.CONNECTION_SUSPENDED, null));
}
Aggregations