ZooKeeper (3.4.5) uses Curator (2.7.0) to listen for events and zookeepercurator

Source: Internet
Author: User

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 ();}}

 

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.