ZkClient watch 操作

忘是亡心i 2022-02-02 06:49 305阅读 0赞

文章目录

      • 1、依赖
      • 2、zkClient.subscribeChildChanges() 监听当前节点和下面子节点的新增、删除
        • 2.1、运行结果:
      • 3、zkClient.subscribeDataChanges() 监听 当前节点和子节点的内容修改、删除
        • 3.1、运行结果:

1、依赖

  1. <dependency>
  2. <groupId>com.101tec</groupId>
  3. <artifactId>zkclient</artifactId>
  4. <version>0.10</version>
  5. </dependency>

2、zkClient.subscribeChildChanges() 监听当前节点和下面子节点的新增、删除

监听当前节点和下面子节点的新增、删除。不监听数据发生修改和变化。

参数说明:

  • 第1个参数: 路径path,要监听的节点
  • 第2个参数:这个一个回调函数,要实现 IZkChildListener 接口,并重写 subscribeChildChanges()

    package com.aop8.zkclient.watcher;

    public class ZkClientWatcher1 {

    1. /** session超时时间 */
    2. static final int SESSION_OUTTIME = 5000;//ms
    3. public static void main(String[] args) throws Exception {
    4. ZkClient zkc = new ZkClient(new ZkConnection("127.0.0.1:2181"), 5000);
    5. //对父节点添加监听子节点变化。
    6. zkc.subscribeChildChanges("/super", new IZkChildListener() {
    7. @Override
    8. public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
    9. System.out.println("parentPath: " + parentPath);
    10. System.out.println("currentChilds: " + currentChilds);
    11. }
    12. });
    13. Thread.sleep(3000);
    14. zkc.createPersistent("/super");
    15. Thread.sleep(1000);
    16. zkc.createPersistent("/super" + "/" + "c1", "c1内容");
    17. Thread.sleep(1000);
    18. zkc.createPersistent("/super" + "/" + "c2", "c2内容");
    19. Thread.sleep(1000);
    20. zkc.delete("/super/c2");
    21. Thread.sleep(1000);
    22. zkc.deleteRecursive("/super");
    23. Thread.sleep(Integer.MAX_VALUE);
    24. }

    }

2.1、运行结果:

  1. parentPath: /super
  2. currentChilds: []
  3. ============添加 c1 ==============
  4. parentPath: /super
  5. currentChilds: [c1]
  6. ============添加 c2 ==============
  7. parentPath: /super
  8. currentChilds: [c1, c2]
  9. ============删除 c2 ==============
  10. parentPath: /super
  11. currentChilds: [c1]
  12. ============删除 super ==============
  13. parentPath: /super
  14. currentChilds: null
  15. parentPath: /super
  16. currentChilds: null

3、zkClient.subscribeDataChanges() 监听 当前节点和子节点的内容修改、删除

这是一个回调函数,要实现 IZkDataListener 接口,并重写 handleDataChange() 和 handleDataDeleted() 两个方法。

  1. package com.aop8.zkclient.watcher;
  2. public class ZkClientWatcher2 {
  3. /** session超时时间 */
  4. static final int SESSION_OUTTIME = 5000;//ms
  5. public static void main(String[] args) throws Exception {
  6. ZkClient zkc = new ZkClient(new ZkConnection("127.0.0.1:2181"), 5000);
  7. zkc.createPersistent("/super", "1234");
  8. //对父节点添加监听子节点变化。
  9. zkc.subscribeDataChanges("/super", new IZkDataListener() {
  10. @Override
  11. public void handleDataChange(String path, Object data) throws Exception {
  12. System.out.println("变更的节点为:" + path + ", 变更内容为:" + data);
  13. }
  14. @Override
  15. public void handleDataDeleted(String path) throws Exception {
  16. System.out.println("删除的节点为:" + path);
  17. }
  18. });
  19. Thread.sleep(3000);
  20. zkc.writeData("/super", "456", -1);
  21. Thread.sleep(1000);
  22. zkc.delete("/super");
  23. Thread.sleep(Integer.MAX_VALUE);
  24. zkc.close();
  25. }
  26. }

3.1、运行结果:

  1. 变更的节点为:/super, 变更内容为:456
  2. 删除的节点为:/super

发表评论

表情:
评论列表 (有 0 条评论,305人围观)

还没有评论,来说两句吧...

相关阅读