ZooKeeper (3.4.5) using curator to listen for events

Source: Internet
Author: User

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

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.