我一直在尝试使用apache curator作为zookeeper,但没有取得进展。我所寻找的就是在zk节点上设置一个监视器,并监听该特定节点上的所有数据更改。我写了一个简单的程序来尝试这一点,但我没有收到任何事件。下面是我的代码:
CuratorFramework curator = new ZookeeperClient(zkHosts).getConnection();
CompletableFuture.runAsync(() -> {
CuratorWatcher curatorWatcher = event -> System.out.println("Watched event: " + event);
try {
curator.getChildren().usingWatcher(curatorWatcher).forPath(NODE_PATH);
} catch (Exception e) {
e.printStackTrace();
}
});
CompletableFuture.runAsync(() -> {
try {
curator.setData().forPath(NODE_PATH, "randomdata1".getBytes());
curator.setData().forPath(NODE_PATH, "randomdata2".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
});谢谢你的帮助!
发布于 2019-02-03 11:17:40
在Zookeeper中,getData()和exists()设置数据监视。getChildren()设置子监视;有关详细信息,请参阅ZooKeeper Watches
你应该在第一个runAsync()中使用curator.getData()而不是curator.getChildren(),因为你在第二个runAsync()中使用了setData()。
如果您想保留curator.getChildren(),那么您应该在NODE_PATH下添加一个新的子级进行测试
https://stackoverflow.com/questions/54490238
复制相似问题