How do I use Curator to monitor events on the neighborhood of ZooKeeper?

I am developing a project, and I need to maintain a monitor on a node, and the node is also a child node. I have tried using PathCache, but I am not sure how to watch children’s children here ?

Here my root node is-“/my/test”, I am using the following code to monitor this node. What I want to do is to put the watch on “/my/test” znode. So suppose these nodes are added to my root node –

"/my/test/test1"
"/my/test/test2"
"/my/test/test3"

Then I should be notified (until this part I can make it work) but if any new nodes are added, updated or deleted to “/my/ test/test1”, “/my/test/test2” and “/my/test/test3” Then I should also be notified, this is the part where I cannot understand how to make it work.

Whenever When I add any new node to “/my/test”, such as “/my/test/test1”, “/my/test/test2”, “/my/test/test3”, the watch will be triggered by the following code But, if I add any new node to “/my/test/test1” or “/my/test/test2”, then no watch is triggered, and I am not sure how to add code? Any ideas to do this?

Maybe if someone has done this in the past.. So any example will be of great help to me..

Below is my code, suitable for “/my/ “test” children, but not applicable to children such as “/my/test/test1”.

private static final String PATH = "/my/test";

public static void main(String[] args) {
CuratorFramework client = null;
PathChildrenCache cache = null;
try {
client = CuratorClient.createSimple(" localhost:2181");
client.start();

// in this example we will cache data. Notice that this is optional.
cache = new PathChildrenCache(client , PATH, true);
cache.start();

addListener(cache);

for(;;) {
try {< br /> Thread.sleep(50000);
} catch(InterruptedException e) {
}
}
} catch (Exception e1) {
e1.printStackTrace( );
}
}

Here is my addListener method –

private static void addListener(PathChildr enCache cache) {

PathChildrenCacheListener listener = new PathChildrenCacheListener() {
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
switch (event.getType()) {
case CHILD_ADDED: {
System.out.println("Node added: "+ ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}

case CHILD_UPDATED: {
System.out.println("Node changed: "+ ZKPaths.getNodeFromPath(event.getData().getPath()));
break ;
}

case CHILD_REMOVED: {
System.out.println("Node removed: "+ ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}
default:
break;
}
}
};
cache.getListenable().addL istener(listener);
}

Can anyone provide a simple example for my use case? I’m using the recently released Curator 2.4.0.

Please use TreeCache instead. It completes the path I can do all the things and deal with child nodes.

I am developing a project, I need to maintain a monitor on the node, and the node is also a child node. I tried to use PathCache, but I am not sure how to watch children’s children here?

Here my root node is-“/my/test”, I am using the following code to monitor this node. What I want to do is to put the watch on “/my/test” znode. So suppose these nodes are added to my root node –

"/my/test/test1"
"/my/test/test2"
"/my/test/test3"

Then I should be notified (until this part I can make it work) but if any new nodes are added, updated or deleted to “/my/ test/test1”, “/my/test/test2” and “/my/test/test3” Then I should also be notified, this is the part where I cannot understand how to make it work.

Whenever When I add any new node to “/my/test”, such as “/my/test/test1”, “/my/test/test2”, “/my/test/test3”, the watch will be triggered by the following code But, if I add any new node to “/my/test/test1” or “/my/test/test2”, then no watch is triggered, and I am not sure how to add code? Any ideas to do this?

Maybe if someone has done this in the past.. So any example will be of great help to me..

Below is my code, suitable for “/my/ “test” children, but not applicable to children such as “/my/test/test1”.

private static final String PATH = "/my/test";

public static void main(String[] args) {
CuratorFramework client = null;
PathChildrenCache cache = null;
try {
client = CuratorClient.createSimple(" localhost:2181");
client.start();

// in this example we will cache data. Notice that this is optional.
cache = new PathChildrenCache(client , PATH, true);
cache.start();

addListener(cache);

for(;;) {
try {< br /> Thread.sleep(50000);
} catch(InterruptedException e) {
}
}
} catch (Exception e1) {
e1.printStackTrace( );
}
}

Here is my addListener method –

private static void addListener(PathChildrenCache cache) {

PathChildrenCacheListener listener = new PathChildrenCacheListener() {
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
switch (event.getType()) {< br /> case CHILD_ADDED: {
System.out.println("Node added: "+ ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}

case CHILD_UPDATED: {
System.out.println("Node changed: "+ ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}

case CHILD_REMOVED: {
System.out.println("Node removed: "+ ZKPaths.getNodeFromPath(event.getData().getPath()));< br /> break;
}
default:
break;
}
}
};
cache.getListenable().addListener (listener);
}

Can anyone provide a simple example for my use case? I am using the recently released Curator 2.4.0.

Please use TreeCache instead. It does all the things PathCache can do and handle child nodes.

p>

Leave a Comment

Your email address will not be published.