Zookeeper native support through the registration of watcher to do event monitoring, but its use is not particularly convenient, the need for developers to repeatedly register their own watcher, more cumbersome. Curator introduces the cache to monitor the Zookeeper server events. Cache is the packaging of event listening in curator, and its monitoring of events can be approximated as a comparison between a local cached view and a remote zookeeper view. At the same time, curator can automatically deal with repeated registration monitoring for developers, thus greatly simplifying the tedious process of native API development. The cache is divided into two types of listening type: node listening and child node listening. Nodecache
Nodecache is used to listen for changes to the specified zookeeper data node itself, constructed with the following two: public Nodecache (curatorframework client, String path); Public Nodecache (curatorframework client, String path, Boolean dataiscompressed);
The Nodecache constructor parameter description is shown in the following table.
Name of parameter |
Description |
Client |
Curator Client Instance |
Path |
node path for data nodes |
Dataiscompressed |
Whether to compress data |
At the same time, Nodecache defines the callback interface Nodecachelistener for event handling. Nodecachelistener Callback Interface Definition
Public interface Nodecachelistener {
Called when a change has occurred
public void NodeChanged () throws Exception;
}
When the content of the data node changes, the method is recalled. Here's a real example to see how to use Nodecache in your code. Nodecache Use Example
public class Nodecache_sample {
static String Path = "/zk-book/nodecache";
static Curatorframework client = Curatorframeworkfactory.builder (). ConnectString ("domain1.book.zookeeper:2181"). SESSIONTIMEOUTMS (5000). Retrypolicy (New Exponentialbackoffretry (1000, 3)). Build ();
public static void Main (string[] args) throws Exception {
Client.start ();
Client.create (). creatingparentsifneeded (). Withmode (createmode.ephemeral). Forpath (Path, "Init". GetBytes ());
Final Nodecache cache = new Nodecache (client, path, false);
Cache.start (TRUE);
Cache.getlistenable (). AddListener (New Nodecachelistener () {
@Override
public void NodeChanged () throws Exception {
System.out.println ("Node Data Update, new data:" + New String (Cache.getcurrentdata (). GetData ()));
}
});
Client.setdata (). Forpath (Path, "U". GetBytes ());
Thread.Sleep (1000);
Client.delete (). deletingchildrenifneeded (). Forpath (path);
Thread.Sleep (Integer.max_value);
}
}
In the example program above, you first construct a Nodecache instance and call the Start method, which has a Boolean parameter, the default is False, and if set to true, Then Nodecache will immediately read the corresponding node data from the zookeeper on the first boot and save it in the cache.
Nodecache can be used not only to monitor the content changes of data nodes, but also to listen for the existence of specified nodes. If the original node does not exist, then the cache will trigger the Nodecachelistener after the node is created. However, if the data node is deleted, then curator cannot trigger the Nodecachelistener. Pathchildrencache
Pathchildrencache is used to listen for changes to child nodes of a specified zookeeper data node.
Pathchildrencache has the following definitions of construction methods: Public Pathchildrencache (curatorframework client, String path, Boolean cachedata); public Pathchildrencache (curatorframework client, String path, Boolean cachedata, Threadfactory threadfactory ); Public Pathchildrencache (curatorframework client, String path, Boolean cachedata, Boolean datalscompressed, Threadfactory threadfactory); Public Pathchildrencache (curatorframework client, String path, Boolean cachedata, Boolean datalscompressed, Final Executorservice executorserivice); Public Pathchildrencache (curatorframework client, String path, Boolean cachedata, Boolean datalscompressed, Final Closeableexecutorservice executorservice);
The public Pathchildrencache constructor parameter description is shown in the following table.
Name of parameter |
Description |
Client |
Curator Client Instance |
Path |
node path for data nodes |
Dataiscompressed |
Whether to compress data |
CacheData |
Used to configure whether to cache the contents of the node, and if configured to True, the client will be able to obtain the data content of the node while receiving a change in the node list, or the data content of the node if configured to False |
Threadfactory |
With these two parameters, developers can process event notifications by constructing a dedicated thread pool |
Executorservice |
With these two parameters, developers can process event notifications by constructing a dedicated thread pool |
Pathchildrencache defines the callback interface Pathchildrencachelistener for event handling, which is defined as follows. Pathchildrenlistener Callback Interface Definition
Public interface Pathchildrenlistener {public void childevent (curatorframework client, pathchildrencacheevent event) Throws Exception;
}
When the child nodes of the specified node change, the method is recalled. All event types are defined in the Pathchildrencacheevent class, mainly including new child nodes (child_added), child node data changes (child_updated), and child node deletion (child_removed) three classes.
The following is a practical example of how to use Pathchildrencache in your code. Pathchildrencache Use Example
public class Pathchildrencache_sample {
static String Path = "/zk-book/nodecache";
static Curatorframework client = Curatorframeworkfactory.builder (). ConnectString ("domain1.book.zookeeper:2181"). SESSIONTIMEOUTMS (5000). Retrypolicy (New Exponentialbackoffretry (1000, 3)). Build ();
public static void Main (string[] args) throws Exception {
Client.start ();
Pathchildrencache cache = new Pathchildrencache (client, path, true);
Cache.start (startmode.post_initialized_event);
Cache.getlistenable (). AddListener (New Pathchildrencachelistener () {
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_updated:
System.out.println ("child_updated," + event.getdata (). GetPath ());
Break
Case child_removed:
System.out.println ("child_removed," + event.getdata (). GetPath ());
Break
Default
Break
}
}
});
Client.create (). Withmode (createmode.persistent). Forpath (path);
Thread.Sleep (1000);
Client.create (). Withmode (create.persistent). Forpath (path + "/C1");
Thread.Sleep (1000);
Client.delete (). Forpath (path + "/C1");
Thread.Sleep (1000);
Client.delete (). Forpath (path);
Thread.Sleep (Integer.max_value);
}
}
Run the program, the output is as follows:
In the above example program, the/zk-book node is monitored for child node change events, once the node adds/deletes child nodes, or when the child node data is changed, the Pathchildrencachelistener is recalled, and the corresponding event type is processed accordingly. At the same time, we also see that there is no notification to the client of changes to the node Zk=book itself.
In addition, as with other zookeeper client products, curator cannot perform event monitoring on level two subnodes. In other words, if you use Pathchildrencache to listen to/zk-book, then when the/ZK-BOOK/C1/C2 node is created or deleted, it is impossible to trigger the child node Change event.
Haha