Reputation: 7932
I keep reading about using zookeeper for configuration managment.
I know there is Apache Curator for easy interaction with zookeeper.
I have some clients connected to a set of resources. And they must use those resources the same way. Stuff like sharding and master election between the resources.
I want to use zookeeper so that if a client in a given time note that one of the resources is down, it can change the configuration and the other clients can inmediatly start using the new configuration.
So one client writes the configuration in a path in a znode in zookeeper, and the other clients are watching that path.
How to set a data in curator:
zk.setData().forPath(pathName, "data".getBytes());
How to watch a path in curator:
zk.getData().usingWatcher(watcher).forPath(pathName);
Now, when the value of the path changes and the watch is trigger I must get the new value of the path. How do I do that?
This always returns null inside of proccess()
zk.getData().watched().inBackground().forPath(pathName)
Other question: As it says in the documentation, after I get the new value, do I have to set the watcher again?
Source of the watcher:
CuratorWatcher watcher = new CuratorWatcher(){
public void process(WatchedEvent event) {
System.out.println("event wt");
System.out.println(ToStringBuilder.reflectionToString(event));
try {
System.out.println(zk.getData().watched().inBackground().forPath(pathName));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
};
System.out.println("event wt");
try {
zk.getData().usingWatcher(this).forPath(pathName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}};
Upvotes: 1
Views: 3674
Reputation: 2929
in Curator there is the adapter :
* Get children and set the given watcher on the node.
*/
return client.getChildren().usingWatcher(watcher).forPath(path);
or you can use CuratorListener
/**
* Get children and set a watcher on the node. The watcher notification will come through the
* CuratorListener (see setDataAsync() above).
*/
return client.getChildren().watched().forPath(path);
Upvotes: 1