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