Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to watch for events on the descendant nodes in ZooKeeper using curator?

I am working on a project in which I need to maintain a watches on a node, and that nodes children as well. I have tried using PathCache but I am not sure how to watch for childrens children here?

Here my root node is - "/my/test" and I am keeping a watch on that node using the below code. What I want to do is, to keep the watch on "/my/test" znode. So suppose if these nodes gets added to my root node -

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

Then I should get notified (till this part I am able to make it work) but if any new node gets added, updated or removed to "/my/test/test1", "/my/test/test2" and "/my/test/test3" then I should also get notified and this is the part I am not able to understand how to make it work.

Whenever I am adding any new node to "/my/test" such as "/my/test/test1", "/my/test/test2", "/my/test/test3" then the watch gets triggered with the use of below code. But if I am adding any new node to "/my/test/test1" or "/my/test/test2", then no watches get triggerd and I am not sure how to add the code for that as well? Any thoughts how this can be done?

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

Below is my code which works fine for "/my/test" children but not the childrens of "/my/test/test1" and etc etc.

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 {
                Thread.sleep(50000);
            } catch(InterruptedException e) {
            }
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

Below 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()) {
            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().addListener(listener);
}

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

like image 474
AKIWEB Avatar asked Feb 10 '14 01:02

AKIWEB


2 Answers

use the TreeCache instead. It does everything the PathCache does plus can handle child nodes.

like image 165
msj Avatar answered Oct 09 '22 05:10

msj


Did you get solution to this problem?

One hack I can suggest to use the children like:

  • /my/test/test1_node1
  • /my/test/test1_node2

So appending the child node of test1 into the name of test1 itself.

like image 37
Sanjeev Avatar answered Oct 09 '22 04:10

Sanjeev