二十一、curator recipes之TreeCache

简介

curator的TreeCache允许对某个路径的数据和路径变更以及其下所有子孙节点的数据和路径变更进行监听。

官方文档:http://curator.apache.org/curator-recipes/tree-cache.html

javaDoc:http://curator.apache.org/apidocs/org/apache/curator/framework/recipes/cache/TreeCache.html

代码示例

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class TreeCacheDemo {
    private static CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(3000, 1));
    private static String path = "/treeCache/test/001";
    static {
        client.start();
    }

    public static void main(String[] args) throws Exception {
        if (client.checkExists().forPath(path) == null) {
            client.create().creatingParentsIfNeeded().forPath(path);
        }
        TreeCache cache = new TreeCache(client, path);
        cache.getListenable().addListener((curatorFramework, treeCacheEvent) -> System.out.println(treeCacheEvent.toString()));
        cache.start();
        client.setData().forPath(path, "lay".getBytes());
        client.create().forPath(path + "/child");
        client.setData().forPath(path + "/child", "marry".getBytes());
        client.delete().forPath(path + "/child");
        client.delete().forPath(path);
        Thread.sleep(4000);
        cache.close();
        Thread.sleep(50000);
        client.close();
    }
}

 

转载于:https://www.cnblogs.com/lay2017/p/10280254.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值