diff --git a/net/src/test/java/com/zfoo/net/zookeeper/curator/cache/CuratorCacheTest.java b/net/src/test/java/com/zfoo/net/zookeeper/curator/cache/CuratorCacheTest.java new file mode 100644 index 00000000..f414f864 --- /dev/null +++ b/net/src/test/java/com/zfoo/net/zookeeper/curator/cache/CuratorCacheTest.java @@ -0,0 +1,50 @@ +package com.zfoo.net.zookeeper.curator.cache; + + +import com.zfoo.util.ThreadUtils; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.CuratorCache; +import org.apache.curator.framework.recipes.cache.CuratorCacheListener; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.junit.Ignore; +import org.junit.Test; + +/** + * @author jaysunxiao + * @version 1.0 + * @since 2019-08-25 09:43 + */ +@Ignore +public class CuratorCacheTest { + + private static CuratorFramework curator = CuratorFrameworkFactory.builder() + .connectString("localhost:2181") + .sessionTimeoutMs(5000) + .retryPolicy(new ExponentialBackoffRetry(1000, 3)) +// .retryPolicy(new RetryNTimes(1, 1000)) + .build(); + + @Test + public void test() { + curator.start(); + var curatorCache = CuratorCache.builder(curator, "/test") + .withExceptionHandler(e -> e.printStackTrace()) + .build(); + + curatorCache.start(); + + //节点变化的监听器 + curatorCache.listenable().addListener(new CuratorCacheListener() { + @Override + public void event(Type type, ChildData oldData, ChildData data) { + System.out.println("pathCache ------ Type:" + type + ","); + System.out.println(data); + } + }); + + ThreadUtils.sleep(Long.MAX_VALUE); + } + +} diff --git a/net/src/test/java/com/zfoo/net/zookeeper/curator/cache/PathCacheTest.java b/net/src/test/java/com/zfoo/net/zookeeper/curator/cache/PathCacheTest.java deleted file mode 100644 index 44d802e2..00000000 --- a/net/src/test/java/com/zfoo/net/zookeeper/curator/cache/PathCacheTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.zfoo.net.zookeeper.curator.cache; - - -import com.zfoo.util.ThreadUtils; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.framework.recipes.cache.PathChildrenCache; -import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; -import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; -import org.apache.curator.retry.ExponentialBackoffRetry; -import org.junit.Ignore; -import org.junit.Test; - -/** - * @author jaysunxiao - * @version 1.0 - * @since 2019-08-25 09:43 - */ -@Ignore -public class PathCacheTest { - - private static CuratorFramework curator = CuratorFrameworkFactory.builder() - .connectString("localhost:2181") - .sessionTimeoutMs(5000) - .retryPolicy(new ExponentialBackoffRetry(1000, 3)) -// .retryPolicy(new RetryNTimes(1, 1000)) - .build(); - - @Test - public void test() { - curator.start(); - - PathChildrenCache pathCache = new PathChildrenCache(curator, "/test", true); - try { - pathCache.start(); - } catch (Exception e) { - e.printStackTrace(); - } - - - //节点变化的监听器 - pathCache.getListenable().addListener(new PathChildrenCacheListener() { - @Override - public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { - System.out.println("pathCache ------ Type:" + event.getType() + ","); - System.out.println(event.getData().getPath()); - } - }); - - while (true) { - ThreadUtils.sleep(3000); - try { - pathCache.getCurrentData().stream() - .forEach(it -> System.out.println(it.getPath() + "->" + it.getData())); - } catch (Exception e) { - e.printStackTrace(); - } - - } - - } - -} diff --git a/net/src/test/java/com/zfoo/net/zookeeper/curator/cache/TreeCacheTest.java b/net/src/test/java/com/zfoo/net/zookeeper/curator/cache/TreeCacheTest.java deleted file mode 100644 index 33761948..00000000 --- a/net/src/test/java/com/zfoo/net/zookeeper/curator/cache/TreeCacheTest.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.zfoo.net.zookeeper.curator.cache; - - -import com.zfoo.util.ThreadUtils; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.framework.api.UnhandledErrorListener; -import org.apache.curator.framework.recipes.cache.TreeCache; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheListener; -import org.apache.curator.retry.ExponentialBackoffRetry; -import org.junit.Ignore; -import org.junit.Test; - -/** - * @author jaysunxiao - * @version 1.0 - * @since 2019-08-25 09:43 - */ -@Ignore -public class TreeCacheTest { - - private static CuratorFramework curator = CuratorFrameworkFactory.builder() - .connectString("localhost:2181") - .sessionTimeoutMs(5000) - .retryPolicy(new ExponentialBackoffRetry(1000, 3)) -// .retryPolicy(new RetryNTimes(1, 1000)) - .build(); - - @Test - public void test() { - curator.start(); - - TreeCache treeCache = new TreeCache(curator, "/test"); - try { - treeCache.start(); - } catch (Exception e) { - e.printStackTrace(); - } - //添加错误监听器 - treeCache.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() { - public void unhandledError(String s, Throwable throwable) { - System.out.println(".错误原因:" + throwable.getMessage() + "\n==============\n"); - } - }); - - //节点变化的监听器 - treeCache.getListenable().addListener(new TreeCacheListener() { - public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception { - System.out.println("treeCache ------ Type:" + treeCacheEvent.getType() + ","); - System.out.println(treeCacheEvent.getData().getPath()); - - } - }); - - while (true) { - ThreadUtils.sleep(3000); - try { - System.out.println("-------->" + treeCache.getCurrentData("/test/a")); - - treeCache.getCurrentChildren("/test").entrySet().stream() - .forEach(it -> System.out.println(it.getKey() + "->" + it.getValue())); - } catch (Exception e) { - e.printStackTrace(); - } - - } - - } - -}