ZooKeeper_9_Java操作ZK_检测节点
检测节点是否存在同步接口Stat exists(final String path, Watcher watcher)Stat exists(String path, boolean watch)异步接口void exists(final String path, Watcher watcher, StatCallback cb, Object ctx)vo
检测节点是否存在
同步接口
Stat exists(final String path, Watcher watcher)
Stat exists(String path, boolean watch)
异步接口
void exists(final String path, Watcher watcher, StatCallback cb, Object ctx)
void exists(String path, boolean watch, StatCallback cb, Object ctx)
参数说明:
path | 指定数据节点的节点路径 |
watcher | 注册的watcher,监听以下三类事件:
|
watch | 指定是否复用zk中默认的watcher |
cb | 注册一个异步回调函数 |
ctx | 用于传递上下文信息的对象 |
该接口主要用于检测指定节点是否存在,返回值是一个stat对象。
另外,如果在调用接口时注册Watcher的话,还可以对节点是否存在进行监听,一旦节点被创建、被删除或数据被更新,都会通知客户端
我们来看代码:
public class TestExist implements Watcher {
// 屏障,计数器
private static CountDownLatch downLatch = new CountDownLatch(1);
private static ZooKeeper zookeeper = null;
public static void main(String[] args) throws Exception {
zookeeper = new ZooKeeper("10.0.227.66:2181", 5000, new TestExist());
System.out.println("zookeeper.getState()1 : " + zookeeper.getState());
try {
downLatch.await();// 在计数器未归零之前,所有线程等待
} catch (Exception e) {
e.printStackTrace();
}
// 订阅"/cyx"
zookeeper.exists("/cyx", true);
// 创建"/cyx"
zookeeper.create("/cyx", "ccc".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// 更新参数
zookeeper.setData("/cyx", "123".getBytes(), -1);
// 创建子节点
zookeeper.create("/cyx/1", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// 删除子节点
zookeeper.delete("/cyx/1", -1);
// 删除根节点
zookeeper.delete("/cyx", -1);
Thread.sleep(Integer.MAX_VALUE);
}
@Override
public void process(WatchedEvent event) {
try {
System.out.println("receive watched event : " + event);
if (KeeperState.SyncConnected == event.getState()) {
if (EventType.None == event.getType() && null == event.getPath()) {
downLatch.countDown();// 计数器-1
} else if (EventType.NodeCreated == event.getType()) {
System.out.println("Node : " + event.getPath() + " created");
zookeeper.exists(event.getPath(), true);
} else if (EventType.NodeDeleted == event.getType()) {
System.out.println("Node : " + event.getPath() + " deleted");
zookeeper.exists(event.getPath(), true);
} else if (EventType.NodeDataChanged == event.getType()) {
System.out.println("Node : " + event.getPath() + " data changed");
zookeeper.exists(event.getPath(), true);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出结果:
zookeeper.getState()1 : CONNECTING
receive watched event : WatchedEvent state:SyncConnected type:None path:null
receive watched event : WatchedEvent state:SyncConnected type:NodeCreated path:/cyx
Node : /cyx created
receive watched event : WatchedEvent state:SyncConnected type:NodeDataChanged path:/cyx
Node : /cyx data changed
receive watched event : WatchedEvent state:SyncConnected type:NodeDeleted path:/cyx
Node : /cyx deleted
我们来捋下代码:
a. 通过exists()方法检测是否存在制定节点,同时注册一个watcher
b. 创建节点"/cyx",此时服务端马上发送事件通知:NodeCreate,回调process(),再次调用exists()方法,再次注册watcher
c.更新该节点的数据,这时候服务端又向客户端发送一个事件通知:NodeDataChanged,回调process(),再次调用exists()方法,再次注册watcher
d. 创建子节点"/cyx/1"
e. 删除子节点
f. 删除节点"/cyx",客户端收到事件通知NodeDeleted
小结:
a. 无论制定节点是否存在,通过调用exists()方法都可以注册watcher
b. exists接口中注册的watcher,能够对节点创建,节点删除和节点数据更新事件进行监听。
c. 对于指定节点的子节点对的各种变化,都不会通知客户端
更多推荐
所有评论(0)