use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent 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.TreeCacheEvent 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.TreeCacheEvent in project elastic-job by dangdangdotcom.
the class CloudJobConfigurationListenerTest method assertChildEventWhenStateIsUpdateAndIsConfigPathAndTransientJob.
@Test
public void assertChildEventWhenStateIsUpdateAndIsConfigPathAndTransientJob() throws Exception {
cloudJobConfigurationListener.childEvent(null, new TreeCacheEvent(TreeCacheEvent.Type.NODE_UPDATED, new ChildData("/config/job/test_job", null, CloudJsonConstants.getJobJson().getBytes())));
verify(readyService, times(0)).remove(Collections.singletonList("test_job"));
verify(producerManager).reschedule(Matchers.<CloudJobConfiguration>any());
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent in project elastic-job by dangdangdotcom.
the class CloudJobConfigurationListenerTest method assertChildEventWhenIsNotConfigPath.
@Test
public void assertChildEventWhenIsNotConfigPath() throws Exception {
cloudJobConfigurationListener.childEvent(null, new TreeCacheEvent(TreeCacheEvent.Type.NODE_UPDATED, new ChildData("/other/test_job", null, "".getBytes())));
verify(producerManager, times(0)).schedule(Matchers.<CloudJobConfiguration>any());
verify(producerManager, times(0)).reschedule(Matchers.<CloudJobConfiguration>any());
verify(producerManager, times(0)).unschedule(Matchers.<String>any());
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent in project elastic-job by dangdangdotcom.
the class CloudJobConfigurationListenerTest method assertChildEventWhenStateIsAddAndIsConfigPathAndInvalidData.
@Test
public void assertChildEventWhenStateIsAddAndIsConfigPathAndInvalidData() throws Exception {
cloudJobConfigurationListener.childEvent(null, new TreeCacheEvent(TreeCacheEvent.Type.NODE_ADDED, new ChildData("/config/job/test_job", null, "".getBytes())));
verify(producerManager, times(0)).schedule(Matchers.<CloudJobConfiguration>any());
verify(producerManager, times(0)).reschedule(Matchers.<CloudJobConfiguration>any());
verify(producerManager, times(0)).unschedule(Matchers.<String>any());
}
Aggregations