Search in sources :

Example 31 with TreeCacheEvent

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent 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 32 with TreeCacheEvent

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent 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 33 with TreeCacheEvent

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent in project elastic-job by dangdangdotcom.

the class CloudJobConfigurationListenerTest method assertChildEventWhenStateIsUpdateAndIsConfigPathAndTransientJob.

@Test
public void assertChildEventWhenStateIsUpdateAndIsConfigPathAndTransientJob() throws Exception {
    cloudJobConfigurationListener.childEvent(null, new TreeCacheEvent(TreeCacheEvent.Type.NODE_UPDATED, new ChildData("/config/job/test_job", null, CloudJsonConstants.getJobJson().getBytes())));
    verify(readyService, times(0)).remove(Collections.singletonList("test_job"));
    verify(producerManager).reschedule(Matchers.<CloudJobConfiguration>any());
}
Also used : ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) Test(org.junit.Test)

Example 34 with TreeCacheEvent

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent in project elastic-job by dangdangdotcom.

the class CloudJobConfigurationListenerTest method assertChildEventWhenIsNotConfigPath.

@Test
public void assertChildEventWhenIsNotConfigPath() throws Exception {
    cloudJobConfigurationListener.childEvent(null, new TreeCacheEvent(TreeCacheEvent.Type.NODE_UPDATED, new ChildData("/other/test_job", null, "".getBytes())));
    verify(producerManager, times(0)).schedule(Matchers.<CloudJobConfiguration>any());
    verify(producerManager, times(0)).reschedule(Matchers.<CloudJobConfiguration>any());
    verify(producerManager, times(0)).unschedule(Matchers.<String>any());
}
Also used : ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) Test(org.junit.Test)

Example 35 with TreeCacheEvent

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent in project elastic-job by dangdangdotcom.

the class CloudJobConfigurationListenerTest method assertChildEventWhenStateIsAddAndIsConfigPathAndInvalidData.

@Test
public void assertChildEventWhenStateIsAddAndIsConfigPathAndInvalidData() throws Exception {
    cloudJobConfigurationListener.childEvent(null, new TreeCacheEvent(TreeCacheEvent.Type.NODE_ADDED, new ChildData("/config/job/test_job", null, "".getBytes())));
    verify(producerManager, times(0)).schedule(Matchers.<CloudJobConfiguration>any());
    verify(producerManager, times(0)).reschedule(Matchers.<CloudJobConfiguration>any());
    verify(producerManager, times(0)).unschedule(Matchers.<String>any());
}
Also used : ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) Test(org.junit.Test)

Aggregations

TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)74 ChildData (org.apache.curator.framework.recipes.cache.ChildData)71 Test (org.junit.Test)69 LeaderElectionJobListener (com.dangdang.ddframe.job.lite.internal.election.ElectionListenerManager.LeaderElectionJobListener)7 SimpleJobConfiguration (com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration)6 JobPausedStatusJobListener (com.dangdang.ddframe.job.lite.internal.server.JobOperationListenerManager.JobPausedStatusJobListener)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 TreeCacheListener (org.apache.curator.framework.recipes.cache.TreeCacheListener)5 TestSimpleJob (com.dangdang.ddframe.job.lite.fixture.TestSimpleJob)4 CronSettingAndJobEventChangedJobListener (com.dangdang.ddframe.job.lite.internal.config.ConfigurationListenerManager.CronSettingAndJobEventChangedJobListener)4 MonitorExecutionChangedJobListener (com.dangdang.ddframe.job.lite.internal.execution.ExecutionListenerManager.MonitorExecutionChangedJobListener)4 TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TimeDiffIntolerableException (com.vip.saturn.job.exception.TimeDiffIntolerableException)1 SaturnThreadFactory (com.vip.saturn.job.threads.SaturnThreadFactory)1 DataChangedEvent (io.shardingjdbc.orchestration.reg.listener.DataChangedEvent)1 StandardCharsets (java.nio.charset.StandardCharsets)1