curator-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Techy Teck <comptechge...@gmail.com>
Subject How to watch for events on all descendant nodes in ZooKeeper using curator?
Date Mon, 18 Nov 2013 04:35:34 GMT
I am working on a project in which I need to maintain a watches on a node,
and that nodes children 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 - "/rj/colo" and keeping a watch on that node as well.

Whenever I am adding any new node to "/rj/colo" such as "/rj/colo/phx",
"/rj/colo/slc", "/rj/colo/lvs" then the watch gets triggered. But if I am
adding any new node to "/rj/colo/phx" or "/rj/colo/slc", 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 efficiently? 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 "/rj/colo" children but not the
childrens of "/rj/colo/phx" and etc etc.

    private static final String PATH = "/rj/colo";

    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) {
                }
            }

            // processCommands(client, cache);
        } 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);
    }

Mime
View raw message