watcher监听

watch监听

watch特性

在这里插入图片描述

watch事件类型

在这里插入图片描述

watch事件捕获

在这里插入图片描述

watch监听连接状态

在这里插入图片描述
在这里插入图片描述

public class ZkConnectionWatcher implements Watcher {
    //计数器对象
    static CountDownLatch CountDownLatch=new CountDownLatch(1);
    static ZooKeeper zooKeeper;

    @Override
    public void process(WatchedEvent event) {
         if(event.getType()==Event.EventType.None){
             if(event.getState()==Event.KeeperState.SyncConnected){
                 System.out.println("连接创建成功");
                 //唤醒线程
                 CountDownLatch.countDown();
             }
             if(event.getState()==Event.KeeperState.Disconnected){
                 System.out.println("断开连接");
             }
             if(event.getState()==Event.KeeperState.Expired){
                 System.out.println("会话超时");
             }
             if(event.getState()==Event.KeeperState.AuthFailed){
                 System.out.println("认证失败");
             }
         }
    }

    public static void main(String[] args) {
        try{
            zooKeeper = new ZooKeeper("192.168.164.134:2181", 5000, new ZkConnectionWatcher());
            //阻塞线程直到连接成功被唤醒
            CountDownLatch.await();
            System.out.println(zooKeeper.getSessionId());
            Thread.sleep(50000);
            zooKeeper.close();
            System.out.println("结束。。。。。。。。。。。。。。。。");
        }catch (Exception e){

        }

    }
}

事件捕获之exists(检查节点是否存在)

exists监听有两种方式:1.使用连接对象watch2.自己重新定义一个watch

在这里插入图片描述

使用连接对象监听

public class ZkWatcherExists {
    String IP="192.168.164.134:2181";
    ZooKeeper zooKeeper=null;
    //计数器
    CountDownLatch countDownLatch=new CountDownLatch(1);
    @Before
    public void berfore() throws IOException {

        zooKeeper=  new ZooKeeper(IP, 5000, 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());

             }
         });
    }

    @Test
    public void test() throws KeeperException, InterruptedException {
        zooKeeper.exists("/exists",true);
        Thread.sleep(50000);
        System.out.println("结束");
    }
    @After
    public void after() throws InterruptedException {
        zooKeeper.close();
    }
}

自定义监听器

    @Test
    public void test2() throws KeeperException, InterruptedException {
        zooKeeper.exists("/exists", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("path="+event.getPath());
                System.out.println("eventType="+event.getType());
            }
        });
        Thread.sleep(50000);
        System.out.println("结束");
    }

watcher一次性

在这里插入图片描述

watcher失效后再次绑定

在这里插入图片描述

多监听器

在这里插入图片描述

事件捕获之getData()

在这里插入图片描述

使用连接对象监听

在这里插入图片描述

自定义监听器

在这里插入图片描述

watcher一次性

在这里插入图片描述

watcher失效后再次绑定

如果节点删除就不需要再次监听
在这里插入图片描述

多监听器(参考exists)

事件捕获之getChildren()

在这里插入图片描述

使用连接对象监听

在这里插入图片描述

自定义监听器

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桀骜浮沉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值