版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26641781/article/details/83346978
Curator之nodeCache一次注册,N次监听
/*Curator之nodeCache一次注册,N次监听*/
//为节点添加watcher
//监听数据节点的变更,会触发事件
final NodeCache nodeCache = new NodeCache(cto.client,nodePath);
//buildInitial: 初始化的时候获取node的值并且缓存
nodeCache.start(true);
if(nodeCache.getCurrentData() != null){
System.out.println("节点的初始化数据为:"+new String(nodeCache.getCurrentData().getData()));
}else{
System.out.println("节点初始化数据为空。。。");
}
nodeCache.getListenable().addListener(new NodeCacheListener() {
public void nodeChanged() throws Exception {
//获取当前数据
String data = new String(nodeCache.getCurrentData().getData());
System.out.println("节点路径为:"+nodeCache.getCurrentData().getPath()+" 数据: "+data);
}
});