use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project sharding-jdbc by shardingjdbc.
the class ZookeeperRegistryCenter method watch.
@Override
public void watch(final String key, final EventListener eventListener) {
final String path = key + "/";
if (!caches.containsKey(path)) {
addCacheData(key);
}
TreeCache cache = caches.get(path);
cache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(final CuratorFramework client, final TreeCacheEvent event) throws Exception {
ChildData data = event.getData();
if (null == data || null == data.getPath()) {
return;
}
eventListener.onChange(new DataChangedEvent(getEventType(event), data.getPath(), null == data.getData() ? null : new String(data.getData(), "UTF-8")));
}
private DataChangedEvent.Type getEventType(final TreeCacheEvent event) {
switch(event.getType()) {
case NODE_UPDATED:
return DataChangedEvent.Type.UPDATED;
case NODE_REMOVED:
return DataChangedEvent.Type.DELETED;
default:
return DataChangedEvent.Type.IGNORED;
}
}
});
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project dubbo by alibaba.
the class ZKTools method testTreeCache.
public static void testTreeCache() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", 60 * 1000, 60 * 1000, new ExponentialBackoffRetry(1000, 3));
client.start();
CountDownLatch latch = new CountDownLatch(1);
TreeCache treeCache = TreeCache.newBuilder(client, "/dubbo/config").setCacheData(true).build();
treeCache.start();
treeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
TreeCacheEvent.Type type = event.getType();
ChildData data = event.getData();
if (type == TreeCacheEvent.Type.INITIALIZED) {
latch.countDown();
}
System.out.println(data.getPath() + "\n");
if (data.getPath().split("/").length == 5) {
byte[] value = data.getData();
String stringValue = new String(value, StandardCharsets.UTF_8);
// fire event to all listeners
Map<String, Object> added = null;
Map<String, Object> changed = null;
Map<String, Object> deleted = null;
switch(type) {
case NODE_ADDED:
added = new HashMap<>(1);
added.put(pathToKey(data.getPath()), stringValue);
added.forEach((k, v) -> System.out.println(k + " " + v));
break;
case NODE_REMOVED:
deleted = new HashMap<>(1);
deleted.put(pathToKey(data.getPath()), stringValue);
deleted.forEach((k, v) -> System.out.println(k + " " + v));
break;
case NODE_UPDATED:
changed = new HashMap<>(1);
changed.put(pathToKey(data.getPath()), stringValue);
changed.forEach((k, v) -> System.out.println(k + " " + v));
}
}
}
});
latch.await();
/* Map<String, ChildData> dataMap = treeCache.getCurrentChildren("/dubbo/config");
dataMap.forEach((k, v) -> {
System.out.println(k);
treeCache.getCurrentChildren("/dubbo/config/" + k).forEach((ck, cv) -> {
System.out.println(ck);
});
});*/
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project paascloud-master by paascloud.
the class MqBeanInitRunner method initProducerListener.
private void initProducerListener(CuratorFramework cf) throws Exception {
TreeCache treeCache = new TreeCache(cf, GlobalConstant.ZK_REGISTRY_PRODUCER_ROOT_PATH);
treeCache.getListenable().addListener(producerChangeListener);
treeCache.start();
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project paascloud-master by paascloud.
the class MqBeanInitRunner method initConsumerListener.
private void initConsumerListener(CuratorFramework cf) throws Exception {
TreeCache treeCache = new TreeCache(cf, GlobalConstant.ZK_REGISTRY_CONSUMER_ROOT_PATH);
treeCache.getListenable().addListener(consumerChangeListener);
treeCache.start();
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project paascloud-master by paascloud.
the class ZookeeperRegistryCenter method addCacheData.
/**
* Add cache data.
*
* @param cachePath the cache path
*/
@Override
public void addCacheData(final String cachePath) {
TreeCache cache = new TreeCache(client, cachePath);
try {
cache.start();
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
RegExceptionHandler.handleException(ex);
}
caches.put(cachePath + "/", cache);
}
Aggregations