ZkClient 事件监听器的疑问

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.

为什么会接收到两次删除节点事件?

阅读 2.2k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏