use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener 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.TreeCacheListener 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.TreeCacheListener in project elastic-job by dangdangdotcom.
the class JobNodeStorageTest method assertAddDataListener.
@Test
public void assertAddDataListener() {
TreeCache treeCache = mock(TreeCache.class);
@SuppressWarnings("unchecked") Listenable<TreeCacheListener> listeners = mock(Listenable.class);
TreeCacheListener listener = mock(TreeCacheListener.class);
when(treeCache.getListenable()).thenReturn(listeners);
when(regCenter.getRawCache("/test_job")).thenReturn(treeCache);
jobNodeStorage.addDataListener(listener);
verify(listeners).addListener(listener);
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener in project Saturn by vipshop.
the class SaturnExecutorService method registerCallbackAndStartExistingJob.
/**
* Register NewJobCallback, and async start existing jobs. The TreeCache will publish the create events for already
* existing jobs.
*/
public void registerCallbackAndStartExistingJob(final ScheduleNewJobCallback callback) throws Exception {
jobsTreeCache = TreeCache.newBuilder((CuratorFramework) coordinatorRegistryCenter.getRawClient(), JobNodePath.ROOT).setExecutor(new CloseableExecutorService(Executors.newSingleThreadExecutor(new SaturnThreadFactory(executorName + "-$Jobs-watcher", false)), true)).setMaxDepth(1).build();
jobsTreeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
if (event == null) {
return;
}
ChildData data = event.getData();
if (data == null) {
return;
}
String path = data.getPath();
if (path == null || path.equals(JobNodePath.ROOT)) {
return;
}
Type type = event.getType();
if (type == null || !type.equals(Type.NODE_ADDED)) {
return;
}
String jobName = StringUtils.substringAfterLast(path, "/");
String jobClassPath = JobNodePath.getNodeFullPath(jobName, ConfigurationNode.JOB_CLASS);
// wait 5 seconds at most until jobClass created.
for (int i = 0; i < WAIT_JOBCLASS_ADDED_COUNT; i++) {
if (client.checkExists().forPath(jobClassPath) == null) {
Thread.sleep(200);
continue;
}
log.info("new job: {} 's jobClass created event received", jobName);
if (!jobNames.contains(jobName)) {
if (callback.call(jobName)) {
jobNames.add(jobName);
log.info("the job {} initialize successfully", jobName);
} else {
log.warn("the job {} initialize fail", jobName);
}
} else {
log.warn("the job {} is unnecessary to initialize, because it's already existing", jobName);
}
break;
}
}
});
jobsTreeCache.start();
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener in project metron by apache.
the class ConfigurationFunctions method setupTreeCache.
private static synchronized void setupTreeCache(Context context) throws Exception {
try {
Optional<Object> treeCacheOpt = context.getCapability("treeCache");
if (treeCacheOpt.isPresent()) {
return;
}
} catch (IllegalStateException ex) {
}
Optional<Object> clientOpt = context.getCapability(Context.Capabilities.ZOOKEEPER_CLIENT);
if (!clientOpt.isPresent()) {
throw new IllegalStateException("I expected a zookeeper client to exist and it did not. Please connect to zookeeper.");
}
CuratorFramework client = (CuratorFramework) clientOpt.get();
TreeCache cache = new TreeCache(client, Constants.ZOOKEEPER_TOPOLOGY_ROOT);
TreeCacheListener listener = new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
if (event.getType().equals(TreeCacheEvent.Type.NODE_ADDED) || event.getType().equals(TreeCacheEvent.Type.NODE_UPDATED)) {
String path = event.getData().getPath();
byte[] data = event.getData().getData();
String sensor = Iterables.getLast(Splitter.on("/").split(path), null);
if (path.startsWith(ConfigurationType.PARSER.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.PARSER);
sensorMap.put(sensor, new String(data));
} else if (ConfigurationType.GLOBAL.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.GLOBAL, new String(data));
} else if (ConfigurationType.PROFILER.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.PROFILER, new String(data));
} else if (path.startsWith(ConfigurationType.ENRICHMENT.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.ENRICHMENT);
sensorMap.put(sensor, new String(data));
} else if (path.startsWith(ConfigurationType.INDEXING.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.INDEXING);
sensorMap.put(sensor, new String(data));
}
} else if (event.getType().equals(TreeCacheEvent.Type.NODE_REMOVED)) {
String path = event.getData().getPath();
String sensor = Iterables.getLast(Splitter.on("/").split(path), null);
if (path.startsWith(ConfigurationType.PARSER.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.PARSER);
sensorMap.remove(sensor);
} else if (path.startsWith(ConfigurationType.ENRICHMENT.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.ENRICHMENT);
sensorMap.remove(sensor);
} else if (path.startsWith(ConfigurationType.INDEXING.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.INDEXING);
sensorMap.remove(sensor);
} else if (ConfigurationType.PROFILER.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.PROFILER, null);
} else if (ConfigurationType.GLOBAL.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.GLOBAL, null);
}
}
}
};
cache.getListenable().addListener(listener);
cache.start();
for (ConfigurationType ct : ConfigurationType.values()) {
switch(ct) {
case GLOBAL:
case PROFILER:
{
String data = "";
try {
byte[] bytes = ConfigurationsUtils.readFromZookeeper(ct.getZookeeperRoot(), client);
data = new String(bytes);
} catch (Exception ex) {
}
configMap.put(ct, data);
}
break;
case INDEXING:
case ENRICHMENT:
case PARSER:
{
List<String> sensorTypes = client.getChildren().forPath(ct.getZookeeperRoot());
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ct);
for (String sensorType : sensorTypes) {
sensorMap.put(sensorType, new String(ConfigurationsUtils.readFromZookeeper(ct.getZookeeperRoot() + "/" + sensorType, client)));
}
}
break;
}
}
context.addCapability("treeCache", () -> cache);
}
Aggregations