Curator 使用(二)
子節(jié)點(diǎn)查詢
List<String> childNodes = client.getChildren()
.forPath(nodePath);
System.out.println("開始打印子節(jié)點(diǎn):");
for (String s : childNodes) {
System.out.println(s);
}
判斷節(jié)點(diǎn)是否存在
// 判斷節(jié)點(diǎn)是否存在,如果不存在則為空
Stat statExist = client.checkExists().forPath(nodePath + "/abc");
System.out.println(statExist);
注冊(cè) 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("觸發(fā) watcher塑陵,節(jié)點(diǎn)路徑為:" + 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("觸發(fā) watcher,節(jié)點(diǎn)路徑為:" + event.getPath());
}
}
CuratorWatcher
和 Watcher
的區(qū)別主要在于實(shí)現(xiàn)process
方法時(shí)對(duì)于異常的處理顿天。其中 CuratorWatcher
可以拋出異常铆遭。
注冊(cè) Watcher 事件(注冊(cè)一次茫死,監(jiān)聽多次)
// 為節(jié)點(diǎn)添加 watcher
// NodeCache: 監(jiān)聽數(shù)據(jù)節(jié)點(diǎn)的變更奋岁,會(huì)觸發(fā)事件
final NodeCache nodeCache = new NodeCache(cto.client, nodePath);
// buildInitial : 初始化的時(shí)候獲取 node 的值并且緩存
nodeCache.start(true);
if (nodeCache.getCurrentData() != null) {
System.out.println("節(jié)點(diǎn)初始化數(shù)據(jù)為:" + new String(nodeCache.getCurrentData().getData()));
} else {
System.out.println("節(jié)點(diǎn)初始化數(shù)據(jù)為空...");
}
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("節(jié)點(diǎn)路徑:" + nodeCache.getCurrentData().getPath() + "數(shù)據(jù):" + data);
}
});
刪除監(jiān)聽事件可以使用
nodeCache.getListenable().removeListener();
為子節(jié)點(diǎn)注冊(cè) Watcher 事件
這種形式的好處在于可以監(jiān)聽節(jié)點(diǎn)的增刪改查操作的觸發(fā)嚼酝。
// 為子節(jié)點(diǎn)添加 watcher
// PathChildrenCache: 監(jiān)聽數(shù)據(jù)節(jié)點(diǎn)的增刪改妄均,會(huì)觸發(fā)事件
String childNodePathCache = nodePath;
// cacheData: 設(shè)置緩存節(jié)點(diǎn)的數(shù)據(jù)狀態(tài)
final PathChildrenCache childrenCache = new PathChildrenCache(cto.client, childNodePathCache, true);
/**
* StartMode: 初始化方式
* POST_INITIALIZED_EVENT:異步初始化,初始化之后會(huì)觸發(fā)事件
* NORMAL:異步初始化
* BUILD_INITIAL_CACHE:同步初始化
*/
childrenCache.start(StartMode.POST_INITIALIZED_EVENT);
List<ChildData> childDataList = childrenCache.getCurrentData();
System.out.println("當(dāng)前數(shù)據(jù)節(jié)點(diǎn)的子節(jié)點(diǎn)數(shù)據(jù)列表:");
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("子節(jié)點(diǎn)初始化 ok...");
}
else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)){
String path = event.getData().getPath();
if (path.equals(ADD_PATH)) {
System.out.println("添加子節(jié)點(diǎn):" + event.getData().getPath());
System.out.println("子節(jié)點(diǎn)數(shù)據(jù):" + 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("刪除子節(jié)點(diǎn):" + event.getData().getPath());
}else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
System.out.println("修改子節(jié)點(diǎn)路徑:" + event.getData().getPath());
System.out.println("修改子節(jié)點(diǎn)數(shù)據(jù):" + new String(event.getData().getData()));
}
}
});