Apache curator Tree Cache Watcher

Source: Internet
Author: User

You can monitor the sub-nodes (all sub-nodes, regardless of the number of sub-nodes) in a path.
More convenient than Nodecache, can listen to a group of nodes, instead of a node to set up a node to monitor
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 (+);        Tlclientupdate.main (null);        Thread.Sleep (+);        Tlclientcreatesub.main (null);        Thread.Sleep (+);        Tlclientupdatesub.main (null);        Thread.Sleep (+);        Tlclientdeletesub.main (null);        Thread.Sleep (+);        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:2,181 ";                        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.prin                                TLN ("===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 Zooke                    eperconnectionstring = "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 Thr EAD (New Runnable () {@Override public void run () {try {String Zo                    okeeperconnectionstring = "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 Thr EAD (New Runnable () {@Override public void run () {try {String Zo                    okeeperconnectionstring = "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 Zooke                    eperconnectionstring = "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, "" ". GetBytes (Nodelisten Er.                    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", "GetBytes" (Treelistener.charset));                } client.close ();                } catch (Exception e) {e.printstacktrace ();    }}). Start (); }}


Apache curator Tree Cache Watcher

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.