You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@curator.apache.org by Check Peck <co...@gmail.com> on 2014/02/10 03:07:32 UTC

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.

Re: How to watch on descendant znodes using Curator PathCache?

Posted by Jordan Zimmerman <jo...@jordanzimmerman.com>.
That’s essentially what I was suggesting. But, you’d need to keep track of all the created PathChildrenCache objects in a map or something so that you can delete/close them when needed. Also, use a common executor service to avoid thread pool explosion.

-JZ

From: Check Peck Check Peck
Reply: Check Peck comptechgeeky@gmail.com
Date: February 9, 2014 at 10:10:26 PM
To: Jordan Zimmerman jordan@jordanzimmerman.com
Subject:  Re: How to watch on descendant znodes using Curator PathCache?  
What about for now, If I keep on recursively using PathChildrenCache for making a watches on each children's?

    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: {

                        String path = ZKPaths.getPathAndNode(event.getData().getPath()).getPath();
                        String node = ZKPaths.getNodeFromPath(event.getData().getPath());

                        System.out.println("Node added: " + path + " " + node);
                       
                        // recursively keep on creating watches
                        PathChildrenCache cache = new PathChildrenCache(client, path + "/" + node, true);
                        cache.start();

                        addListener(cache);

                        break;
                    }
                }
            }
        }
    }

Will there be any problem if I do it like this?


On Sun, Feb 9, 2014 at 6:53 PM, Jordan Zimmerman <jo...@jordanzimmerman.com> wrote:
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 <jo...@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.



Re: How to watch on descendant znodes using Curator PathCache?

Posted by Check Peck <co...@gmail.com>.
What about for now, If I keep on recursively using PathChildrenCache for
making a watches on each children's?

    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: {

                        String path =
ZKPaths.getPathAndNode(event.getData().getPath()).getPath();
                        String node =
ZKPaths.getNodeFromPath(event.getData().getPath());

                        System.out.println("Node added: " + path + " " +
node);

                        // recursively keep on creating watches
                        PathChildrenCache cache = new
PathChildrenCache(client, path + "/" + node, true);
                        cache.start();

                        addListener(cache);

                        break;
                    }
                }
            }
        }
    }

Will there be any problem if I do it like this?


On Sun, Feb 9, 2014 at 6:53 PM, Jordan Zimmerman <jordan@jordanzimmerman.com
> wrote:

> 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 <co...@gmail.com>
> 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 <co...@gmail.com>
>> 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.
>>
>>
>

Re: How to watch on descendant znodes using Curator PathCache?

Posted by Jordan Zimmerman <jo...@jordanzimmerman.com>.
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 <jo...@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.


Re: How to watch on descendant znodes using Curator PathCache?

Posted by Cameron McKenzie <mc...@gmail.com>.
That author would be me, sorry I haven't got back to you Jordan, just been
flat out with work. I believe that some additional unit tests were
required, which I haven't got a chance to look at yet.

Check, the patch is attached to the Jira ticket if you want to give it a
spin. I'll eventually get around to writing some additional unit tests, but
I'm not sure when that will be, so more than happy for you to look into it
if you've got time.
cheers
Cam


On Mon, Feb 10, 2014 at 1:42 PM, Check Peck <co...@gmail.com> wrote:

> Sure Jordan. Let me take a look into that. In the meantime, is there any
> workaround for this for now which I can try it out or if you have any
> example which I can take a look?
>
>
> On Sun, Feb 9, 2014 at 6:41 PM, Jordan Zimmerman <
> jordan@jordanzimmerman.com> wrote:
>
>> Sorry - I thought you were using NodeCache. There's already an issue to
>> add recursive support to PathChildrenCache:
>> https://issues.apache.org/jira/browse/CURATOR-33 - I'm waiting for the
>> author of the patch to respond to some comments. Maybe you can take on this
>> issue as I haven't heard back from him..
>>
>> -JZ
>>
>> ------------------------------
>> From: Check Peck Check Peck <co...@gmail.com>
>> 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 <co...@gmail.com>
>>> 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.
>>>
>>>
>>
>

Re: How to watch on descendant znodes using Curator PathCache?

Posted by Check Peck <co...@gmail.com>.
Sure Jordan. Let me take a look into that. In the meantime, is there any
workaround for this for now which I can try it out or if you have any
example which I can take a look?


On Sun, Feb 9, 2014 at 6:41 PM, Jordan Zimmerman <jordan@jordanzimmerman.com
> wrote:

> Sorry - I thought you were using NodeCache. There's already an issue to
> add recursive support to PathChildrenCache:
> https://issues.apache.org/jira/browse/CURATOR-33 - I'm waiting for the
> author of the patch to respond to some comments. Maybe you can take on this
> issue as I haven't heard back from him..
>
> -JZ
>
> ------------------------------
> From: Check Peck Check Peck <co...@gmail.com>
> 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 <co...@gmail.com>
>> 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.
>>
>>
>

Re: How to watch on descendant znodes using Curator PathCache?

Posted by Jordan Zimmerman <jo...@jordanzimmerman.com>.
Sorry - I thought you were using NodeCache. There’s already an issue to add recursive support to PathChildrenCache: https://issues.apache.org/jira/browse/CURATOR-33 - I’m waiting for the author of the patch to respond to some comments. Maybe you can take on this issue as I haven’t heard back from him..

-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 <jo...@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.


Re: How to watch on descendant znodes using Curator PathCache?

Posted by Check Peck <co...@gmail.com>.
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 <co...@gmail.com>
> 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.
>
>

Re: How to watch on descendant znodes using Curator PathCache?

Posted by Jordan Zimmerman <jo...@jordanzimmerman.com>.
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.