zookeeper事件监听机制

一.watcher概念

zookeeper提供给了数据的发布/订阅功能,多个订阅者可同时监听某一特定主题对象,当该主题对象的自身状态发送变化时(例如节点内容改变、节点下的子节点列表改变等),会实时、主动通知所有订阅者。

zookeeper采用了watcher机制实现数据的发布/订阅功能,该机制在被订阅对象发送变化时会异步通知客户端,因此客户端不必再watcher注册后轮询阻塞,从而减轻了客户端压力。

watcher机制实际上与观察者模式类似,也可看作是一种观察者模式在分布式场景下的实现方式。

二.Watcher架构

1.watcher实现的3个部分

  • zookeeper服务端
  • zookeeper客户端
  • 客户端的ZKWatchManager对象

2.实现流程

客户端首先将Watcher注册到服务端,同时将Watcher对象保存到客户端的Watch管理器中。当Zookeeper服务端监听的数据状态发送变化时,服务端会主动通知客户端,接着客户端的Watch管理器会触发相关Watcher来回调相应处理逻辑,从而完成整体的数据发布/订阅流程。

3.图解

图片

三.watcher特性

特性说明
一次性watcher是一次性的,一旦被触发就会移除,再次使用时需要重新注册。
客户端顺序回调watcher回调是顺序串行化执行的,只有回调后客户端才能看到最新的数据状态。一个watcher回调逻辑不应该太多,以免影响别的watcher执行。
轻量级WatcherEvent是最小的通信单元,结构上质保函通知状态、事件类型和节点路径,并不会告诉数据节点变化前后的具体内容。
时效性watcher只有在当前session彻底失效时才会无效,若在session有效期内快速重连成功,则watcher依然存在,仍可接受到通知。

四.watcher的状态

1.watcher通知状态(keeperState)

keeperState是客户端与服务端连接状态发送变化时对应的通知类型。路径为org.apache.zookeeper.Watcher.Event.KeeperState,是一个枚举类,其枚举属性如下:

枚举属性说明
SyncConnected客户端与服务器正常连接时
Disconnected客户端与服务器断开连接时
Expired会话session失效时
AuthFailed身份认证失败时

2.watcher事件类型(EventType)

EventType是数据节点(znode)发送变化时对应的通知类型。EventType变化时KeeperState永远处于SyncConnected通知状态下;当KeeperState发送变化时,EventType永远为None,其路径为org.apache.zookeeper.Watcher.Event.EventType,是一个枚举类,枚举属性如下:

枚举属性说明
None
NodeCreatedWatcher监听的数据节点被创建时
NodeDeletedWatcher监听的数据节点被删除时
NodeDataChangedWatcher监听的数据节点内容发送变更时(无论内容数据是否变化)
NodeChildrenChangedWatcher监听的数据节点的子节点列表发送变更时

注:客户端接收到的相关事件通知中只包含状态及类型等信息,不包括节点变化前后的具体内容,变化前的数据需业务自身存储,变化后的数据需调用get等方法重新获取。

五.捕获事件的入门案例

1.说明

zookeeper的watcher监听,在zookeeper中采用zk.getChildren(path,watch)、zj.exists(path,watch)、zk.getData(path,wattcher,stat)这样的方式为某个znode注册监听。

注册方式createdchildrenChangedchangeddeleted
zk.exists("/node-x",watcher)可监控可监控可监控
zk.getData("/node-x",watcher)可监控可监控
zk.getChildren("/node-x",watcher)可监控可监控

2.watcher的使用范例

2.1对象说明

KeeperState:通知状态。

SyncConnected:客户端与服务器正常连接时。

Disconnected:客户端与服务器断开连接时。

Expired:会话session失效时。

AuthFailed:身份认证失败时。

事件类型为:None

2.2调用类

(2.2.1)代码块
package com.fengmo.zookeeper;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import sun.awt.windows.ThemeReader;
import java.util.concurrent.CountDownLatch;
//通过java设置监听器
public class WatcherDemo01  implements Watcher {
    //计数器对象
    static CountDownLatch countDownLatch = new CountDownLatch(1);
    //连接对象
    static ZooKeeper zooKeeper;
    @Override
    public void process(WatchedEvent event) {
        try{
            //事件类型
            if(event.getType() == Event.EventType.None){
                //判断连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    System.out.println("连接创建watcher成功");
                    //唤醒主线程
                    countDownLatch.countDown();
                }else if(event.getState() == Event.KeeperState.Disconnected){
                    System.out.println("断开连接");
                }else if(event.getState() == Event.KeeperState.Expired){
                    System.out.println("会话超时");
                    //参数一:zookeeper服务器地址,参数二:连接超时,参数三:watcher实现类
                    zooKeeper = new ZooKeeper("192.168.83.136:2181",5000,new WatcherDemo01());
                }else if(event.getState() == Event.KeeperState.AuthFailed){ //当scheme设置错误才通过
                    System.out.println("认证失败");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        try {
            //参数一:zookeeper服务器地址,参数二:连接超时,参数三:watcher实现类
            zooKeeper = new ZooKeeper("192.168.83.136:2181",5000,new WatcherDemo01());
            //阻塞线程,等待连接的创建
            countDownLatch.await();
            //获取会话id
            System.out.println("这是会话的id" + zooKeeper.getSessionId());
            //添加授权用户
            zooKeeper.addAuthInfo("digest","user2:user2".getBytes());
            //读取节点的数据
            byte[] bytes = zooKeeper.getData("/watcher1", false, null);
            System.out.println(new String(bytes));
            Thread.sleep(5000);
//            zooKeeper.close();
            System.out.println("结束");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
(2.2.2)范例

图片

图片

图片

六.检查节点是否存在

1.语法

//使用连接对象的监视器
exists(String path,boolean b)
//自定义监视器
exists(String path,watcher w)
//NodeCreated:节点创建
//NodeDeleted:节点删除
//NodeDataChanged:节点内容发送变化
  • path-znode路径。
  • b-是否使用连接对象中注册的监视器。
  • w-监视器对象。

2.演示一【创建zookeeper对象并设置监听器】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo02 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //调用创建zookeeper的时候,建立的通用监听器实现类
    public void watcherExists1() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:开启监听器
        zooKeeper.exists("/watcher1",true);
        Thread.sleep(5000);
        System.out.println("结束");
    }
    @Test
    //调用自定义的监听器
    public void watcherExists2() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //调用一次性监听器
    public void watcherExists3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        //调用一次性监听器
        zooKeeper.exists("/watcher1",watcher);
        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //调用监听器【多次事件捕获】
    public void watcherExists4() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //调用一次性监听器
                    zooKeeper.exists("/watcher1", this);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        };
        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //同时调用多个监听器
    public void watcherExists5() throws InterruptedException, KeeperException {
        //监听器1
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("1");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        //监听器二
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("2");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(500000);
        System.out.println("结束");
    }
}

(2)范例

图片

图片

3.演示二【创建自定义监听器】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo02 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //调用创建zookeeper的时候,建立的通用监听器实现类
    public void watcherExists1() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:开启监听器
        zooKeeper.exists("/watcher1",true);
        Thread.sleep(5000);
        System.out.println("结束");
    }
    @Test
    //调用自定义的监听器
    public void watcherExists2() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //调用一次性监听器
    public void watcherExists3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        //调用一次性监听器
        zooKeeper.exists("/watcher1",watcher);
        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //调用监听器【多次事件捕获】
    public void watcherExists4() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //调用一次性监听器
                    zooKeeper.exists("/watcher1", this);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        };
        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //同时调用多个监听器
    public void watcherExists5() throws InterruptedException, KeeperException {
        //监听器1
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("1");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        //监听器二
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("2");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(500000);
        System.out.println("结束");
    }
}

(2)范例

图片

图片

4.演示三【创建一次性监听器】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo02 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    
    @Test
    //调用一次性监听器
    public void watcherExists3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        //调用一次性监听器
        zooKeeper.exists("/watcher1",watcher);
        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //调用自定义的监听器
    public void watcherExists2() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //调用创建zookeeper的时候,建立的通用监听器实现类
    public void watcherExists1() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:开启监听器
        zooKeeper.exists("/watcher1",true);
        Thread.sleep(5000);
        System.out.println("结束");
    }
    @Test
    //调用监听器【多次事件捕获】
    public void watcherExists4() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //调用一次性监听器
                    zooKeeper.exists("/watcher1", this);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        };

        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //同时调用多个监听器
    public void watcherExists5() throws InterruptedException, KeeperException {
        //监听器1
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("1");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        //监听器二
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("2");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(500000);
        System.out.println("结束");
    }
}

(2)范例

图片

5.演示四【创建持久性监听器】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo02 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //同时调用多个监听器
    public void watcherExists5() throws InterruptedException, KeeperException {
        //监听器1
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("1");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        //监听器二
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("2");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //调用监听器【多次事件捕获】
    public void watcherExists4() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //调用该持久性监听器
                    zooKeeper.exists("/watcher1", this);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        };
        zooKeeper.exists("/watcher1", watcher);
        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //调用一次性监听器
    public void watcherExists3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        //调用一次性监听器
        zooKeeper.exists("/watcher1",watcher);
        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //调用自定义的监听器
    public void watcherExists2() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //调用创建zookeeper的时候,建立的通用监听器实现类
    public void watcherExists1() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:开启监听器
        zooKeeper.exists("/watcher1",true);
        Thread.sleep(5000);
        System.out.println("结束");
    }
}

(2)范例

图片

图片

6.演示五【同时调用多个事件监听器】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo02 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //同时调用多个监听器
    public void watcherExists5() throws InterruptedException, KeeperException {
        //监听器1
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("1");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        //监听器二
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("2");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //调用监听器【多次事件捕获】
    public void watcherExists4() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //调用该持久性监听器
                    zooKeeper.exists("/watcher1", this);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        };
        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //调用一次性监听器
    public void watcherExists3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        //调用一次性监听器
        zooKeeper.exists("/watcher1",watcher);
        Thread.sleep(500000);
        System.out.println("结束");
    }
    @Test
    //调用自定义的监听器
    public void watcherExists2() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        zooKeeper.exists("/watcher1", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //调用创建zookeeper的时候,建立的通用监听器实现类
    public void watcherExists1() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:开启监听器
        zooKeeper.exists("/watcher1",true);
        Thread.sleep(5000);
        System.out.println("结束");
    }

}

(2)范例

图片

图片

图片

7.注意事项

图片

在执行测试方法的时候,在客户端进行zookeeper服务器上指定节点的增删改查功能,则控制台会返回如图显示。eventType=事件类型。

七.查看节点的信息

1.语法

//使用连接对象的监视器
getData(String path,boolean b,Stat stat)
//自定义监视器
getData(String path,watcher w,Stat stat)
//NodeDeleted:节点删除
//NodeDataChanged:节点内容发生变化
  • path-znode路径。
  • b-是否使用连接对象中注册的监视器。
  • w-监视器对象。
  • stat-返回znode的元数据。

2.演示一【通过连接对象的监听器进行监听】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo03 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //使用连接对象的监听器
    public void watcherGetData1() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:连接对象的监听器开启
        zooKeeper.getData("/watcher2",true,null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //通过自定义监听器获取节点的状态信息
    public void watcherGetData2() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:自定义watcher
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("捕获监听的事件");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
    }
    @Test
    //调用一次性监听器
    public void watcherGetData3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        zooKeeper.getData("/watcher2",watcher,null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //设置持久化的监听器
    public void watcherGetData4() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //判断通过则循环调用本类
                    if(watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                        zooKeeper.getData("/watcher2", this, null);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.getData("/watcher2", watcher, null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //同时注册多个监听器
    public void watcherGetData5() throws InterruptedException, KeeperException {
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第一个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第二个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
}

(2)范例

图片

图片

3.演示二【通过自定义监听器来获取节点状态】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo03 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //使用连接对象的监听器
    public void watcherGetData1() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:连接对象的监听器开启
        zooKeeper.getData("/watcher2",true,null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //通过自定义监听器获取节点的状态信息
    public void watcherGetData2() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:自定义watcher
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("捕获监听的事件");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
    }
    @Test
    //调用一次性监听器
    public void watcherGetData3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        zooKeeper.getData("/watcher2",watcher,null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //设置持久化的监听器
    public void watcherGetData4() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //判断通过则循环调用本类
                    if(watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                        zooKeeper.getData("/watcher2", this, null);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.getData("/watcher2", watcher, null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //同时注册多个监听器
    public void watcherGetData5() throws InterruptedException, KeeperException {
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第一个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第二个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
}

(2)范例

图片

4.演示三【调用一次性监听器】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo03 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //使用连接对象的监听器
    public void watcherGetData1() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:连接对象的监听器开启
        zooKeeper.getData("/watcher2",true,null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //通过自定义监听器获取节点的状态信息
    public void watcherGetData2() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:自定义watcher
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("捕获监听的事件");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
    }
    @Test
    //调用一次性监听器
    public void watcherGetData3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        zooKeeper.getData("/watcher2",watcher,null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //设置持久化的监听器
    public void watcherGetData4() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //判断通过则循环调用本类
                    if(watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                        zooKeeper.getData("/watcher2", this, null);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.getData("/watcher2", watcher, null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //同时注册多个监听器
    public void watcherGetData5() throws InterruptedException, KeeperException {
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第一个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第二个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
}

(2)范例

图片

5.演示四【持久化监听器】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo03 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //使用连接对象的监听器
    public void watcherGetData1() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:连接对象的监听器开启
        zooKeeper.getData("/watcher2",true,null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //通过自定义监听器获取节点的状态信息
    public void watcherGetData2() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:自定义watcher
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("捕获监听的事件");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
    }
    @Test
    //调用一次性监听器
    public void watcherGetData3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        zooKeeper.getData("/watcher2",watcher,null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //设置持久化的监听器
    public void watcherGetData4() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //判断通过则循环调用本类
                    if(watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                        zooKeeper.getData("/watcher2", this, null);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.getData("/watcher2", watcher, null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //同时注册多个监听器
    public void watcherGetData5() throws InterruptedException, KeeperException {
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第一个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第二个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
}

(2)范例

图片

6.演示五【同时注册多个监听器】

(1)代码块

(2)范例package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo03 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //使用连接对象的监听器
    public void watcherGetData1() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:连接对象的监听器开启
        zooKeeper.getData("/watcher2",true,null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //通过自定义监听器获取节点的状态信息
    public void watcherGetData2() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:自定义watcher
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("捕获监听的事件");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
    }
    @Test
    //调用一次性监听器
    public void watcherGetData3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        zooKeeper.getData("/watcher2",watcher,null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //设置持久化的监听器
    public void watcherGetData4() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //判断通过则循环调用本类
                    if(watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                        zooKeeper.getData("/watcher2", this, null);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.getData("/watcher2", watcher, null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //同时注册多个监听器
    public void watcherGetData5() throws InterruptedException, KeeperException {
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第一个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        zooKeeper.getData("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第二个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
}

(2)范例

图片

八.查看子节点

1.语法

//使用连接对象的监听器
getChildren(String path,boolean b)
//自定义监视器
getChildren(String path,Watcher w)
//NodeChildrenChanged:子节点发生变化
//b-是否使用连接对象中注册的监视器
//w-监视器对象

2.演示一【通过连接对象的监听器进行监听】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo04 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //使用连接对象的监听器
    public void watcherGetChild1() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:连接对象的监听器开启
        zooKeeper.getChildren("/watcher2",true);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //通过自定义监听器获取子节点
    public void watcherGetChild2() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:自定义watcher
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("捕获监听的事件");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //调用一次性监听器
    public void watcherGetChild3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        zooKeeper.getChildren("/watcher2",watcher);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //设置持久化的监听器
    public void watcherGetChild4() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //判断通过则循环调用本类
                    if(watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
                        zooKeeper.getChildren("/watcher2", this);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.getChildren("/watcher2", watcher);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //同时注册多个监听器
    public void watcherGetChild5() throws InterruptedException, KeeperException {
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第一个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第二个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(50000);
        System.out.println("结束");
    }
}

(2)范例

图片

图片

3.演示二【通过自定义监听器进行监听】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo04 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //使用连接对象的监听器
    public void watcherGetChild1() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:连接对象的监听器开启
        zooKeeper.getChildren("/watcher2",true);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //通过自定义监听器获取子节点
    public void watcherGetChild2() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:自定义watcher
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("捕获监听的事件");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //调用一次性监听器
    public void watcherGetChild3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        zooKeeper.getChildren("/watcher2",watcher);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //设置持久化的监听器
    public void watcherGetChild4() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //判断通过则循环调用本类
                    if(watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
                        zooKeeper.getChildren("/watcher2", this);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.getChildren("/watcher2", watcher);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //同时注册多个监听器
    public void watcherGetChild5() throws InterruptedException, KeeperException {
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第一个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第二个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(50000);
        System.out.println("结束");
    }
}

(2)范例

图片

4.演示三【设置一次性监听器】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo04 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //使用连接对象的监听器
    public void watcherGetChild1() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:连接对象的监听器开启
        zooKeeper.getChildren("/watcher2",true);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //通过自定义监听器获取子节点
    public void watcherGetChild2() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:自定义watcher
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("捕获监听的事件");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //调用一次性监听器
    public void watcherGetChild3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        zooKeeper.getChildren("/watcher2",watcher);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //设置持久化的监听器
    public void watcherGetChild4() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //判断通过则循环调用本类
                    if(watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
                        zooKeeper.getChildren("/watcher2", this);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.getChildren("/watcher2", watcher);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //同时注册多个监听器
    public void watcherGetChild5() throws InterruptedException, KeeperException {
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第一个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第二个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(50000);
        System.out.println("结束");
    }
}

(2)范例

图片

5.演示四【持久性监听器】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo04 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //使用连接对象的监听器
    public void watcherGetChild1() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:连接对象的监听器开启
        zooKeeper.getChildren("/watcher2",true);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //通过自定义监听器获取子节点
    public void watcherGetChild2() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:自定义watcher
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("捕获监听的事件");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //调用一次性监听器
    public void watcherGetChild3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        zooKeeper.getChildren("/watcher2",watcher);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //设置持久化的监听器
    public void watcherGetChild4() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //判断通过则循环调用本类
                    if(watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
                        zooKeeper.getChildren("/watcher2", this);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.getChildren("/watcher2", watcher);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //同时注册多个监听器
    public void watcherGetChild5() throws InterruptedException, KeeperException {
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第一个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第二个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(50000);
        System.out.println("结束");
    }
}

(2)范例

图片

6.演示五【同时调用多个监听器】

(1)代码块

package com.fengmo.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
//监听器的应用
public class WatcherDemo04 {
    String IP = "192.168.83.136:2181";
    ZooKeeper zooKeeper = null;
    @Before
    public void before()throws IOException,InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(1);
        //连接zookeeper客户端
        zooKeeper = new ZooKeeper(IP, 6000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("连接对象的参数");
                //连接成功
                if(event.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                }
                System.out.println("path=" + event.getPath());
                System.out.println("eventType=" + event.getType());
            }
        });
        countDownLatch.await();
    }
    @After
    public void after()throws InterruptedException{
        zooKeeper.close();
    }
    @Test
    //使用连接对象的监听器
    public void watcherGetChild1() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:连接对象的监听器开启
        zooKeeper.getChildren("/watcher2",true);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //通过自定义监听器获取子节点
    public void watcherGetChild2() throws InterruptedException, KeeperException {
        //参数一:节点路径。参数二:自定义watcher
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("捕获监听的事件");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        },null);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //调用一次性监听器
    public void watcherGetChild3() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        };
        zooKeeper.getChildren("/watcher2",watcher);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //设置持久化的监听器
    public void watcherGetChild4() throws InterruptedException, KeeperException {
        //参数一:节点路径,参数二:自定义监听器
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path=" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    //判断通过则循环调用本类
                    if(watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
                        zooKeeper.getChildren("/watcher2", this);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.getChildren("/watcher2", watcher);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @Test
    //同时注册多个监听器
    public void watcherGetChild5() throws InterruptedException, KeeperException {
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第一个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        zooKeeper.getChildren("/watcher2", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("开启第二个监听器");
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(50000);
        System.out.println("结束");
    }
}

(2)范例

图片

九.源码

zookeeperDemo02.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值