use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project BRFS by zhangnianli.
the class CuratorTreeCache method cancelListener.
public void cancelListener(String path) {
LOG.info("remove listeners for tree:" + path);
TreeCache cache = cacheMap.get(path);
if (cache != null) {
cache.close();
cacheMap.remove(path);
}
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project elastic-job by dangdangdotcom.
the class ZookeeperRegistryCenter method addCacheData.
@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);
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache 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.TreeCache in project BRFS by zhangnianli.
the class CuratorTreeCache method removeListener.
public void removeListener(String path, AbstractTreeCacheListener listener) {
LOG.info("remove listener for tree:" + path);
TreeCache cache = cacheMap.get(path);
if (cache != null) {
cache.getListenable().removeListener(listener);
}
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache 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