Search in sources :

Example 6 with TreeCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project sharding-jdbc by shardingjdbc.

the class ZookeeperRegistryCenter method watch.

@Override
public void watch(final String key, final EventListener eventListener) {
    final String path = key + "/";
    if (!caches.containsKey(path)) {
        addCacheData(key);
    }
    TreeCache cache = caches.get(path);
    cache.getListenable().addListener(new TreeCacheListener() {

        @Override
        public void childEvent(final CuratorFramework client, final TreeCacheEvent event) throws Exception {
            ChildData data = event.getData();
            if (null == data || null == data.getPath()) {
                return;
            }
            eventListener.onChange(new DataChangedEvent(getEventType(event), data.getPath(), null == data.getData() ? null : new String(data.getData(), "UTF-8")));
        }

        private DataChangedEvent.Type getEventType(final TreeCacheEvent event) {
            switch(event.getType()) {
                case NODE_UPDATED:
                    return DataChangedEvent.Type.UPDATED;
                case NODE_REMOVED:
                    return DataChangedEvent.Type.DELETED;
                default:
                    return DataChangedEvent.Type.IGNORED;
            }
        }
    });
}
Also used : DataChangedEvent(io.shardingjdbc.orchestration.reg.listener.DataChangedEvent) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) CuratorFramework(org.apache.curator.framework.CuratorFramework) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) OperationTimeoutException(org.apache.zookeeper.KeeperException.OperationTimeoutException)

Example 7 with TreeCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project dubbo by alibaba.

the class ZKTools method testTreeCache.

public static void testTreeCache() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", 60 * 1000, 60 * 1000, new ExponentialBackoffRetry(1000, 3));
    client.start();
    CountDownLatch latch = new CountDownLatch(1);
    TreeCache treeCache = TreeCache.newBuilder(client, "/dubbo/config").setCacheData(true).build();
    treeCache.start();
    treeCache.getListenable().addListener(new TreeCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
            TreeCacheEvent.Type type = event.getType();
            ChildData data = event.getData();
            if (type == TreeCacheEvent.Type.INITIALIZED) {
                latch.countDown();
            }
            System.out.println(data.getPath() + "\n");
            if (data.getPath().split("/").length == 5) {
                byte[] value = data.getData();
                String stringValue = new String(value, StandardCharsets.UTF_8);
                // fire event to all listeners
                Map<String, Object> added = null;
                Map<String, Object> changed = null;
                Map<String, Object> deleted = null;
                switch(type) {
                    case NODE_ADDED:
                        added = new HashMap<>(1);
                        added.put(pathToKey(data.getPath()), stringValue);
                        added.forEach((k, v) -> System.out.println(k + "  " + v));
                        break;
                    case NODE_REMOVED:
                        deleted = new HashMap<>(1);
                        deleted.put(pathToKey(data.getPath()), stringValue);
                        deleted.forEach((k, v) -> System.out.println(k + "  " + v));
                        break;
                    case NODE_UPDATED:
                        changed = new HashMap<>(1);
                        changed.put(pathToKey(data.getPath()), stringValue);
                        changed.forEach((k, v) -> System.out.println(k + "  " + v));
                }
            }
        }
    });
    latch.await();
/* Map<String, ChildData> dataMap = treeCache.getCurrentChildren("/dubbo/config");
        dataMap.forEach((k, v) -> {
            System.out.println(k);
            treeCache.getCurrentChildren("/dubbo/config/" + k).forEach((ck, cv) -> {
                System.out.println(ck);
            });
        });*/
}
Also used : TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) CuratorListener(org.apache.curator.framework.api.CuratorListener) HashMap(java.util.HashMap) NamedThreadFactory(org.apache.dubbo.common.utils.NamedThreadFactory) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) StringUtils(org.apache.dubbo.common.utils.StringUtils) ChildData(org.apache.curator.framework.recipes.cache.ChildData) CountDownLatch(java.util.concurrent.CountDownLatch) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Map(java.util.Map) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) ExecutorService(java.util.concurrent.ExecutorService) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) HashMap(java.util.HashMap) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with TreeCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project paascloud-master by paascloud.

the class MqBeanInitRunner method initProducerListener.

private void initProducerListener(CuratorFramework cf) throws Exception {
    TreeCache treeCache = new TreeCache(cf, GlobalConstant.ZK_REGISTRY_PRODUCER_ROOT_PATH);
    treeCache.getListenable().addListener(producerChangeListener);
    treeCache.start();
}
Also used : TreeCache(org.apache.curator.framework.recipes.cache.TreeCache)

Example 9 with TreeCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project paascloud-master by paascloud.

the class MqBeanInitRunner method initConsumerListener.

private void initConsumerListener(CuratorFramework cf) throws Exception {
    TreeCache treeCache = new TreeCache(cf, GlobalConstant.ZK_REGISTRY_CONSUMER_ROOT_PATH);
    treeCache.getListenable().addListener(consumerChangeListener);
    treeCache.start();
}
Also used : TreeCache(org.apache.curator.framework.recipes.cache.TreeCache)

Example 10 with TreeCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project paascloud-master by paascloud.

the class ZookeeperRegistryCenter method addCacheData.

/**
 * Add cache data.
 *
 * @param cachePath the cache path
 */
@Override
public void addCacheData(final String cachePath) {
    TreeCache cache = new TreeCache(client, cachePath);
    try {
        cache.start();
    // CHECKSTYLE:OFF
    } catch (final Exception ex) {
        // CHECKSTYLE:ON
        RegExceptionHandler.handleException(ex);
    }
    caches.put(cachePath + "/", cache);
}
Also used : TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)26 ChildData (org.apache.curator.framework.recipes.cache.ChildData)6 TreeCacheListener (org.apache.curator.framework.recipes.cache.TreeCacheListener)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 CloseableExecutorService (org.apache.curator.utils.CloseableExecutorService)2 TreeCache (org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache)2 KeeperException (org.apache.zookeeper.KeeperException)2 OperationTimeoutException (org.apache.zookeeper.KeeperException.OperationTimeoutException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ShardingTreeCache (com.vip.saturn.job.sharding.entity.ShardingTreeCache)1 ShardingException (com.vip.saturn.job.sharding.exception.ShardingException)1 DataChangedEvent (io.shardingjdbc.orchestration.reg.listener.DataChangedEvent)1 StandardCharsets (java.nio.charset.StandardCharsets)1 EnumMap (java.util.EnumMap)1 Entry (java.util.Map.Entry)1 CountDownLatch (java.util.concurrent.CountDownLatch)1