use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.
the class CacheExample method main.
public static void main(String[] args) throws Exception {
CuratorClient client = CuratorClient.getClientInstance("192.168.101.86:2181");
final Set<String> set = new HashSet<String>();
PathChildrenCache cache = new PathChildrenCache(client.getInnerClient(), PATH, true);
cache.start();
PathChildrenCacheListener listener1 = new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
switch(event.getType()) {
case CHILD_ADDED:
{
System.out.println("1Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
System.out.println("1Node added: " + event.getData().getPath());
break;
}
case CHILD_UPDATED:
{
System.out.println("1Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
System.out.println("1Node changed: " + new String(event.getData().getData()));
break;
}
case CHILD_REMOVED:
{
System.out.println("1Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}
default:
break;
}
}
};
// PathChildrenCacheListener listener2 = new PathChildrenCacheListener() {
//
// @Override
// public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
// switch (event.getType()) {
// case CHILD_ADDED: {
// System.out.println("2Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
// System.out.println("2Node added: " + event.getData().getPath());
// break;
// }
// case CHILD_UPDATED: {
// System.out.println("2Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
// break;
// }
// case CHILD_REMOVED: {
// System.out.println("2Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
// break;
// }
// default:
// break;
// }
// }
// };
ExecutorService services = Executors.newFixedThreadPool(5);
cache.getListenable().addListener(listener1, services);
// cache.getListenable().addListener(listener2,services);
// cache.getListenable().addListener(listener);
Thread.sleep(Long.MAX_VALUE);
cache.close();
client.close();
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.
the class CuratorPathCache method addListener.
public void addListener(String path, AbstractPathChildrenCacheListener listener) {
LOG.info("add listener for path:" + path);
PathChildrenCache cache = cacheMap.get(path);
if (cache == null) {
cache = new PathChildrenCache(client.getInnerClient(), path, true);
cacheMap.put(path, cache);
startCache(path);
}
cache.getListenable().addListener(listener);
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.
the class CuratorPathCache method removeListener.
public void removeListener(String path, AbstractPathChildrenCacheListener listener) {
LOG.info("remove listener for path:" + path);
PathChildrenCache cache = cacheMap.get(path);
if (cache != null) {
cache.getListenable().removeListener(listener);
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.
the class CuratorPathCache method cancelListener.
public void cancelListener(String path) throws IOException {
LOG.info("cancel listener for path:" + path);
PathChildrenCache cache = cacheMap.get(path);
if (cache != null) {
cache.close();
cacheMap.remove(path);
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.
the class FileNodeDistributor method close.
@Override
public void close() {
if (running.compareAndSet(true, false)) {
timeEventEmitter.removeListener(TIMEOUT_CHECK_DURATION, this);
serviceManager.removeServiceStateListener(DUPLICATE_SERVICE_GROUP, this);
executor.shutdownNow();
wildFileNodes.clear();
for (PathChildrenCache watcher : childWatchers.values()) {
CloseUtils.closeQuietly(watcher);
}
childWatchers.clear();
}
}
Aggregations