可以监控某一路径下的子结点(所有子结节,不管有多少层子结点)变化。
比NodeCache方便的是,可以监听一群结点,而不用一个节点一个节点的去设置监听
1.run TreeListener 2.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(); } }
时间: 2024-12-28 12:15:04