public static void main(String[] args) throws Exception {
ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000);
zkClient.createEphemeral("/zkclient");
zkClient.subscribeDataChanges("/zkclient", new IZkDataListener() {
@Override
public void handleDataDeleted(String dataPath) throws Exception {
System.out.println(String.format("The node '%s' is deleted.", dataPath));
}
@Override
public void handleDataChange(String dataPath, Object data) throws Exception {
System.out.println(String.format("The node '%s' is changed, now its data is '%s'.", dataPath, data));
}
});
zkClient.writeData("/zkclient", "hello world");
Thread.sleep(1000);
zkClient.delete("/zkclient");
Thread.sleep(1000);
zkClient.close();
}
这段代码输出的内容是:
The node '/zkclient' is changed, now its data is 'hello world'.
The node '/zkclient' is deleted.
public static void main(String[] args) throws Exception {
ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000);
zkClient.createEphemeral("/zkclient");
zkClient.subscribeDataChanges("/zkclient", new IZkDataListener() {
@Override
public void handleDataDeleted(String dataPath) throws Exception {
System.out.println(String.format("The node '%s' is deleted.", dataPath));
}
@Override
public void handleDataChange(String dataPath, Object data) throws Exception {
System.out.println(String.format("The node '%s' is changed, now its data is '%s'.", dataPath, data));
}
});
zkClient.writeData("/zkclient", "hello world");
zkClient.delete("/zkclient");
Thread.sleep(1000);
zkClient.close();
}
这段代码的输出内容却是:
The node '/zkclient' is deleted.
The node '/zkclient' is deleted.
为什么会接收到两次删除节点事件?