Apache Curator Tree Cache Watcher
来源:互联网 发布:产品三维展示软件 编辑:程序博客网 时间:2024/06/05 09:03
可以监控某一路径下的子结点(所有子结节,不管有多少层子结点)变化。
比NodeCache方便的是,可以监听一群结点,而不用一个节点一个节点的去设置监听
1.run TreeListener2.run TLTest
package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;public class TLTest { public static void main(String[] args) throws Exception { TLClientCreate.main(null); Thread.sleep(2000); TLClientUpdate.main(null); Thread.sleep(2000); TLClientCreateSub.main(null); Thread.sleep(2000); TLClientUpdateSub.main(null); Thread.sleep(2000); TLClientDeleteSub.main(null); Thread.sleep(2000); TLClientDelete.main(null); }}package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.framework.recipes.cache.*;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.curator.utils.EnsurePath;import java.util.List;import java.util.Map;public class TreeListener { public static final String C_PATH = "/TestTree"; public static final String CHARSET = "UTF-8"; public static void main(String[] args) { try { new Thread(new Runnable() { @Override public void run() { try{ String zookeeperConnectionString = "127.0.0.1:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy); client.start(); final TreeCache treeCache = new TreeCache(client, C_PATH); treeCache.getListenable().addListener(new TreeCacheListener() { @Override public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { System.out.println("================== catch tree change =================="); if(event.getData() == null){ System.out.println("===init," + event.getType()); return; } if(event.getData().getData() == null){ System.out.println("===delete," + event.getType() + "," + event.getData().getPath()); }else{ System.out.println("===update or add," + event.getType() + "," + event.getData().getPath() + "," + new String(event.getData().getData(), TreeListener.CHARSET)); } } }); treeCache.start(); Thread.sleep(Integer.MAX_VALUE); client.close(); }catch (Exception e){ e.printStackTrace(); } } }).start(); }catch (Exception e){ e.printStackTrace(); } }}
package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.data.Stat;public class TLClientCreate { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { try { String zookeeperConnectionString = "127.0.0.1:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy); client.start(); Stat stat = client.checkExists().forPath(TreeListener.C_PATH); if (stat == null) { client.create().withMode(CreateMode.PERSISTENT).forPath(TreeListener.C_PATH, "-1".getBytes(NodeListener.CHARSET)); } client.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); }}
package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.data.Stat;public class TLClientCreateSub { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { try { String zookeeperConnectionString = "127.0.0.1:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy); client.start(); Stat stat = client.checkExists().forPath(TreeListener.C_PATH + "/sub1"); if (stat == null) { client.create().withMode(CreateMode.PERSISTENT).forPath(TreeListener.C_PATH + "/sub1", "-1".getBytes(NodeListener.CHARSET)); } client.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); }}
package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.data.Stat;public class TLClientDelete { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { try { String zookeeperConnectionString = "127.0.0.1:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy); client.start(); Stat stat = client.checkExists().forPath(TreeListener.C_PATH); if (stat != null) { client.delete().deletingChildrenIfNeeded().forPath(TreeListener.C_PATH); } client.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); }}
package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.data.Stat;public class TLClientDeleteSub { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { try { String zookeeperConnectionString = "127.0.0.1:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy); client.start(); Stat stat = client.checkExists().forPath(TreeListener.C_PATH + "/sub1"); if (stat != null) { client.delete().forPath(TreeListener.C_PATH + "/sub1"); } client.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); }}
package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.data.Stat;public class TLClientUpdate { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { try { String zookeeperConnectionString = "127.0.0.1:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy); client.start(); Stat stat = client.checkExists().forPath(TreeListener.C_PATH); if (stat != null) { client.setData().forPath(TreeListener.C_PATH, "64".getBytes(NodeListener.CHARSET)); } client.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); }}
package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.data.Stat;public class TLClientUpdateSub { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { try { String zookeeperConnectionString = "127.0.0.1:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy); client.start(); Stat stat = client.checkExists().forPath(TreeListener.C_PATH + "/sub1"); if (stat != null) { client.setData().forPath(TreeListener.C_PATH + "/sub1", "128".getBytes(TreeListener.CHARSET)); } client.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); }}
0 0
- Apache Curator Tree Cache Watcher
- Apache Curator Node Cache Watcher
- Apache Curator Path Cache Watcher
- Apache Curator Leader Election
- Apache Curator入门实战
- Apache Curator入门实战
- Apache Curator快速入门
- Apache Curator入门实战
- Apache Curator入门实战
- Apache Curator入门实战
- Apache Curator入门实战
- Apache Curator入门实战
- Apache Curator入门实战
- Apache Curator入门实战
- Apache curator-client详解
- Apache Curator入门实战
- 【第三方类库】Apache Curator
- Apache Curator入门实战
- ssm整合1(环境搭建)
- listview item点击和子控件的冲突
- Linux进阶:让效率翻倍的Bash技巧(一)
- wav文件格式分析详解(darkdragonking认为比较全的了)
- 编译live555共享库
- Apache Curator Tree Cache Watcher
- NSdata与NSString,Byte数组,UIImage的相互转换
- git简单用法
- 运算 double类型的数字相加不丢失精度
- 正则表达式用法学习
- 递归算法
- MAC下如何执行SHELL脚本
- 什么是回调函数
- Spring事务配置的五种方式