本文整理汇总了Java中org.apache.curator.framework.recipes.cache.NodeCache类的典型用法代码示例。如果您正苦于以下问题:Java NodeCache类的具体用法?Java NodeCache怎么用?Java NodeCache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NodeCache类属于org.apache.curator.framework.recipes.cache包,在下文中一共展示了NodeCache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startup
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
@Override
public void startup()
{
if( isCluster() )
{
CuratorFramework curator = getCurator();
debugCache = new NodeCache(curator, CLUSTER_DEBUG_FULL_PATH);
try
{
debugCache.start();
createNode(SERVER_REL_PATH, "");
membersCache = createPathCache(SERVER_REL_PATH, false);
ensureDebug = curator.newNamespaceAwareEnsurePath(CLUSTER_DEBUG_FULL_PATH);
}
catch( Exception e )
{
Throwables.propagate(e);
}
}
hasStarted = true;
}
示例2: runWatch
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
/**
* @param cache NodeCache
* @param zkListen
* @throws Exception
* @Created 2016/9/20
*/
private static void runWatch(final NodeCache cache, final ZookeeperProcessListen zkListen)
throws Exception {
cache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() {
LOGGER.info("ZktoxmlMain runWatch process path event start ");
String notPath = cache.getCurrentData().getPath();
LOGGER.info("NodeCache changed, path is: " + notPath);
// notify
zkListen.notify(notPath);
LOGGER.info("ZktoxmlMain runWatch process path event over");
}
});
}
示例3: listenNodes
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
private List<NodeCache> listenNodes(String profileCode, String[] appCodes) {
ZkTemplate.NodeListener listener = new ZkTemplate.NodeListener() {
@Override
public void nodeChanged() throws Exception {
try {
refresher.refresh();
} catch (Throwable e) {
logger.error("触发刷新出错:", e);
}
}
};
List<NodeCache> nodeCaches = new ArrayList<>();
for (String appCode : appCodes) {
NodeCache nodeCache = zkTemplate.listenNode(ZkTemplate.buildPath(profileCode, appCode), false, listener);
nodeCaches.add(nodeCache);
}
return nodeCaches;
}
示例4: test_zkNode
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
@Test
public void test_zkNode() throws Exception {
CuratorFramework zk = curator.usingNamespace("namespace-test");
String groupPath = "/group-1/localhost:9001";
String s = zk.create().creatingParentsIfNeeded().forPath(groupPath);
Assert.assertEquals(s, "/group-1/localhost:9001");
NodeCache nodeCache = new NodeCache(zk, "/group-1", true);
nodeCache.start();
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
logger.info("node cache change");
}
});
zk.setData().forPath("/group-1", "test-0".getBytes());
zk.setData().forPath("/group-1", "test-2".getBytes());
}
示例5: initProvider
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
private void initProvider(CuratorFramework client, String serviceName) throws Exception {
this.client = client;
this.serviceName = serviceName;
tmpFile = confFile(true);
confFile = confFile(false);
String configPath = ZookeeperUtils.configPath(serviceName);
byte[] bytes = client.getData().forPath(configPath);
saveFile(bytes);
cache = new NodeCache(client, configPath);
cache.getListenable().addListener(() -> {
try {
if (cache.getCurrentData() != null) {
byte[] data = cache.getCurrentData().getData();
if (data.length > 0) {
saveFile(data);
}
}
} catch (Exception e) {
logger.warn("Error while processing config file change event. message={}", e.getMessage());
}
});
cache.start();
}
示例6: addNodeCacheListener
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
public void addNodeCacheListener(String path) throws Exception {
Stat existStat = curatorFramework
.checkExists()
.forPath(path);
if (existStat == null)
curatorFramework
.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath(path);
NodeCache nodeCache = new NodeCache(curatorFramework, path, false);
nodeCache.start();
nodeCache.getListenable().addListener(() -> {
ChildData currentData = nodeCache.getCurrentData();
LOG.info("New Cache Data: {}", currentData == null ? "null" : new String(currentData.getData()));
}
);
}
示例7: main
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
CuratorFramework framework = CuratorFrameworkFactory.builder()
.connectString("localhost:2181").retryPolicy(new RetryNTimes(3, 2000)).build();
try {
framework.start();
final NodeCache nodeCache = new NodeCache(framework, "/test");
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
ChildData data = nodeCache.getCurrentData();
System.out.println(new String(data.getData()) + " / " + data);
}
});
nodeCache.start();
Thread.sleep(30000);
nodeCache.close();
} finally {
framework.close();
}
}
示例8: start
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
/**
* Start.
*
* @throws Exception the exception
*/
public void start() throws Exception { //NOSONAR
LOG.info("Starting node tracker");
zkClient.getUnhandledErrorListenable().addListener(errorsListener);
if (createZkNode()) {
controlCache = new NodeCache(zkClient, CONTROL_SERVER_NODE_PATH);
controlCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
ChildData currentData = controlCache.getCurrentData();
if (currentData == null) {
LOG.warn("Control service node died!");
onNoMaster();
} else {
LOG.warn("Control service node changed!");
onMasterChange(currentData);
}
}
});
controlCache.start();
} else {
LOG.warn("Failed to create ZK node!");
}
}
示例9: registerListener
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
private void registerListener(final CuratorFramework curator, final String path, final ZookeeperConfigurationListener listener) throws Exception
{
final NodeCache cache = new NodeCache(curator, path, true);
cache.start();
cache.getListenable().addListener(new NodeCacheListener()
{
@Override
public void nodeChanged() throws Exception
{
listener.onChange(curator, cache, path);
}
});
childrens.add(cache);
logger.info(String.format("Add listener %s for %s", listener, path));
}
示例10: ZkNodeCacheWrapper
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
public ZkNodeCacheWrapper(IDataSourceConnector connector, String path, NodeCache cache, boolean dataIsCompressed, boolean useCache, boolean useCacheWhenNotConnectedToDataSource) {
super(connector);
this.useCache = useCache;
this.dataIsCompressed = dataIsCompressed;
this.useCacheWhenNotConnectedToDataSource = useCacheWhenNotConnectedToDataSource;
this.path = path;
this.cache = cache;
}
示例11: loadZkWatch
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
private static void loadZkWatch(Set<String> setPaths, final CuratorFramework zkConn,
final ZookeeperProcessListen zkListen) throws Exception {
if (null != setPaths && !setPaths.isEmpty()) {
for (String path : setPaths) {
final NodeCache node = new NodeCache(zkConn, path);
node.start(true);
runWatch(node, zkListen);
LOGGER.info("ZktoxmlMain loadZkWatch path:" + path + " regist success");
}
}
}
示例12: close
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
/**
* 关闭(释放相关资源)
*/
public void close() {
for (NodeCache nodeCache : nodeCaches) {
try {
nodeCache.close();
} catch (IOException e) {
logger.error("关闭节点监听器出错:", e);
}
}
zkTemplate.close();
}
示例13: buildNodeCache
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
private Observable<NodeCache> buildNodeCache(final ConfigDescriptor desc)
{
return Observable.fromCallable(() -> {
final String configPath = makePath(ROOT_ZK_PATH, desc.getConfigName());
final NodeCache nc = new NodeCache(curator, configPath);
nc.getListenable().addListener(() -> onNodeChanged(nc, desc));
try {
nc.start(true);
// Note that we have to force calling onNodeChanged() here since `nc.start(true)` will not emit an initial event.
onNodeChanged(nc, desc);
// Create the ephemeral node last, just in case something goes wrong with setting up the node cache
// NOTE: This process is what actually creates the configuration node if it was missing.
PersistentEphemeralNode en = new PersistentEphemeralNode(curator, EPHEMERAL, makePath(configPath, localNodeName), new byte[0]);
en.start();
if (!en.waitForInitialCreate(getDefaultNodeCreationTimeout().toMillis(), TimeUnit.MILLISECONDS)) {
throw new TimeoutException("Timeout on creation of ephemeral node for " + makePath(configPath, localNodeName));
}
ephemeralNodes.put(desc, en);
return nc;
}
catch (Exception ex) {
log.warn("Failed to initialize for configPath {}", configPath, ex);
throw ex;
}
});
}
示例14: onNodeChanged
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
public void onNodeChanged(final NodeCache cache, final ConfigDescriptor desc)
{
ChildData childData = cache.getCurrentData();
try {
Optional<String> valueOpt = Optional.empty();
if (childData != null && childData.getData() != null && childData.getData().length > 0) {
valueOpt = Optional.of(new String(childData.getData(), Charsets.UTF_8));
}
emitEvent(desc.getConfigName(), valueOpt);
}
catch (Exception ex) {
log.warn("Failed to handle onNodeChanged w/ new data for config key {}, data {}", desc.getConfigName(), childData, ex);
}
}
示例15: serviceStart
import org.apache.curator.framework.recipes.cache.NodeCache; //导入依赖的package包/类
@Override
protected void serviceStart() throws Exception {
framework.start();
nodeCache = new NodeCache(framework, zkMountTablePath, false);
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
handleMountTableChange(nodeCache.getCurrentData().getData());
}
});
nodeCache.start(false);
}