Apache curator Node Cache Watcher

Source: Internet
Author: User

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

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.