Curator Watch事件监听

Curator Watch事件监听

  • Zookeeper提供了三种事件监听:

    • NodeCache: 给指定节点设置监听器
    • PathChildrenCache: 监听指定节点下的所有子节点
    • TreeCache: 监听指定节点以及该节点下的所有子节点
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class CuratorWatcherTest {
    private CuratorFramework clientBuild;

    /**
     * Before 所有Test测试方法执行时先执行该方法
     * 通过Curator操作zookeeper
     * 建立连接  
     * namespace -- 默认所有操作都是在test节点下操作
     */
    @Before
    public void testConnectBuild(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
        clientBuild = CuratorFrameworkFactory.builder()
                .connectString("192.168.192.137:2181")
                .sessionTimeoutMs(60000)
                .connectionTimeoutMs(15000)
                .retryPolicy(retryPolicy)
                .namespace("test")
                .build();
        clientBuild.start();
    }

    /**
     * 所有Test测试方法执行完成后执行该方法
     */
    @After
    public void close(){
        if (clientBuild != null){
            clientBuild.close();
        }
    }

    /**
     * NodeCache: 给指定节点设置监听器
     * NodeCache(CuratorFramework client, String path, boolean dataIsCompressed)
     * dataIsCompressed: 如果true,表示传递的数据会进行压缩,传递速度快,但取数据时需要把压缩的数据进行转换,默认为false
     */
    @Test
    public void testNodeCache() throws Exception {
        // 创建NodeCache对象
        NodeCache nodeCache = new NodeCache(clientBuild,"/app1");
        // 注册监听
        nodeCache.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                System.out.println("发生变化"); // 增删改都会发生变化
                // 获取节点修改后的数据
                byte[] data = nodeCache.getCurrentData().getData();
                System.out.println(new String(data));
            }
        });
        // 开启监听 如果设置为true,加载缓冲数据
        nodeCache.start(true);
        // 因为为单元测试,需要保证方法一直运行
        while (true){

        }
    }

    /**
     * PathChildrenCache: 监听指定节点下的所有子节点
     * @throws Exception
     */
    @Test
    public void testPathChildrenCache() throws Exception{
        // 创建监听对象
        PathChildrenCache pathChildrenCache = new PathChildrenCache(clientBuild,"/app2",true);
        // 注册监听
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
                System.out.println("子节点发生变化"); // 子节点增删改都会发生变化
                System.out.println(pathChildrenCacheEvent);
                // 获取客户端对子节点的修改操作,同时获取子节点修改后的数据信息
                // 1. 获取当前的操作类型
                PathChildrenCacheEvent.Type type = pathChildrenCacheEvent.getType();
                // 2. 判断是否为修改操作  PathChildrenCacheEvent.Type.CHILD_UPDATED
                if (type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
                    System.out.println("子节点发生了修改操作");
                    // 3. 获取子节点修改后的数据信息
                    byte[] data = pathChildrenCacheEvent.getData().getData();
                    System.out.println("子节点修改后的数据信息:" + new String(data));
                }
            }
        });
        // 开启监听
        pathChildrenCache.start();
        while (true){

        }
    }

    /**
     * TreeCache: 监听指定节点以及该节点下的所有子节点
     * @throws Exception
     */
    @Test
    public void testTreeCache() throws Exception{
        TreeCache treeCache = new TreeCache(clientBuild,"/app2");
        treeCache.getListenable().addListener(new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
                System.out.println("节点发生了变化");
                System.out.println(treeCacheEvent);
            }
        });
        treeCache.start();
        while (true){

        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值