use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project pravega by pravega.
the class ZKStreamMetadataStore method unregisterBucketListener.
@Override
public void unregisterBucketListener(int bucket) {
PathChildrenCache cache = bucketCacheMap.remove(bucket);
if (cache != null) {
try {
cache.clear();
cache.close();
} catch (IOException e) {
log.warn("unable to close watch on bucket {} with exception {}", bucket, e);
}
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project pravega by pravega.
the class ZKStreamMetadataStore method registerBucketChangeListener.
@Override
@SneakyThrows
public void registerBucketChangeListener(int bucket, BucketChangeListener listener) {
Preconditions.checkNotNull(listener);
PathChildrenCacheListener bucketListener = (client, event) -> {
StreamImpl stream;
switch(event.getType()) {
case CHILD_ADDED:
stream = getStreamFromPath(event.getData().getPath());
listener.notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamAdded));
break;
case CHILD_REMOVED:
stream = getStreamFromPath(event.getData().getPath());
listener.notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamRemoved));
break;
case CHILD_UPDATED:
stream = getStreamFromPath(event.getData().getPath());
listener.notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamUpdated));
break;
case CONNECTION_LOST:
listener.notify(new StreamNotification(null, null, NotificationType.ConnectivityError));
break;
default:
log.warn("Received unknown event {} on bucket", event.getType(), bucket);
}
};
String bucketRoot = String.format(ZKStoreHelper.BUCKET_PATH, bucket);
bucketCacheMap.put(bucket, new PathChildrenCache(storeHelper.getClient(), bucketRoot, true));
PathChildrenCache pathChildrenCache = bucketCacheMap.get(bucket);
pathChildrenCache.getListenable().addListener(bucketListener);
pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);
log.info("bucket {} change notification listener registered", bucket);
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project turbo-rpc by hank-whu.
the class ZooKeeperRegister method addRegisterWatcher.
/**
* 当断掉连接又重新连接上时起作用
*
* @param group
* @param app
* @param protocol
* @param serverAddress
* @param serverWeight
*/
private synchronized void addRegisterWatcher(String group, String app, Protocol protocol, HostPort serverAddress, int serverWeight) {
final String path = "/turbo/" + group + "/" + app + "/" + protocol + "/" + HexUtils.toHex(serverAddress.toString().getBytes(StandardCharsets.UTF_8));
if (watcherMap.containsKey(path)) {
return;
}
PathChildrenCache watcher = new PathChildrenCache(client, path, false);
PathChildrenCacheListener pathChildrenCacheListener = new PathChildrenCacheListener() {
private volatile boolean waitForInitializedEvent = true;
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
switch(event.getType()) {
case INITIALIZED:
waitForInitializedEvent = false;
break;
case CONNECTION_RECONNECTED:
if (waitForInitializedEvent) {
return;
}
if (logger.isInfoEnabled()) {
logger.info("获得zk连接尝试重新注册, " + path + ", " + serverAddress + "@" + serverWeight);
}
ZooKeeperRegister.this.register(group, app, protocol, serverAddress, serverWeight);
break;
default:
break;
}
}
};
watcher.getListenable().addListener(pathChildrenCacheListener);
try {
watcher.start(StartMode.POST_INITIALIZED_EVENT);
watcherMap.put(path, watcher);
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error("zk监听失败, " + path, e);
}
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project Mycat_plus by coderczp.
the class ZktoXmlMain method runCommandWatch.
/**
* 进行命令的监听操作
* 方法描述
* @param zkConn zk的连接信息
* @param path 路径信息
* @param ZKLISTENER 监控路径信息
* @throws Exception
* @创建日期 2016年9月20日
*/
@SuppressWarnings("resource")
private static void runCommandWatch(final CuratorFramework zkConn, final String path) throws Exception {
PathChildrenCache children = new PathChildrenCache(zkConn, path, true);
CommandPathListener commandListener = new CommandPathListener();
// 移除原来的监听再进行添加
children.getListenable().addListener(commandListener);
children.start();
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project Mycat_plus by coderczp.
the class ZKUtils method addChildPathCache.
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);
watchMap.put(path, childrenCache);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations