curator-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jordan Zimmerman <jor...@jordanzimmerman.com>
Subject Re: How to watch on descendant znodes using Curator PathCache?
Date Mon, 10 Feb 2014 02:53:25 GMT
If I were to do something like this I’d use a Guava Cache or something to hold PathChildrenCache
objects for each child node. That’s a quick’n’dirty solution. I think you’d want to
pass in a common CloseableExecutorService to avoid too many threads. This might get cumbersome
to manage though. If you only need the one sub-level, you could use NodeCache for each of
the children instead of PathChildrenCache. Whenever the PathChildrenCache sends a CHILD_ADDED,
you allocate a new PatchChildrenCache/NodeCache for that node. When CHILD_REMOVED is sent,
close and remove it. So, what you end with is a map of PatchChildrenCache/NodeCache instances. 

So, this is off the top of my head and I haven’t thought through all the edge cases. If
it works, though, it might make a nice recipe addition.

-JZ

From: Check Peck Check Peck
Reply: Check Peck comptechgeeky@gmail.com
Date: February 9, 2014 at 9:35:24 PM
To: Jordan Zimmerman jordan@jordanzimmerman.com
Subject:  Re: How to watch on descendant znodes using Curator PathCache?  
Thanks for suggestion. I am using PathChildrenCache for one level. For example -

If my root node is - "/my/test" and I am keeping a watch on that node using the PathChildrenCache
as mentioned in my previous email code. So suppose if these nodes gets added to my root node
-

    "/my/test/test1"
    "/my/test/test2"
    "/my/test/test3"
   
Then I get notified and it works fine with the PathChildrenCache code but if any new node
gets added, updated or removed to `"/my/test/test1"`, `"/my/test/test2"` and `"/my/test/test3"`
then it doesn't works and no watches gets triggered and I am not able to understand how to
make that work as my understanding is very limited as of now.



On Sun, Feb 9, 2014 at 6:31 PM, Jordan Zimmerman <jordan@jordanzimmerman.com> wrote:
Do you need just the one level? If so, why not use PathChildrenCache: http://curator.apache.org/curator-recipes/path-cache.html

-JZ

From: Check Peck Check Peck
Reply: user@curator.apache.org user@curator.apache.org
Date: February 9, 2014 at 9:08:20 PM
To: user user@curator.apache.org
Subject:  How to watch on descendant znodes using Curator PathCache?
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.


Mime
View raw message