Reprint: http://www.mamicode.com/info-detail-494364.html
Label:
Zookeeper native API support through the registration of Watcher for event monitoring, but watcher notification is a one-time, so the development process needs to be repeatedly registered watcher, more cumbersome. Curator introduced the cache to listen for events on the zookeeper server. The cache encapsulates zookeeper event monitoring, automates the process of repeatedly registering monitoring, and simplifies the tedious development of zookeeper native APIs.
A simple example:
Package Com.huey.dream.demo;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors; Import Org.apache.curator.framework.curatorframework;import Org.apache.curator.framework.CuratorFrameworkFactory ; Import Org.apache.curator.framework.recipes.cache.nodecache;import Org.apache.curator.framework.recipes.cache.nodecachelistener;import Org.apache.curator.framework.recipes.cache.pathchildrencache;import Org.apache.curator.framework.recipes.cache.pathchildrencacheevent;import Org.apache.curator.framework.recipes.cache.pathchildrencachelistener;import Org.apache.curator.framework.recipes.cache.pathchildrencache.startmode;import org.apache.curator.retry.exponentialbackoffretry;/** * Curator Event monitoring * @author Huey * @version 1.0 * @created 2015-3-2 */p Ublic class Carutordemo {public static void main (string[] args) throws Exception {curatorframework client = Cu Ratorframeworkfactory.builder (). ConnectString ("192.168.1.109:2181"). SessiontimeOutms (CONNECTIONTIMEOUTMS). Retrypolicy (New Exponentialbackoffretry (1000, 3)) . build (); Client.start (); Client.create (). creatingparentsifneeded (). Forpath ("/zk-huey/cnode", "Hello". GetBytes ()); /** * When registering the listener, if this parameter is passed in, the logic is processed by the thread pool when the event fires */executorservice pool = Executors.newfixedthreadpo OL (2); /** * Monitor Data node changes */FINAL Nodecache Nodecache = new Nodecache (client, "/zk-huey/cnode", false); Nodecache.start (TRUE); Nodecache.getlistenable (). AddListener (New Nodecachelistener () {@Override public void NodeChanged () throws Exception {System.out.println ("Node data is changed, new data:" + New String (Nodecache.getcurrentdata (). GetData ())); }}, pool); /** * Monitor the change of the sub-nodeStatus */FINAL Pathchildrencache Childrencache = new Pathchildrencache (client, "/zk-huey", true); Childrencache.start (startmode.post_initialized_event); Childrencache.getlistenable (). AddListener (New Pathchildrencachelistener () {@Override public void Childevent (curatorframework client, pathchildrencacheevent event) throws Exceptio n {switch (Event.gettype ()) {case child_added: System.out.println ("child_added:" + event.getdata (). GetPath ()); Break Case CHILD_REMOVED:System.out.println ("child_removed:" + event.getdata (). GetPath ()); Break Case CHILD_UPDATED:System.out.println ("child_updated:" + event.getdata (). GetPath ()); Break Default:break; }}}, pool); Client.setdata (). Forpath ("/zk-huey/cnode", "World". GetBytes ()); Thread.Sleep (10 * 1000); Pool.shutdown (); Client.close (); }}
ZooKeeper (3.4.5) using curator to listen for events