ZooKeeper Watch Java API Analysis GetChildren

Source: Internet
Author: User
Tags zookeeper

Watch is a very important mechanism in zookeeper, which can monitor the change of nodes in zookeeper and inform the client. Below, we use code as an example to analyze how watch is implemented in zookeeper. Zookeeper in a total of three ways to achieve watch, respectively, GetData, exists and GetChildren, today we first look at the GetChildren () method:

3, GetChildren

Import Java.io.ioexception;import Org.apache.zookeeper.createmode;import org.apache.zookeeper.KeeperException; Import Org.apache.zookeeper.watchedevent;import Org.apache.zookeeper.watcher;import Org.apache.zookeeper.zookeeper;import Org.apache.zookeeper.zoodefs.ids;import Org.apache.zookeeper.data.Stat; public class Testzookeeperwatcher {public static void main (string[] args) {ZooKeeper ZK = null;try {System.out.println (".. ."); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("Start connection Zookeeper ...");//Create a connection to the zookeeper server zkstring address = "192.168.1.226:2181"; int Sessiontimeout = 3000;zk = new ZooKeeper (address, sessiontimeout, new Watcher () {//monitor all triggered events public void process (Watchede Vent event) {if (event.gettype () = = NULL | | ". Equals (Event.gettype ())) {return;} System.out.println ("has triggered" + event.gettype () + "Event! ");}}); SYSTEM.OUT.PRINTLN ("Zookeeper Connection created successfully! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); SYstem.out.println ("..."); System.out.println ("...");//create root node//path to/tmp_root_path//node content as string "I am the root directory/tmp_root_path"// The creation mode is CREATEMODE.PERSISTENTSYSTEM.OUT.PRINTLN ("Start creating root node/tmp_root_path ..."), Zk.create ("/tmp_root_path", "I am the root directory/ Tmp_root_path ". GetBytes (), Ids.open_acl_unsafe, createmode.persistent); SYSTEM.OUT.PRINTLN ("Root directory node/tmp_root_path created successfully! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");//Get subdirectory node list System.out.println ("Start getting root directory/tmp_root_path node's subdirectory node column ..."); System.out.println (Zk.getchildren ("/tmp_root_path", true)); SYSTEM.OUT.PRINTLN ("root directory/tmp_root_path node's subdirectory node column gets successful! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");//Create First subdirectory node//path to/tmp_root_path/childpath1//node content as string "I am the first subdirectory/tmp_root_path/ ChildPath1 "//Create Mode is CREATEMODE.PERSISTENTSYSTEM.OUT.PRINTLN (" Start creating first subdirectory node/tmp_root_path/childpath1 ... "); Zk.create ("/tmP_root_path/childpath1 "," I am the first subdirectory of/tmp_root_path/childpath1 ". GetBytes (), Ids.open_acl_unsafe, Createmode.persistent); System.out.println ("First subdirectory node/tmp_root_path/childpath1 created successfully! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");//Create a second subdirectory node//path to/tmp_root_path/childpath2//node content as string "I am the second subdirectory/tmp_root_path/ ChildPath2 "//Create Mode is CREATEMODE.PERSISTENTSYSTEM.OUT.PRINTLN (" Start creating second subdirectory node/tmp_root_path/childpath2 ... "); Zk.create ("/tmp_root_path/childpath2", "I am the second subdirectory/tmp_root_path/childpath2". GetBytes (), Ids.open_acl_unsafe, Createmode.persistent); System.out.println ("Second subdirectory node/tmp_root_path/childpath2 created successfully! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");//Modify the first subdirectory node/tmp_root_path/childpath1 data System.out.println ("Start modifying the first subdirectory node/tmp_root_ Path/childpath1 data ... "); Zk.setdata ("/tmp_root_path/childpath1 "," I am the first subdirectory after modifying the data/tmp_root_patH/childpath1 ". GetBytes (),-1); System.out.println ("Modify the first subdirectory node/TMP_ROOT_PATH/CHILDPATH1 data successfully! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");//Modify the second subdirectory node/tmp_root_path/childpath2 data System.out.println ("Start modifying the second subdirectory node/tmp_root_ Path/childpath2 data ... "); Zk.setdata ("/tmp_root_path/childpath2 "," I am the second subdirectory after modifying the data/tmp_root_path/childpath2 ". GetBytes (),-1); System.out.println ("Modify the second subdirectory node/TMP_ROOT_PATH/CHILDPATH2 data successfully! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");//Modify the root node data System.out.println ("Start modifying root node/tmp_root_path data ..."); Zk.setdata ("/tmp_root_ Path "," I am the root directory after modifying the data/tmp_root_path ". GetBytes (),-1); System.out.println ("Modify root node/tmp_root_path data succeeded! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");//delete the first subdirectory node System.out.println ("Start deletingA subdirectory node/tmp_root_path/childpath1 ... "); Zk.delete ("/tmp_root_path/childpath1 ",-1); System.out.println ("First subdirectory node/tmp_root_path/childpath1 deleted successfully! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");//delete the second subdirectory node System.out.println ("Start removing the second subdirectory node/tmp_root_path/childpath2 ..."); zk.delete ("/tmp_root_path/childpath2",-1); System.out.println ("Second subdirectory node/tmp_root_path/childpath2 deleted successfully! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");//delete root node System.out.println ("Start removing root node/tmp_root_path ...") zk.delete ("/tmp_root_path" ,-1); SYSTEM.OUT.PRINTLN ("Root directory node/tmp_root_path deleted successfully! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); catch (IOException | keeperexception | Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally {//closes the connection if (ZK! = null) {try {zk.close (); System.out.println ("Release Zookeeper connection succeeded! ");} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}}
The results of the implementation are as follows:

............ Start Connecting zookeeper ... Zookeeper Connection created successfully! The None event has been triggered! ............ Start creating root directory Node/tmp_root_path ... Root node/tmp_root_path created successfully! ............ Start getting the root directory of the/tmp_root_path node in the Subdirectories node column ... [] The subdirectory node of the root directory/tmp_root_path node column gets successful! ............ Start creating the first subdirectory node/tmp_root_path/childpath1 ... The first subdirectory node/tmp_root_path/childpath1 created successfully! The Nodechildrenchanged event has been triggered! ............ Start creating a second subdirectory node/tmp_root_path/childpath2 ... The second subdirectory node/tmp_root_path/childpath2 created successfully! ............ Start modifying the first subdirectory node/tmp_root_path/childpath1 data ... Modify the first subdirectory node/TMP_ROOT_PATH/CHILDPATH1 data successfully! ............ Start modifying the second subdirectory node/tmp_root_path/childpath2 data ... Modify the second subdirectory node/TMP_ROOT_PATH/CHILDPATH2 data successfully! ............ Start modifying root node/tmp_root_path data ... Modify root node/tmp_root_path data success! ............ Start Delete First subdirectory node/tmp_root_path/childpath1 ... The first subdirectory node/tmp_root_path/childpath1 deleted successfully! ............ Start removing the second subdirectory node/tmp_root_path/childpath2 ... The second subdirectory node/tmp_root_path/childpath2 deleted successfully! ............ Start Delete root node/tmp_root_path ... Root node/tmp_root_path deleted successfully! ............ Release Zookeeper connection Successful!
And when we get the subdirectory node List GetChildren () method call, and then call the SetData () method that modifies the root node data, add the code and the results are as follows:

Modify the root node data System.out.println ("Start modifying root node/tmp_root_path data ..."), Zk.setdata ("/tmp_root_path", "I am the root directory after the data is modified/TMP_ Root_path ". GetBytes (),-1); System.out.println ("Modify root node/tmp_root_path data succeeded! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");
............ Start Connecting zookeeper ... Zookeeper Connection created successfully! The None event has been triggered! ............ Start creating root directory Node/tmp_root_path ... Root node/tmp_root_path created successfully! ............ Start getting the root directory of the/tmp_root_path node in the Subdirectories node column ... [] The subdirectory node of the root directory/tmp_root_path node column gets successful! ............ Start modifying root node/tmp_root_path data ... Modify root node/tmp_root_path data success! ............ Start creating the first subdirectory node/tmp_root_path/childpath1 ... The Nodechildrenchanged event has been triggered! The first subdirectory node/tmp_root_path/childpath1 created successfully! ............ Start creating a second subdirectory node/tmp_root_path/childpath2 ... The second subdirectory node/tmp_root_path/childpath2 created successfully! ............ Start modifying the first subdirectory node/tmp_root_path/childpath1 data ... Modify the first subdirectory node/TMP_ROOT_PATH/CHILDPATH1 data successfully! ............ Start modifying the second subdirectory node/tmp_root_path/childpath2 data ... Modify the second subdirectory node/TMP_ROOT_PATH/CHILDPATH2 data successfully! ............ Start modifying root node/tmp_root_path data ... Modify root node/tmp_root_path data success! ............ Start Delete First subdirectory node/tmp_root_path/childpath1 ... The first subdirectory node/tmp_root_path/childpath1 deleted successfully! ............ Start removing the second subdirectory node/tmp_root_path/childpath2 ... The second subdirectory node/tmp_root_path/childpath2 deleted successfully! ............ Start Delete root node/tmp_root_path ... Root node/tmp_root_path deleted successfully! ............ Release Zookeeper connection Successful!
It still only monitors the change of child nodes under the root directory, and it triggers the nodechildrenchanged event! What happens when we create the first child node and then create a child node of it and get the subdirectory node column of the root/tmp_root_path node before it is created? The added code and execution results are as follows:

Gets the subdirectory node list System.out.println ("Start getting root directory/tmp_root_path node of subdirectories node column ..."); System.out.println (Zk.getchildren ("/tmp_root_path", true)); SYSTEM.OUT.PRINTLN ("root directory/tmp_root_path node's subdirectory node column gets successful! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");//Create a child node of the first subdirectory node//path to/tmp_root_path/childpath1/childpath1//node content as string "I am the first subdirectory/tmp_ Root_path/childpath1/childpath1 "//Create Mode is CREATEMODE.PERSISTENTSYSTEM.OUT.PRINTLN (" Start creating child nodes of the first subdirectory node/tmp_root_path /childpath1/childpath1 ... "); Zk.create ("/tmp_root_path/childpath1/childpath1 "," I am the child of the first subdirectory/tmp_root_path/ Childpath1/childpath1 ". GetBytes (), ids.open_acl_unsafe,createmode.persistent); SYSTEM.OUT.PRINTLN ("Child node of the first subdirectory node/tmp_root_path/childpath1/childpath1 created successfully!") "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");
............ Start Connecting zookeeper ... Zookeeper Connection created successfully! The None event has been triggered! ............ Start creating root directory Node/tmp_root_path ... Root node/tmp_root_path created successfully! ............ Start getting the root directory of the/tmp_root_path node in the Subdirectories node column ... [] The subdirectory node of the root directory/tmp_root_path node column gets successful! ............ Start modifying root node/tmp_root_path data ... Modify root node/tmp_root_path data success! ............ Start creating the first subdirectory node/tmp_root_path/childpath1 ... The first subdirectory node/tmp_root_path/childpath1 created successfully! The Nodechildrenchanged event has been triggered! ............ Start getting the root directory of the/tmp_root_path node in the Subdirectories node column ... [ChildPath1] Root directory/tmp_root_path node sub-directory node column gets successful! ............ Start creating a child node of the first subdirectory node/tmp_root_path/childpath1/childpath1 ... Child nodes of the first subdirectory node/tmp_root_path/childpath1/childpath1 created successfully! ............ Start creating a second subdirectory node/tmp_root_path/childpath2 ... The Nodechildrenchanged event has been triggered! The second subdirectory node/tmp_root_path/childpath2 created successfully! ............ Start modifying the first subdirectory node/tmp_root_path/childpath1 data ... Modify the first subdirectory node/TMP_ROOT_PATH/CHILDPATH1 data successfully! ............ Start modifying the second subdirectory node/tmp_root_path/childpath2 data ... Modify the second subdirectory node/TMP_ROOT_PATH/CHILDPATH2 data successfully! ............ Start modifying root node/tmp_root_path data ... Modify root node/tmp_root_path data success! ............ Start deleting the first subdirectory node/tmp_root_path/chiLdpath1...org.apache.zookeeper.keeperexception$notemptyexception:keepererrorcode = Directory not empty for/tmp_root _path/childpath1at org.apache.zookeeper.KeeperException.create (keeperexception.java:125) at Org.apache.zookeeper.KeeperException.create (keeperexception.java:51) at Org.apache.zookeeper.ZooKeeper.delete ( zookeeper.java:873) at Com.jngreen.bgm.scheduler.TestZooKeeperWatcher.main (testzookeeperwatcher.java:197) Release Zookeeper connection Successful!
Or only the nodes under the direct sub-directory are monitored, and the nodechildrenchanged event is triggered when the second node is added, and does not leapfrog monitoring! Of course, the org.apache.zookeeper.keeperexception$notemptyexception exception occurs because we delete the first node, because it has a node, so it will be an error!

Another interesting thing is, what happens when we get the subdirectory node list of the root/tmp_root_path node before we modify the first child node data, and call the GetChildren () method? The code and execution results at the add-on are as follows:

//get subdirectory node list System.ou T.println ("Start get root directory/tmp_root_path node subdirectory node column ..."); System.out.println (Zk.getchildren ("/tmp_root_path", true)); SYSTEM.OUT.PRINTLN ("root directory/tmp_root_path node's subdirectory node column gets successful! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("...");//Modify the first subdirectory node/tmp_root_path/childpath1 data System.out.println ("Start modifying the first subdirectory node/tmp_root_ Path/childpath1 data ... "); Zk.setdata ("/tmp_root_path/childpath1 "," I am the first subdirectory/tmp_root_path/childpath1 after the data has been modified. ") GetBytes (),-1); System.out.println ("Modify the first subdirectory node/TMP_ROOT_PATH/CHILDPATH1 data successfully! "); Thread.CurrentThread (). Sleep (1000l); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); System.out.println ("..."); 
............ Start Connecting zookeeper ... Zookeeper Connection created successfully! The None event has been triggered! ............ Start creating root directory Node/tmp_root_path ... Root node/tmp_root_path created successfully! ............ Start getting the root directory of the/tmp_root_path node in the Subdirectories node column ... [] The subdirectory node of the root directory/tmp_root_path node column gets successful! ............ Start modifying root node/tmp_root_path data ... Modify root node/tmp_root_path data success! ............ Start creating the first subdirectory node/tmp_root_path/childpath1 ... The Nodechildrenchanged event has been triggered! The first subdirectory node/tmp_root_path/childpath1 created successfully! ............ Start creating a second subdirectory node/tmp_root_path/childpath2 ... The second subdirectory node/tmp_root_path/childpath2 created successfully! ............ Start getting the root directory of the/tmp_root_path node in the Subdirectories node column ... [ChildPath2, ChildPath1] root directory/tmp_root_path node sub-directory node column gets successful! ............ Start modifying the first subdirectory node/tmp_root_path/childpath1 data ... Modify the first subdirectory node/TMP_ROOT_PATH/CHILDPATH1 data successfully! ............ Start modifying the second subdirectory node/tmp_root_path/childpath2 data ... Modify the second subdirectory node/TMP_ROOT_PATH/CHILDPATH2 data successfully! ............ Start modifying root node/tmp_root_path data ... Modify root node/tmp_root_path data success! ............ Start Delete First subdirectory node/tmp_root_path/childpath1 ... The Nodechildrenchanged event has been triggered! The first subdirectory node/tmp_root_path/childpath1 deleted successfully! ............ Start removing the second subdirectory node/tmp_root_path/childpath2 ... Second sub-directory node/tmp_root_path/childpath2 Delete Success! ............ Start Delete root node/tmp_root_path ... Root node/tmp_root_path deleted successfully! ............ Release Zookeeper connection Successful!
is also only monitored once, but, however, it only monitors the root sub-nodes of the increase and decrease, as to whether the data changes, will not be monitored at all! This is why the first child node is modified without triggering, and the nodechildrenchanged event is triggered when the first child node is deleted!


Conclusion:

The GetChildren () method only monitors the changes of the direct sub-directories of the corresponding nodes, but only monitors the increase and decrease of the direct child nodes, and does not monitor the data changes! To change the corresponding node each time the changes are monitored, then each time you have to call the GetChildren () method to get through the node's child node list!










ZooKeeper Watch Java API Analysis GetChildren

Related Article

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.