curator-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Senecal, Shaun | Shaun | BDD" <>
Subject RE: How to watch for events on all descendant nodes in ZooKeeper using curator?
Date Mon, 18 Nov 2013 06:19:43 GMT
You need to set a child watcher on each node you want to watch.   So if you initially set a
watch on "/rj/colo", then a new node is created (ie: "/rj/colo/phx") you need to add a new
watch on the new node as well.

From: Techy Teck []
Sent: Monday, November 18, 2013 1:35 PM
To: user
Subject: How to watch for events on all 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 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");

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


            for(;;) {
                try {
                } catch(InterruptedException e) {

            // processCommands(client, cache);
        } catch (Exception e1) {

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()));

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

                case CHILD_REMOVED: {
                    System.out.println("Node removed: "    + ZKPaths.getNodeFromPath(event.getData().getPath()));

View raw message