Search in sources :

Example 6 with TreeCacheListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener in project Saturn by vipshop.

the class ShardingTreeCacheService method addTreeCacheListenerIfAbsent.

public void addTreeCacheListenerIfAbsent(String path, int depth, TreeCacheListener treeCacheListener) throws ShardingException {
    synchronized (isShutdownFlag) {
        if (isShutdownFlag.get()) {
            throw new ShardingException("ShardingTreeCacheService has been shutdown");
        }
        String fullPath = namespace + path;
        TreeCacheListener treeCacheListenerOld = shardingTreeCache.addTreeCacheListenerIfAbsent(path, depth, treeCacheListener);
        if (treeCacheListenerOld == null) {
            LOGGER.info("add {}, full path is {}, depth is {}", treeCacheListener.getClass().getSimpleName(), fullPath, depth);
        }
    }
}
Also used : ShardingException(com.vip.saturn.job.sharding.exception.ShardingException) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener)

Example 7 with TreeCacheListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener in project Saturn by vipshop.

the class ShardingTreeCache method addTreeCacheListenerIfAbsent.

public TreeCacheListener addTreeCacheListenerIfAbsent(String path, int depth, TreeCacheListener treeCacheListener) {
    synchronized (this) {
        TreeCacheListener treeCacheListenerOld = null;
        String key = getKey(path, depth);
        TreeCache treeCache = treeCacheMap.get(key);
        if (treeCache == null) {
            logger.error("The TreeCache is not exists, cannot add TreeCacheListener, path is {}, depth is {}", path, depth);
        } else {
            List<TreeCacheListener> treeCacheListeners = treeCacheListenerMap.get(treeCache);
            boolean included = false;
            for (TreeCacheListener tmp : treeCacheListeners) {
                Class<? extends TreeCacheListener> tmpClass = tmp.getClass();
                Class<? extends TreeCacheListener> treeCacheListenerClass = treeCacheListener.getClass();
                if (tmpClass.equals(treeCacheListenerClass)) {
                    treeCacheListenerOld = tmp;
                    included = true;
                    break;
                }
            }
            if (included) {
                logger.info("The TreeCache has already included the instance of listener, will not be added, path is {}, depth is {}, listener is {}", path, depth, treeCacheListener.getClass());
            } else {
                treeCacheListeners.add(treeCacheListener);
                treeCache.getListenable().addListener(treeCacheListener);
            }
        }
        return treeCacheListenerOld;
    }
}
Also used : TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener)

Example 8 with TreeCacheListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener in project metron by apache.

the class ZKCache method start.

/**
 * Start the cache.
 * @throws Exception If unable to be started.
 */
public void start() throws Exception {
    if (cache == null) {
        if (ownClient) {
            client.start();
        }
        TreeCache.Builder builder = TreeCache.newBuilder(client, zkRoot);
        builder.setCacheData(true);
        cache = builder.build();
        for (TreeCacheListener l : listeners) {
            cache.getListenable().addListener(l);
        }
        cache.start();
    }
}
Also used : TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener)

Example 9 with TreeCacheListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener in project coprhd-controller by CoprHD.

the class DistributedLockQueueManagerImpl method addLoggingCacheEventListener.

/**
 * For logging purposes only, this will cause all nodes to log received TreeCache events.
 */
private void addLoggingCacheEventListener() {
    TreeCacheListener listener = new TreeCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
            switch(event.getType()) {
                case NODE_ADDED:
                    log.debug("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                    break;
                case NODE_UPDATED:
                    log.debug("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                    break;
                case NODE_REMOVED:
                    log.debug("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                    break;
            }
        }
    };
    treeCache.getListenable().addListener(listener);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent)

Example 10 with TreeCacheListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener in project flink by apache.

the class ZooKeeperUtilsTreeCacheTest method testCallbackNotCalledOnConnectionOrInitializationEvents.

@Test
public void testCallbackNotCalledOnConnectionOrInitializationEvents() throws Exception {
    final TreeCacheListener treeCacheListener = ZooKeeperUtils.createTreeCacheListener(() -> {
        throw new AssertionError("Should not be called.");
    });
    treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.INITIALIZED, null));
    treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.CONNECTION_RECONNECTED, null));
    treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.CONNECTION_LOST, null));
    treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.CONNECTION_SUSPENDED, null));
}
Also used : TreeCacheListener(org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener) TreeCacheEvent(org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent) Test(org.junit.Test)

Aggregations

TreeCacheListener (org.apache.curator.framework.recipes.cache.TreeCacheListener)9 TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)6 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)5 ChildData (org.apache.curator.framework.recipes.cache.ChildData)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TimeDiffIntolerableException (com.vip.saturn.job.exception.TimeDiffIntolerableException)1 ShardingException (com.vip.saturn.job.sharding.exception.ShardingException)1 SaturnThreadFactory (com.vip.saturn.job.threads.SaturnThreadFactory)1 DataChangedEvent (io.shardingjdbc.orchestration.reg.listener.DataChangedEvent)1 StandardCharsets (java.nio.charset.StandardCharsets)1 EnumMap (java.util.EnumMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)1