Curator 使用(二)
子节点查询
List<String> childNodes = client.getChildren()
.forPath(nodePath);
System.out.println("开始打印子节点:");
for (String s : childNodes) {
System.out.println(s);
}
判断节点是否存在
// 判断节点是否存在,如果不存在则为空
Stat statExist = client.checkExists().forPath(nodePath + "/abc");
System.out.println(statExist);
注册 Watcher 事件
client.getData().usingWatcher(new MyCuratorWatcher()).forPath(nodePath);
client.getData().usingWatcher(new MyWatcher()).forPath(nodePath);
MyCuratorWatcher
import org.apache.curator.framework.api.CuratorWatcher;
import org.apache.zookeeper.WatchedEvent;
public class MyCuratorWatcher implements CuratorWatcher {
@Override
public void process(WatchedEvent event) throws Exception {
System.out.println("触发 watcher,节点路径为:" + event.getPath());
}
}
MyWatcher
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
public class MyWatcher implements Watcher {
@Override
public void process(WatchedEvent event) {
System.out.println("触发 watcher,节点路径为:" + event.getPath());
}
}
CuratorWatcher
和 Watcher
的区别主要在于实现process
方法时对于异常的处理。其中 CuratorWatcher
可以抛出异常。
注册 Watcher 事件(注册一次,监听多次)
// 为节点添加 watcher
// NodeCache: 监听数据节点的变更,会触发事件
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 {
if (nodeCache.getCurrentData() == null) {
System.out.println("空");
return;
}
String data = new String(nodeCache.getCurrentData().getData());
System.out.println("节点路径:" + nodeCache.getCurrentData().getPath() + "数据:" + data);
}
});
删除监听事件可以使用
nodeCache.getListenable().removeListener();
为子节点注册 Watcher 事件
这种形式的好处在于可以监听节点的增删改查操作的触发。
// 为子节点添加 watcher
// PathChildrenCache: 监听数据节点的增删改,会触发事件
String childNodePathCache = nodePath;
// cacheData: 设置缓存节点的数据状态
final PathChildrenCache childrenCache = new PathChildrenCache(cto.client, childNodePathCache, true);
/**
* StartMode: 初始化方式
* POST_INITIALIZED_EVENT:异步初始化,初始化之后会触发事件
* NORMAL:异步初始化
* BUILD_INITIAL_CACHE:同步初始化
*/
childrenCache.start(StartMode.POST_INITIALIZED_EVENT);
List<ChildData> childDataList = childrenCache.getCurrentData();
System.out.println("当前数据节点的子节点数据列表:");
for (ChildData cd : childDataList) {
String childData = new String(cd.getData());
System.out.println(childData);
}
childrenCache.getListenable().addListener(new PathChildrenCacheListener() {
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if(event.getType().equals(PathChildrenCacheEvent.Type.INITIALIZED)){
System.out.println("子节点初始化 ok...");
}
else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)){
String path = event.getData().getPath();
if (path.equals(ADD_PATH)) {
System.out.println("添加子节点:" + event.getData().getPath());
System.out.println("子节点数据:" + new String(event.getData().getData()));
} else if (path.equals("/super/imooc/e")) {
System.out.println("添加不正确...");
}
}else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED)){
System.out.println("删除子节点:" + event.getData().getPath());
}else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
System.out.println("修改子节点路径:" + event.getData().getPath());
System.out.println("修改子节点数据:" + new String(event.getData().getData()));
}
}
});