本文整理汇总了Java中org.apache.curator.framework.recipes.cache.TreeCache类的典型用法代码示例。如果您正苦于以下问题:Java TreeCache类的具体用法?Java TreeCache怎么用?Java TreeCache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TreeCache类属于org.apache.curator.framework.recipes.cache包,在下文中一共展示了TreeCache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: watchTree
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
public boolean watchTree(String path, TreeCacheListener listener) {
TreeCache tc = nodeTreeCache.get(path);
if (tc != null) {
return false; // node been listened
}
try {
tc = TreeCache.newBuilder(client, path).build();
tc.start();
if (executor != null) {
tc.getListenable().addListener(listener, executor);
} else {
tc.getListenable().addListener(listener);
}
nodeTreeCache.put(path, tc);
return true;
} catch (Throwable e) {
throw checkException(String.format("Unable to watch tree for path: %s", path), e);
}
}
示例2: ZKStacksCache
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
public ZKStacksCache(CuratorFramework curator, IDataSourceConnector connector,
ServiceDiscoveryHostDeserializer hostsSerializer,
boolean cacheHosts, String basePath) {
log.info("starting stacks cache");
this.stackPathPrefix = getStackPathPrefix(basePath);
this.hostGetter = cacheHosts ? new CachedHostsGetter() : new ZookeeperHostGetter(connector);
this.hostsSerializer = hostsSerializer;
stacksCache = TreeCache.newBuilder(curator, basePath + ZKPathHelperConstants.STACKS_PATH)
.setCacheData(cacheHosts)
.setMaxDepth(cacheHosts ? STACK_ELEMENTS_COUNT + 1 : STACK_ELEMENTS_COUNT)
.build(); //(APPDS-1904-related) please be careful: listener somehow may be initialized after cache population, so the INITIALIZE event may be missed
stacksCache.getListenable().addListener(new StackCacheListener());//this is a curator problem, so now ZooKeeper
// is connected _after_ cache start, which prevents initialize to happen too early
try {
stacksCache.start();
} catch (Exception e) {
log.error("Failed to start stacks cache", e);
throw new RuntimeException(e);
}
}
示例3: Namespaces
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
/**
* Create a zookeeper-based namespace service
* @param client the curator client
* @param cache the treecache
* @param filePath the file
*/
public Namespaces(final CuratorFramework client, final TreeCache cache, final String filePath) {
requireNonNull(cache, "TreeCache may not be null!");
this.client = client;
this.cache = cache;
try {
this.client.create().orSetData().forPath(ZNODE_NAMESPACES);
this.cache.getListenable().addListener((c, e) -> {
final Map<String, ChildData> tree = cache.getCurrentChildren(ZNODE_NAMESPACES);
readTree(tree).forEach(data::put);
});
init(filePath).forEach(data::put);
} catch (final Exception ex) {
LOGGER.error("Could not create a zk node cache: {}", ex);
throw new RuntimeTrellisException(ex);
}
}
示例4: start
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
@Override
public void start() {
try {
// 1000ms - initial amount of time to wait between retries
// 3 times - max number of times to retry
client = CuratorFrameworkFactory.newClient(getIdentifier().getConnectionString(),
new ExponentialBackoffRetry(1000, 3));
client.start();
client.getZookeeperClient().blockUntilConnectedOrTimedOut();
cache = TreeCache.newBuilder(client, buildBasePath()).build();
serializer = new JsonInstanceSerializer<HostMetadata>(HostMetadata.class);
} catch (Exception e) {
logger.error("Service registry start error for server identifier '{}' !",
getIdentifier().getConnectionString(), e);
throw new ServiceDiscoveryException(e);
}
}
示例5: getData
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
public static Object getData(TreeCache treeCache, String pPath, String encode) throws IOException, KeeperException, InterruptedException {
log.debug("pPath=" + pPath);
ChildData cdata = treeCache.getCurrentData(pPath);
log.debug("cdata=" + cdata);
byte[] data = cdata.getData();
log.debug("data=" + data);
String datas = new String(data, encode);
Object ret = datas;
if (datas != null && !datas.trim().isEmpty()) {
datas = datas.trim();
if (datas.startsWith("{") && datas.endsWith("}")) {
Map<String, Object> map = JsonUtil.toJavaBean(datas, Map.class);
ret = map;
} else if (datas.startsWith("[") && datas.endsWith("]")) {
Collection<Object> ocoll = JsonUtil.toJavaBean(datas, Collection.class);
ret = ocoll;
}
}
log.debug("ret=" + ret);
return ret;
}
示例6: dumpDirectly
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
private void dumpDirectly(final String path, final List<String> result) {
for (String each : coordinatorRegistryCenter.getChildrenKeys(path)) {
String zkPath = path + "/" + each;
String zkValue = coordinatorRegistryCenter.get(zkPath);
if (null == zkValue) {
zkValue = "";
}
TreeCache treeCache = (TreeCache) coordinatorRegistryCenter.getRawCache("/" + jobName);
ChildData treeCacheData = treeCache.getCurrentData(zkPath);
String treeCachePath = null == treeCacheData ? "" : treeCacheData.getPath();
String treeCacheValue = null == treeCacheData ? "" : new String(treeCacheData.getData());
if (zkValue.equals(treeCacheValue) && zkPath.equals(treeCachePath)) {
result.add(Joiner.on(" | ").join(zkPath, zkValue));
} else {
result.add(Joiner.on(" | ").join(zkPath, zkValue, treeCachePath, treeCacheValue));
}
dumpDirectly(zkPath, result);
}
}
示例7: addChildNodeListener
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
@SuppressWarnings("resource")
@Override
public void addChildNodeListener(String path, ChildNodeListener listener) {
TreeCache cache = new TreeCache(client, path);
cache.getListenable().addListener((curatorFramework, event) -> {
if (null != event && null != event.getData() && null != event.getType()) {
String servicePath = event.getData().getPath();
if (servicePath.split("/").length > 3) {
if (null != event.getData().getData()) {
listener.childChanged(servicePath, event.getType().toString(),
new String(event.getData().getData()));
} else {
listener.childChanged(servicePath, event.getType().toString(), null);
}
}
}
});
try {
cache.start();
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: dumpDirectly
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
private void dumpDirectly(final String path, final List<String> result) {
for (String each : coordinatorRegistryCenter.getElasticConfigRegistryCenter().getChildrenKeys(path)) {
String zkPath = path + "/" + each;
String zkValue = coordinatorRegistryCenter.getElasticConfigRegistryCenter().get(zkPath);
if (null == zkValue) {
zkValue = "";
}
TreeCache treeCache = (TreeCache) coordinatorRegistryCenter.getElasticConfigRegistryCenter().getRawCache(
"/" + configProfile.getNode());
ChildData treeCacheData = treeCache.getCurrentData(zkPath);
String treeCachePath = null == treeCacheData ? "" : treeCacheData.getPath();
String treeCacheValue = null == treeCacheData ? "" : new String(treeCacheData.getData());
if (zkValue.equals(treeCacheValue) && zkPath.equals(treeCachePath)) {
result.add(Joiner.on(" | ").join(zkPath, zkValue));
}
else {
result.add(Joiner.on(" | ").join(zkPath, zkValue, treeCachePath, treeCacheValue));
}
dumpDirectly(zkPath, result);
}
}
示例9: AccumuloGraphMetadataStore
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
public AccumuloGraphMetadataStore(CuratorFramework curatorFramework, String zkPath) {
this.zkPath = zkPath;
this.curatorFramework = curatorFramework;
this.treeCache = new TreeCache(curatorFramework, zkPath);
this.treeCache.getListenable().addListener((client, event) -> {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("treeCache event, clearing cache");
}
synchronized (entries) {
entries.clear();
}
});
try {
this.treeCache.start();
} catch (Exception e) {
throw new VertexiumException("Could not start metadata sync", e);
}
}
示例10: start
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
public static void start() throws Exception {
if (!started) {
synchronized (RegistryManager.class) {
if (!started) {
String zookeeperAddress = ConfigManager.getInstance().getProperty(Constants.ZOOKEEPER_ADDRESS);
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
client = CuratorFrameworkFactory.newClient(zookeeperAddress, retryPolicy);
client.start();
TreeCache treeCache = TreeCache.newBuilder(client, Constants.SERVICE_ZK_PATH_PREFIX).setCacheData(false).build();
treeCache.getListenable().addListener(new ProviderNodeEventListener(), curatorEventThreadPool);
treeCache.start();
started = client.blockUntilConnected(1000, TimeUnit.MILLISECONDS);
}
}
}
}
示例11: ModeledCacheImpl
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
ModeledCacheImpl(CuratorFramework client, ModelSpec<T> modelSpec, ExecutorService executor)
{
if ( !modelSpec.path().isResolved() && !modelSpec.path().isRoot() && modelSpec.path().parent().isResolved() )
{
modelSpec = modelSpec.parent(); // i.e. the last item is a parameter
}
basePath = modelSpec.path();
this.serializer = modelSpec.serializer();
cache = TreeCache.newBuilder(client, basePath.fullPath())
.setCacheData(false)
.setDataIsCompressed(modelSpec.createOptions().contains(CreateOption.compress))
.setExecutor(executor)
.setCreateParentNodes(modelSpec.createOptions().contains(CreateOption.createParentsIfNeeded) || modelSpec.createOptions().contains(CreateOption.createParentsAsContainers))
.build();
}
示例12: checkTreeCache
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
private static void checkTreeCache(CuratorFramework curatorFramework, String path) throws Exception {
final Semaphore semaphore = new Semaphore(0);
TreeCache treeCache = TreeCache.newBuilder(curatorFramework, path)
.setCacheData(true)
.setMaxDepth(3)
.build();
if (treeCache == null) {
LOGGER.error("treeCache is null");
}
treeCache.getListenable().addListener((client, event) -> {
if (event.getType().equals(TreeCacheEvent.Type.INITIALIZED)) {
semaphore.release();
}
});
treeCache.start();
semaphore.tryAcquire(2, TimeUnit.SECONDS);
Map<String, ChildData> map = treeCache.getCurrentChildren("/propertyService");
if (map == null) {
LOGGER.error("map is null");
}
map.entrySet().forEach(entry -> {
LOGGER.info("{} - {}", entry.getKey(), Bytes.toString(entry.getValue().getData()));
});
}
示例13: close
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
@Override
public void close() {
for (Entry<String, TreeCache> each : caches.entrySet()) {
each.getValue().close();
}
waitForCacheClose();
CloseableUtils.closeQuietly(client);
}
示例14: get
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
@Override
public String get(final String key) {
TreeCache cache = findTreeCache(key);
if (null == cache) {
return getDirectly(key);
}
ChildData resultInCache = cache.getCurrentData(key);
if (null != resultInCache) {
return null == resultInCache.getData() ? null : new String(resultInCache.getData(), StandardCharsets.UTF_8);
}
return getDirectly(key);
}
示例15: findTreeCache
import org.apache.curator.framework.recipes.cache.TreeCache; //导入依赖的package包/类
private TreeCache findTreeCache(final String key) {
for (Entry<String, TreeCache> entry : caches.entrySet()) {
if (key.startsWith(entry.getKey())) {
return entry.getValue();
}
}
return null;
}