apache-zookeeper - 监控 znode 子节点上的 Zookeeper 添加和删除
问题描述
我正在寻找一个 Zookeeper 配方来监控在父节点中执行的操作(添加子节点、删除子节点等)
我无法弄清楚如何使用观察者来获取事件。任何帮助表示赞赏。
import org.apache.zookeeper.*;
import java.io.IOException;
public class ZookeeperMonitor {
private static ZooKeeper zookeeper;
public ZooKeeper connect(String connectionString, int port) throws IOException, InterruptedException {
ZookeeperWatcher watcher = new ZookeeperWatcher();
zookeeper = new ZooKeeper(connectionString, port, watcher);
return zookeeper;
}
public void checkForChanges() throws KeeperException, InterruptedException {
zookeeper.getChildren("/parent",new ChildChangeWatcher());
}
}
class ZookeeperWatcher implements Watcher {
public void process(WatchedEvent watchedEvent) {
}
}
class ChildChangeWatcher implements Watcher {
public void process(WatchedEvent e) {
if(e.getType() == Event.EventType.NodeChildrenChanged){
System.out.println("change in children");
}
}
}
如何将其作为长时间运行的守护程序运行?
也watcher
足够单独或者我应该使用callback
. 我不明白这里回调的目的。
解决方案
你不需要孩子的观察者,让 parentNode 实现观察者
public void checkForChanges() throws KeeperException, InterruptedException { zookeeper.getChildren("/parent",this); }
推荐阅读
- javascript - 将 useMemo 或 useCallbacks 添加到地图组件会破坏代码并显示错误“渲染的钩子比上一次渲染期间更多”
- javascript - 我的
不加载我的 js 文件并且我的浏览器无法加载它 - javascript - 在 JSON.stringify(obj) 之后反应 js 不同的值
- elasticsearch - Elasticsearch 操作现有字段值以添加新字段
- node.js - 重新连接到 EventStore 中的持久连接后不重复确认的事件
- python - PHP 不执行 Python 文件
- java - 以平滑的外观更改 JButton 文本和背景颜色
- python - 在 1 个项目中运行 2 个 Discord 机器人 (Discord.py)
- c# - C# Modbus:ReadHoldingRegisters 在返回输出中不一致
- r - 使用 Tidyverse 将整数转换为 R 中的向量