Curator Tutorial (ii) Path cache listener Znode

Source: Internet
Author: User
Tags zookeeper

In practical application development, when a znode changes we need to be notified and do some follow-up processing, curator recipes provides the path Cache to help us easily implement watch Znode. Path Cache

The Path cache monitors changes to Znode sub-nodes, for example: Add,update,delete.

A Path Cache is used to watch a znode. Whenever a child is added, updated or removed, the Path Cache would change it state to contain the current set of children , the children ' s data and the children ' s state.

The cache must is started by calling start (). Call Close () is through with the cache. maven Dependency

<curator.version>2.11.1</curator.version>

<dependency>
    <groupId> org.apache.curator</groupid>
    <artifactId>curator-framework</artifactId>
    <version >${curator.version}</version>
</dependency>
<dependency>
    <groupId> org.apache.curator</groupid>
    <artifactId>curator-client</artifactId>
    <version> ${curator.version}</version>
    <exclusions>
        <exclusion>
            <groupId> com.google.guava</groupid>
            <artifactId>guava</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupid>org.apache.curator </groupId>
    <artifactId>curator-recipes</artifactId>
    <version>${ Curator.version}</version>
</dependency>

The sample code is as follows:

Package COM.BYTEBEATS.ZOOKEEPER.CURATOR.CH2;
Import Com.bytebeats.zookeeper.curator.CuratorUtils;
Import Org.apache.curator.framework.CuratorFramework;
Import Org.apache.curator.framework.recipes.cache.ChildData;
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 Java.nio.charset.Charset;
Import java.util.List;

Import Java.util.concurrent.TimeUnit; /** * ${description} * * @author Ricky Fung * @create 2016-12-10 15:37 * public class Curatorpathcachedemo {PR

    Ivate String Path = "/pandora";

    public static final Charset Charset = Charset.forname ("UTF-8");
        public static void Main (string[] args) {try{new Curatorpathcachedemo (). Start ();
        } catch (Exception e) {e.printstacktrace (); }} private void Start () throws Exception {CuratOrframework client = Curatorutils.getcuratorclient ();

            try{Client.start ();

            Final Pathchildrencache Pathchildrencache = new Pathchildrencache (client, path, true);
                Pathchildrencache.getlistenable (). AddListener (New Pathchildrencachelistener () {@Override public void Childevent (Curatorframework curatorframework, pathchildrencacheevent Ev
                    ENT) throws Exception {System.out.println ("======== Catch Children Change ======="); SYSTEM.OUT.PRINTLN ("Update event Type:" + event.gettype () + ", Path:" + Event.getdata (). g

                    Etpath () + ", Data:" + New String (Event.getdata (). GetData (), CHARSET));
                    list<childdata> childdatalist = Pathchildrencache.getcurrentdata (); if (childdatalist! = null && childdatalist.size () > 0) {System.out.println ("path alL children list: ");  for (Childdata childdata:childdatalist) {System.out.println ("path:" + childdata.getpath () +
                        "," + New String (Childdata.getdata (), CHARSET));

            }
                    }
                }
            });  Pathchildrencache.start ();

            Must call Start ();

            TimeUnit.MINUTES.sleep (5);

        Pathchildrencache.close ();
        }finally {if (client!=null) client.close ();
 }
    }
}

Click here to download the full code: Https://github.com/TiFG/zookeeper-samples Reference

Path cache:http://curator.apache.org/curator-recipes/path-cache.html

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.