Search in sources :

Example 66 with PathChildrenCacheEvent

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project np by liuhouer.

the class DistributedLock method addWatcherToLock.

/**
 * 分布式锁子节点添加watch事件,监听父节点子节点的移除--锁的释放
 */
public void addWatcherToLock(String path) throws Exception {
    final PathChildrenCache pathChildrenCache = new PathChildrenCache(client, path, true);
    pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            if (event.getType() == PathChildrenCacheEvent.Type.CHILD_REMOVED) {
                String path = event.getData().getPath();
                log.info("上一个会话已结束或已经释放锁路径:{}", path);
                if (path.contains(ZK_LOCK_PROJECT)) {
                    log.info("释放计数器,当前线程可获得分布式锁");
                    countDownLatch.countDown();
                }
            }
        }
    });
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

Example 67 with PathChildrenCacheEvent

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project study-by-myself by Howinfun.

the class NodeCacheDemo method pathChildrenCache.

private static void pathChildrenCache(String path) throws Exception {
    try {
        CuratorFramework client = CuratorClientFacotry.newClient();
        client.start();
        /**
         * PathChildrenCache 监听当前节点的子节点增删改查事件
         *
         * 所有的PathChildrenCache构造方法的前三个参数都是一样的。
         *  · 第一个参数就是传入创建的Curator框架的客户端。
         *  · 第二个参数就是监听节点的路径。
         *  · 第三个重载参数cacheData表示是否把节点的内容缓存起来。如果cacheData为true,那么接收到节点列表变更事件的同时会将获得节点内容。(如果为 false,那么 data 一直是 null,一直获取不了)
         *
         * 除了上边的三个参数,其他参数的说明如下:
         *  · dataIsCompressed参数,表示是否对节点数据进行压缩。
         *  · threadFactory参数表示线程池工厂,当PathChildrenCache内部需要启动新的线程执行时,使用该线程池工厂来创建线程。
         *  · executorService和threadFactory参数差不多,表示通过传入的线程池或者线程工厂来异步处理监听事件。
         *
         * start方法可以传入启动的模式,定义在StartMode枚举中,具体如下:
         * · NORMAL——异步初始化cache
         * · BUILD_INITIAL_CACHE——同步初始化cache
         * · POST_INITIALIZED_EVENT——异步初始化cache,并触发完成事件
         *
         * StartMode枚举的三种启动方式,详细说明如下:(
         * (1)BUILD_INITIAL_CACHE模式:启动时同步初始化cache,表示创建cache后就从服务器提取对应的数据;
         * (2)POST_INITIALIZED_EVENT模式:启动时异步初始化cache,表示创建cache后从服务器提取对应的数据,完成后触发PathChildrenCacheEvent.Type#INITIALIZED事件,cache中Listener会收到该事件的通知;
         * (3)NORMAL模式:启动时异步初始化cache,完成后不会发出通知。
         */
        PathChildrenCache nodeCache = new PathChildrenCache(client, path, true);
        PathChildrenCacheListener listener = new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
                switch(pathChildrenCacheEvent.getType()) {
                    case CHILD_ADDED:
                        log.info("子节点增加,path={},data={}", pathChildrenCacheEvent.getData().getPath(), new String(pathChildrenCacheEvent.getData().getData()));
                        return;
                    case CHILD_REMOVED:
                        log.info("子节点删除,path={},data={}", pathChildrenCacheEvent.getData().getPath(), new String(pathChildrenCacheEvent.getData().getData()));
                        return;
                    case CHILD_UPDATED:
                        log.info("子节点更新,path={},data={}", pathChildrenCacheEvent.getData().getPath(), new String(pathChildrenCacheEvent.getData().getData()));
                        return;
                    default:
                        return;
                }
            }
        };
        nodeCache.getListenable().addListener(listener);
        // 唯一的一个参数buildInitial代表着是否将该节点的数据立即进行缓存。如果设置为true的话,在start启动时立即调用NodeCache的getCurrentData方法就能够得到对应节点的信息ChildData类,如果设置为false,就得不到对应的信息。
        nodeCache.start();
    } catch (Exception e) {
        log.error("NodeCache 监听异常:{}", e.getMessage());
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

Example 68 with PathChildrenCacheEvent

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project drill by apache.

the class TestEventDispatcher method testDispatcherPropagatesEvents.

@Test
public void testDispatcherPropagatesEvents() throws Exception {
    final PathChildrenCacheEvent.Type[] types = new PathChildrenCacheEvent.Type[] { PathChildrenCacheEvent.Type.CHILD_ADDED, PathChildrenCacheEvent.Type.CHILD_REMOVED, PathChildrenCacheEvent.Type.CHILD_UPDATED };
    for (final PathChildrenCacheEvent.Type type : types) {
        dispatcher.childEvent(null, new PathChildrenCacheEvent(type, child));
        final TransientStoreEvent event = TransientStoreEvent.of(EventDispatcher.MAPPINGS.get(type), key, value);
        Mockito.verify(store).fireListeners(event);
    }
    Assert.assertEquals("Number of event types that dispatcher can handle is different", types.length, EventDispatcher.MAPPINGS.size());
}
Also used : TransientStoreEvent(org.apache.drill.exec.coord.store.TransientStoreEvent) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Aggregations

PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)68 CuratorFramework (org.apache.curator.framework.CuratorFramework)58 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)52 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)36 ChildData (org.apache.curator.framework.recipes.cache.ChildData)21 IOException (java.io.IOException)15 KeeperException (org.apache.zookeeper.KeeperException)11 Test (org.junit.Test)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 DataSegment (org.apache.druid.timeline.DataSegment)5 Map (java.util.Map)4 ZKPaths (org.apache.curator.utils.ZKPaths)4 RedisConfig (cn.northpark.zookeeper.RedisConfig)3 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)3 ServiceInstance (com.baidu.brpc.client.channel.ServiceInstance)3 RpcException (com.baidu.brpc.exceptions.RpcException)3 DataSegment (io.druid.timeline.DataSegment)3 Releasable (com.ctrip.xpipe.api.lifecycle.Releasable)2 RegistryPackage (com.polyu.blockchain.common.wrapper.RegistryPackage)2 ServiceDTO (io.fabric8.gateway.ServiceDTO)2