ZooKeeper (3.4.5) uses Curator (2.7.0) to listen for events and zookeepercurator
The native API of ZooKeeper supports event listening by registering Watcher, but Watcher notifications are one-time. Therefore, it is cumbersome to register Watcher repeatedly during development. Curator introduces Cache to listen to events on the ZooKeeper server. The Cache encapsulates the ZooKeeper event listening and can automatically process repeated registration listening. This simplifies the tedious development process of ZooKeeper native APIs.
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 listener * @ author huey * @ version 1.0 * @ created 2015-3-2 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (1000, 3 )). build (); client. start (); client. create (). creatingParentsIfNeeded (). forPath ("/zk-huey/cnode", "hello ". getBytes ();/*** if this parameter is input when the listener is registered, when the event is triggered, the logic is handled by the thread pool */ExecutorService pool = Executors. newFixedThreadPool (2);/*** monitors changes of data nodes */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);/*** listen to subnode changes */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 Exception {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 ();}}