Search in sources :

Example 51 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.

the class CacheExample method main.

public static void main(String[] args) throws Exception {
    CuratorClient client = CuratorClient.getClientInstance("192.168.101.86:2181");
    final Set<String> set = new HashSet<String>();
    PathChildrenCache cache = new PathChildrenCache(client.getInnerClient(), PATH, true);
    cache.start();
    PathChildrenCacheListener listener1 = new PathChildrenCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            switch(event.getType()) {
                case CHILD_ADDED:
                    {
                        System.out.println("1Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                        System.out.println("1Node added: " + event.getData().getPath());
                        break;
                    }
                case CHILD_UPDATED:
                    {
                        System.out.println("1Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                        System.out.println("1Node changed: " + new String(event.getData().getData()));
                        break;
                    }
                case CHILD_REMOVED:
                    {
                        System.out.println("1Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                        break;
                    }
                default:
                    break;
            }
        }
    };
    // PathChildrenCacheListener listener2 = new PathChildrenCacheListener() {
    // 
    // @Override
    // public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    // switch (event.getType()) {
    // case CHILD_ADDED: {
    // System.out.println("2Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
    // System.out.println("2Node added: " + event.getData().getPath());
    // break;
    // }
    // case CHILD_UPDATED: {
    // System.out.println("2Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
    // break;
    // }
    // case CHILD_REMOVED: {
    // System.out.println("2Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
    // break;
    // }
    // default:
    // break;
    // }
    // }
    // };
    ExecutorService services = Executors.newFixedThreadPool(5);
    cache.getListenable().addListener(listener1, services);
    // cache.getListenable().addListener(listener2,services);
    // cache.getListenable().addListener(listener);
    Thread.sleep(Long.MAX_VALUE);
    cache.close();
    client.close();
}
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) CuratorClient(com.bonree.brfs.common.zookeeper.curator.CuratorClient) ExecutorService(java.util.concurrent.ExecutorService) HashSet(java.util.HashSet)

Example 52 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.

the class CuratorPathCache method addListener.

public void addListener(String path, AbstractPathChildrenCacheListener listener) {
    LOG.info("add listener for path:" + path);
    PathChildrenCache cache = cacheMap.get(path);
    if (cache == null) {
        cache = new PathChildrenCache(client.getInnerClient(), path, true);
        cacheMap.put(path, cache);
        startCache(path);
    }
    cache.getListenable().addListener(listener);
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

Example 53 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.

the class CuratorPathCache method removeListener.

public void removeListener(String path, AbstractPathChildrenCacheListener listener) {
    LOG.info("remove listener for path:" + path);
    PathChildrenCache cache = cacheMap.get(path);
    if (cache != null) {
        cache.getListenable().removeListener(listener);
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

Example 54 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.

the class CuratorPathCache method cancelListener.

public void cancelListener(String path) throws IOException {
    LOG.info("cancel listener for path:" + path);
    PathChildrenCache cache = cacheMap.get(path);
    if (cache != null) {
        cache.close();
        cacheMap.remove(path);
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

Example 55 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.

the class FileNodeDistributor method close.

@Override
public void close() {
    if (running.compareAndSet(true, false)) {
        timeEventEmitter.removeListener(TIMEOUT_CHECK_DURATION, this);
        serviceManager.removeServiceStateListener(DUPLICATE_SERVICE_GROUP, this);
        executor.shutdownNow();
        wildFileNodes.clear();
        for (PathChildrenCache watcher : childWatchers.values()) {
            CloseUtils.closeQuietly(watcher);
        }
        childWatchers.clear();
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

Aggregations

PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)68 IOException (java.io.IOException)25 CuratorFramework (org.apache.curator.framework.CuratorFramework)21 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)20 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)14 KeeperException (org.apache.zookeeper.KeeperException)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 ChildData (org.apache.curator.framework.recipes.cache.ChildData)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)6 Before (org.junit.Before)5 Test (org.junit.Test)5 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 ExecutorService (java.util.concurrent.ExecutorService)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 Slf4j (lombok.extern.slf4j.Slf4j)4 ZKPaths (org.apache.curator.utils.ZKPaths)4 Preconditions (com.google.common.base.Preconditions)3 StreamImpl (io.pravega.client.stream.impl.StreamImpl)3 CompletableFuture (java.util.concurrent.CompletableFuture)3