Can only listen to the add,delete,update of a path itself
1.run Nodelistener
2.run NLTest
Package Com.collonn.javautilmvn.zookeeper.curator.nodecache;public class NLTest {public static void main (string[ ] args) throws Exception { nlclientcreate.main (null); Thread.Sleep (+ 2); Nlclientupdate.main (null); Thread.Sleep (+ 2); Nlclientdelete.main (null);} }
Package Com.collonn.javautilmvn.zookeeper.curator.nodecache;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;public class Nodelistener {public static final String C_path = "/testnode"; 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:2,181 "; Retrypolicy retrypolicy = new Exponentialbackoffretry (1000, 3); Curatorframework client = curatorframeworkfactory.newclient (zookeeperconnectionstring, Retrypolicy); Client.start (); Final Nodecache Nodecache = new Nodecache (client, C_path); Nodecache.getlistenable (). AddListener (New Nodecachelistener () {@Override public void NodeChanged () throws Exception {System.out.println ("================== C Atch node Data Change ================== "); Childdata childdata = Nodecache.getcurrentdata (); if (Childdata = = null) {System.out.println ("===delete, path=" + C_path + ", childdata=" + Childdata); }else{System.out.println ("===update or Add, path=" + C_path + ", childdata=" + new STR ING (Childdata.getdata (), CHARSET)); } } }); Nodecache.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.nodecache;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;import Java.util.random;public class Nlclientcreate {public static void main (string[] A RGS) {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 (Nodelistener.c_path); if (stat = = null) { Client.create (). Withmode (createmode.persistent). Forpath (Nodelistener.c_path, "1". GetBytes (Nodelistener.charset )); } client.close (); } catch (Exception e) {e.printstacktrace (); }}). Start (); }}
Package Com.collonn.javautilmvn.zookeeper.curator.nodecache;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;import Java.util.random;public class Nlclientupdate {public static void main (string[] args) {new Thread (new Runnable ( {@Override public void run () {try {String zookeeperconnection String = "127.0.0.1:2181"; Retrypolicy retrypolicy = new Exponentialbackoffretry (1000, 3); Curatorframework client = curatorframeworkfactory.newclient (zookeeperconnectionstring, Retrypolicy); Client.start (); Stat stat = client.checkexists (). Forpath (Nodelistener.c_path); if (stat! = null) {Client.setdata (). Forpath (NodelistenEr. C_path, "GetBytes" (Nodelistener.charset)); } client.close (); } catch (Exception e) {e.printstacktrace (); }}). Start (); }}
Package Com.collonn.javautilmvn.zookeeper.curator.nodecache;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;import Java.util.random;public class Nlclientdelete {public static void main (string[] A RGS) {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 (Nodelistener.c_path); if (stat! = null) { Client.delete (). Forpath (Nodelistener.c_path); } client.close (); } catch (Exception e) {e.printstacktrace (); }}). Start (); }}
Apache curator Node Cache Watcher