You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@curator.apache.org by Techy Teck <co...@gmail.com> on 2013/11/08 21:17:57 UTC

How to use Curator for Watching the nodes and trigger purpose

I recently started working with Zookeeper and I am using Curator library
for this. I am able to create all kind of nodes and delete nodes as well.

Now I started working on Watches but I am not able to get any simple
example how to use Curator library to do watches on any parent nodes and if
any new child nodes get added to these parent nodes, watches should get
triggered and perform some activity then.

Is there any simple example that I can see on how watches works using
Curator library? Any github link will also be great. Thanks.

Re: How to use Curator for Watching the nodes and trigger purpose

Posted by Cameron McKenzie <mc...@gmail.com>.
Yes, if you want to keep watching a node after you've been notified that
it's changed then you need to add another watch to it.


On Sat, Nov 9, 2013 at 11:12 AM, Techy Teck <co...@gmail.com> wrote:

> This is working for me after I added watchedGetChildren method. Is this
> the right way to do it?
>
>
> public void eventReceived(CuratorFramework client, CuratorEvent event)
> throws Exception {
>                 // examine event for details
>
>                 System.out.println("Hello World");
>                 watchedGetChildren(client, "/foo");
>             }
>
>
> On Fri, Nov 8, 2013 at 4:04 PM, Techy Teck <co...@gmail.com>wrote:
>
>>
>> On Fri, Nov 8, 2013 at 1:36 PM, Cameron McKenzie <mc...@gmail.com>wrote:
>>
>>> for(;;) {
>>>     try {
>>>         Thread.sleep(50000);
>>>     } catch(InterruptedException e) {
>>>     }
>>> }
>>>
>>
>>
>> Aah... I was in the impression that I need to start some sort of server
>> using Curator.. Never mind.. It works fine for the first time.. But second
>> time when I create a new child nodes again, it doesn't print out Hello
>> World again which strikes me that you mentioned I need to rewatch the
>> parent node after each event. Below is my example.. Do I need to add
>> anything in eventReceived method to rewatch again?
>>
>>
>>     public class ZKWatcher {
>>
>>         public static void main(String[] args) {
>>
>>             try {
>>                 CuratorFramework client =
>> CuratorClient.createSimple("localhost:2181");
>>                 client.start();
>>
>>                 List<String> children = watchedGetChildren(client,
>> "/foo");
>>                 System.out.println(children);
>>
>>                 handleWatchEvents(client);
>>
>>                 for(;;) {
>>                     try {
>>                         Thread.sleep(50000);
>>                     } catch(InterruptedException e) {
>>                     }
>>                 }
>>
>>
>>             } catch (Exception ex) {
>>                 ex.printStackTrace();
>>             }
>>         }
>>
>>         public static List<String> watchedGetChildren(CuratorFramework
>> client, String path) throws Exception {
>>
>>             return client.getChildren().watched().forPath(path);
>>         }
>>
>>         public static void handleWatchEvents(CuratorFramework client)
>> throws Exception {
>>             // this is one method of getting event/async notifications
>>             CuratorListener listener = new CuratorListener() {
>>                 public void eventReceived(CuratorFramework client,
>> CuratorEvent event) throws Exception {
>>                     // examine event for details
>>
>>                     System.out.println("Hello World");
>>                 }
>>             };
>>             client.getCuratorListenable().addListener(listener);
>>         }
>>     }
>>
>>
>>
>

Re: How to use Curator for Watching the nodes and trigger purpose

Posted by Techy Teck <co...@gmail.com>.
This is working for me after I added watchedGetChildren method. Is this the
right way to do it?

public void eventReceived(CuratorFramework client, CuratorEvent event)
throws Exception {
                // examine event for details

                System.out.println("Hello World");
                watchedGetChildren(client, "/foo");
            }


On Fri, Nov 8, 2013 at 4:04 PM, Techy Teck <co...@gmail.com> wrote:

>
> On Fri, Nov 8, 2013 at 1:36 PM, Cameron McKenzie <mc...@gmail.com>wrote:
>
>> for(;;) {
>>     try {
>>         Thread.sleep(50000);
>>     } catch(InterruptedException e) {
>>     }
>> }
>>
>
>
> Aah... I was in the impression that I need to start some sort of server
> using Curator.. Never mind.. It works fine for the first time.. But second
> time when I create a new child nodes again, it doesn't print out Hello
> World again which strikes me that you mentioned I need to rewatch the
> parent node after each event. Below is my example.. Do I need to add
> anything in eventReceived method to rewatch again?
>
>
>     public class ZKWatcher {
>
>         public static void main(String[] args) {
>
>             try {
>                 CuratorFramework client =
> CuratorClient.createSimple("localhost:2181");
>                 client.start();
>
>                 List<String> children = watchedGetChildren(client, "/foo");
>                 System.out.println(children);
>
>                 handleWatchEvents(client);
>
>                 for(;;) {
>                     try {
>                         Thread.sleep(50000);
>                     } catch(InterruptedException e) {
>                     }
>                 }
>
>
>             } catch (Exception ex) {
>                 ex.printStackTrace();
>             }
>         }
>
>         public static List<String> watchedGetChildren(CuratorFramework
> client, String path) throws Exception {
>
>             return client.getChildren().watched().forPath(path);
>         }
>
>         public static void handleWatchEvents(CuratorFramework client)
> throws Exception {
>             // this is one method of getting event/async notifications
>             CuratorListener listener = new CuratorListener() {
>                 public void eventReceived(CuratorFramework client,
> CuratorEvent event) throws Exception {
>                     // examine event for details
>
>                     System.out.println("Hello World");
>                 }
>             };
>             client.getCuratorListenable().addListener(listener);
>         }
>     }
>
>
>

Re: How to use Curator for Watching the nodes and trigger purpose

Posted by Techy Teck <co...@gmail.com>.
On Fri, Nov 8, 2013 at 1:36 PM, Cameron McKenzie <mc...@gmail.com>wrote:

> for(;;) {
>     try {
>         Thread.sleep(50000);
>     } catch(InterruptedException e) {
>     }
> }
>


Aah... I was in the impression that I need to start some sort of server
using Curator.. Never mind.. It works fine for the first time.. But second
time when I create a new child nodes again, it doesn't print out Hello
World again which strikes me that you mentioned I need to rewatch the
parent node after each event. Below is my example.. Do I need to add
anything in eventReceived method to rewatch again?

    public class ZKWatcher {

        public static void main(String[] args) {

            try {
                CuratorFramework client =
CuratorClient.createSimple("localhost:2181");
                client.start();

                List<String> children = watchedGetChildren(client, "/foo");
                System.out.println(children);

                handleWatchEvents(client);

                for(;;) {
                    try {
                        Thread.sleep(50000);
                    } catch(InterruptedException e) {
                    }
                }


            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        public static List<String> watchedGetChildren(CuratorFramework
client, String path) throws Exception {

            return client.getChildren().watched().forPath(path);
        }

        public static void handleWatchEvents(CuratorFramework client)
throws Exception {
            // this is one method of getting event/async notifications
            CuratorListener listener = new CuratorListener() {
                public void eventReceived(CuratorFramework client,
CuratorEvent event) throws Exception {
                    // examine event for details

                    System.out.println("Hello World");
                }
            };
            client.getCuratorListenable().addListener(listener);
        }
    }

Re: How to use Curator for Watching the nodes and trigger purpose

Posted by Cameron McKenzie <mc...@gmail.com>.
First, you should register your listeners before you place any watches.
Otherwise there's the possibility that you'll miss a watch event. So you
should call handleWatchEvents() before you do the getChildren() call.

Secondly, your program is exiting because you get to the end of your main()
method. This is standard Java behaviour and nothing to do with Curator /
ZooKeeper. If you want your program to hang around for ZooKeeper events
then you'll need to make sure it doesn't exit.

Add the following to the end of your main

for(;;) {
    try {
        Thread.sleep(50000);
    } catch(InterruptedException e) {
    }
}

This will just cause it to sleep forever, so your program will never exit
(you'll have to force it to close).


On Sat, Nov 9, 2013 at 8:04 AM, Techy Teck <co...@gmail.com> wrote:

> I am not calling that method at all.. I see, you mean to say, I should
> called handleWatchEvent(CuratorFramework client) after the
> watchedGetChildren method? Something like this -  Slightly updated my code
> -
>
> public class ZKWatcher {
>
>     public static void main(String[] args) {
>
>         try {
>             CuratorFramework client =
> CuratorClient.createSimple("localhost:2181");
>             client.start();
>
>             List<String> children = watchedGetChildren(client, "/foo");
>             System.out.println(children);
>
>             handleWatchEvents(client);
>
>         } catch (Exception ex) {
>             ex.printStackTrace();
>         }
>     }
>
>     public static List<String> watchedGetChildren(CuratorFramework client,
> String path) throws Exception {
>
>         return client.getChildren().watched().forPath(path);
>     }
>
>     public static void handleWatchEvents(CuratorFramework client) throws
> Exception {
>
>         // this is one method of getting event/async notifications
>         CuratorListener listener = new CuratorListener() {
>             public void eventReceived(CuratorFramework client,
> CuratorEvent event) throws Exception {
>                 // examine event for details
>
>                 System.out.println("Hello World");
>             }
>         };
>         client.getCuratorListenable().addListener(listener);
>     }
> }
>
>
> I tried this as well but my program gets stopped if I am running it as a
> java application and it prints out only the child nodes of */foo* parent
> node... So if I am adding any new child node through zkCli on /foo parent
> node, eventReceived won't be called then right?
>
> Correct me if my understanding is wrong..?
>
>
>
>
> On Fri, Nov 8, 2013 at 12:59 PM, Cameron McKenzie <mc...@gmail.com>wrote:
>
>> WHen does handleWatchEvents get called()? In your sample code the method
>> is defined but its not called from your main() function.
>>
>>
>> On Sat, Nov 9, 2013 at 7:56 AM, Techy Teck <co...@gmail.com>wrote:
>>
>>>
>>> On Fri, Nov 8, 2013 at 12:31 PM, Cameron McKenzie <
>>> mckenzie.cam@gmail.com> wrote:
>>>
>>>> client.getCuratorListenable().addListener(new CuratorListener() {
>>>>     public void eventReceived(CuratorFramework curator, CuratorEvent
>>>> event) {
>>>>         //Your event handling code here.
>>>>     }
>>>> });
>>>>
>>>
>>>
>>> Thanks Cameron and Jordan for the suggestion.. I am also using the same
>>> code in my simple test program but one thing that I am not able to
>>> understand is - My client program should always be running to detect any
>>> trigger happening because of any watches on the parent node right?
>>> Currently, I am running my simple Java static void main code which gets the
>>> children and has the watch code as well - Something like below is my full
>>> program-
>>>
>>> public class ZKWatcher {
>>>
>>>     public static void main(String[] args) {
>>>
>>>         try {
>>>             CuratorFramework client =
>>> CuratorClient.createSimple("localhost:2181");
>>>             client.start();
>>>
>>>             List<String> children = watchedGetChildren(client, "/foo");
>>>
>>>         } catch (Exception ex) {
>>>             ex.printStackTrace();
>>>         }
>>>     }
>>>
>>>     public static List<String> watchedGetChildren(CuratorFramework
>>> client, String path) throws Exception {
>>>
>>>         return client.getChildren().watched().forPath(path);
>>>     }
>>>
>>>     public static void handleWatchEvents(CuratorFramework client, String
>>> path, byte[] payload) throws Exception {
>>>         // this is one method of getting event/async notifications
>>>         CuratorListener listener = new CuratorListener() {
>>>             public void eventReceived(CuratorFramework client,
>>> CuratorEvent event) throws Exception {
>>>                 // examine event for details
>>>
>>>                 System.out.println("Hello World");
>>>             }
>>>         };
>>>         client.getCuratorListenable().addListener(listener);
>>>     }
>>> }
>>>
>>> I run the above program as the simple java application and it gets all
>>> the children from the */foo* parent node but the above program gets
>>> stopped after getting the children. So I am wondering if I am adding any
>>> child node to the same */foo* parent node behind the scene using zkCli,
>>> then eventReceived won't get called for sure as my program was stopped..
>>>
>>> Which makes me to think, I need to run my above program as some sort of
>>> server way so that it will keep on running once we start it and then if any
>>> trigger is happening because of addition of any child nodes to /foo parent
>>> node then eventReceived will get called automatically?
>>>
>>> if yes, then how to do that? Pardon my ignorance on this.
>>>
>>>
>>>
>>>
>>
>

Re: How to use Curator for Watching the nodes and trigger purpose

Posted by Techy Teck <co...@gmail.com>.
I am not calling that method at all.. I see, you mean to say, I should
called handleWatchEvent(CuratorFramework client) after the
watchedGetChildren method? Something like this -  Slightly updated my code
-

public class ZKWatcher {

    public static void main(String[] args) {

        try {
            CuratorFramework client =
CuratorClient.createSimple("localhost:2181");
            client.start();

            List<String> children = watchedGetChildren(client, "/foo");
            System.out.println(children);

            handleWatchEvents(client);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static List<String> watchedGetChildren(CuratorFramework client,
String path) throws Exception {

        return client.getChildren().watched().forPath(path);
    }

    public static void handleWatchEvents(CuratorFramework client) throws
Exception {
        // this is one method of getting event/async notifications
        CuratorListener listener = new CuratorListener() {
            public void eventReceived(CuratorFramework client, CuratorEvent
event) throws Exception {
                // examine event for details

                System.out.println("Hello World");
            }
        };
        client.getCuratorListenable().addListener(listener);
    }
}


I tried this as well but my program gets stopped if I am running it as a
java application and it prints out only the child nodes of */foo* parent
node... So if I am adding any new child node through zkCli on /foo parent
node, eventReceived won't be called then right?

Correct me if my understanding is wrong..?




On Fri, Nov 8, 2013 at 12:59 PM, Cameron McKenzie <mc...@gmail.com>wrote:

> WHen does handleWatchEvents get called()? In your sample code the method
> is defined but its not called from your main() function.
>
>
> On Sat, Nov 9, 2013 at 7:56 AM, Techy Teck <co...@gmail.com>wrote:
>
>>
>> On Fri, Nov 8, 2013 at 12:31 PM, Cameron McKenzie <mckenzie.cam@gmail.com
>> > wrote:
>>
>>> client.getCuratorListenable().addListener(new CuratorListener() {
>>>     public void eventReceived(CuratorFramework curator, CuratorEvent
>>> event) {
>>>         //Your event handling code here.
>>>     }
>>> });
>>>
>>
>>
>> Thanks Cameron and Jordan for the suggestion.. I am also using the same
>> code in my simple test program but one thing that I am not able to
>> understand is - My client program should always be running to detect any
>> trigger happening because of any watches on the parent node right?
>> Currently, I am running my simple Java static void main code which gets the
>> children and has the watch code as well - Something like below is my full
>> program-
>>
>> public class ZKWatcher {
>>
>>     public static void main(String[] args) {
>>
>>         try {
>>             CuratorFramework client =
>> CuratorClient.createSimple("localhost:2181");
>>             client.start();
>>
>>             List<String> children = watchedGetChildren(client, "/foo");
>>
>>         } catch (Exception ex) {
>>             ex.printStackTrace();
>>         }
>>     }
>>
>>     public static List<String> watchedGetChildren(CuratorFramework
>> client, String path) throws Exception {
>>
>>         return client.getChildren().watched().forPath(path);
>>     }
>>
>>     public static void handleWatchEvents(CuratorFramework client, String
>> path, byte[] payload) throws Exception {
>>         // this is one method of getting event/async notifications
>>         CuratorListener listener = new CuratorListener() {
>>             public void eventReceived(CuratorFramework client,
>> CuratorEvent event) throws Exception {
>>                 // examine event for details
>>
>>                 System.out.println("Hello World");
>>             }
>>         };
>>         client.getCuratorListenable().addListener(listener);
>>     }
>> }
>>
>> I run the above program as the simple java application and it gets all
>> the children from the */foo* parent node but the above program gets
>> stopped after getting the children. So I am wondering if I am adding any
>> child node to the same */foo* parent node behind the scene using zkCli,
>> then eventReceived won't get called for sure as my program was stopped..
>>
>> Which makes me to think, I need to run my above program as some sort of
>> server way so that it will keep on running once we start it and then if any
>> trigger is happening because of addition of any child nodes to /foo parent
>> node then eventReceived will get called automatically?
>>
>> if yes, then how to do that? Pardon my ignorance on this.
>>
>>
>>
>>
>

Re: How to use Curator for Watching the nodes and trigger purpose

Posted by Cameron McKenzie <mc...@gmail.com>.
WHen does handleWatchEvents get called()? In your sample code the method is
defined but its not called from your main() function.


On Sat, Nov 9, 2013 at 7:56 AM, Techy Teck <co...@gmail.com> wrote:

>
> On Fri, Nov 8, 2013 at 12:31 PM, Cameron McKenzie <mc...@gmail.com>wrote:
>
>> client.getCuratorListenable().addListener(new CuratorListener() {
>>     public void eventReceived(CuratorFramework curator, CuratorEvent
>> event) {
>>         //Your event handling code here.
>>     }
>> });
>>
>
>
> Thanks Cameron and Jordan for the suggestion.. I am also using the same
> code in my simple test program but one thing that I am not able to
> understand is - My client program should always be running to detect any
> trigger happening because of any watches on the parent node right?
> Currently, I am running my simple Java static void main code which gets the
> children and has the watch code as well - Something like below is my full
> program-
>
> public class ZKWatcher {
>
>     public static void main(String[] args) {
>
>         try {
>             CuratorFramework client =
> CuratorClient.createSimple("localhost:2181");
>             client.start();
>
>             List<String> children = watchedGetChildren(client, "/foo");
>
>         } catch (Exception ex) {
>             ex.printStackTrace();
>         }
>     }
>
>     public static List<String> watchedGetChildren(CuratorFramework client,
> String path) throws Exception {
>
>         return client.getChildren().watched().forPath(path);
>     }
>
>     public static void handleWatchEvents(CuratorFramework client, String
> path, byte[] payload) throws Exception {
>         // this is one method of getting event/async notifications
>         CuratorListener listener = new CuratorListener() {
>             public void eventReceived(CuratorFramework client,
> CuratorEvent event) throws Exception {
>                 // examine event for details
>
>                 System.out.println("Hello World");
>             }
>         };
>         client.getCuratorListenable().addListener(listener);
>     }
> }
>
> I run the above program as the simple java application and it gets all the
> children from the */foo* parent node but the above program gets stopped
> after getting the children. So I am wondering if I am adding any child node
> to the same */foo* parent node behind the scene using zkCli, then
> eventReceived won't get called for sure as my program was stopped..
>
> Which makes me to think, I need to run my above program as some sort of
> server way so that it will keep on running once we start it and then if any
> trigger is happening because of addition of any child nodes to /foo parent
> node then eventReceived will get called automatically?
>
> if yes, then how to do that? Pardon my ignorance on this.
>
>
>
>

Re: How to use Curator for Watching the nodes and trigger purpose

Posted by Techy Teck <co...@gmail.com>.
On Fri, Nov 8, 2013 at 12:31 PM, Cameron McKenzie <mc...@gmail.com>wrote:

> client.getCuratorListenable().addListener(new CuratorListener() {
>     public void eventReceived(CuratorFramework curator, CuratorEvent
> event) {
>         //Your event handling code here.
>     }
> });
>


Thanks Cameron and Jordan for the suggestion.. I am also using the same
code in my simple test program but one thing that I am not able to
understand is - My client program should always be running to detect any
trigger happening because of any watches on the parent node right?
Currently, I am running my simple Java static void main code which gets the
children and has the watch code as well - Something like below is my full
program-

public class ZKWatcher {

    public static void main(String[] args) {

        try {
            CuratorFramework client =
CuratorClient.createSimple("localhost:2181");
            client.start();

            List<String> children = watchedGetChildren(client, "/foo");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static List<String> watchedGetChildren(CuratorFramework client,
String path) throws Exception {

        return client.getChildren().watched().forPath(path);
    }

    public static void handleWatchEvents(CuratorFramework client, String
path, byte[] payload) throws Exception {
        // this is one method of getting event/async notifications
        CuratorListener listener = new CuratorListener() {
            public void eventReceived(CuratorFramework client, CuratorEvent
event) throws Exception {
                // examine event for details

                System.out.println("Hello World");
            }
        };
        client.getCuratorListenable().addListener(listener);
    }
}

I run the above program as the simple java application and it gets all the
children from the */foo* parent node but the above program gets stopped
after getting the children. So I am wondering if I am adding any child node
to the same */foo* parent node behind the scene using zkCli, then
eventReceived won't get called for sure as my program was stopped..

Which makes me to think, I need to run my above program as some sort of
server way so that it will keep on running once we start it and then if any
trigger is happening because of addition of any child nodes to /foo parent
node then eventReceived will get called automatically?

if yes, then how to do that? Pardon my ignorance on this.

Re: How to use Curator for Watching the nodes and trigger purpose

Posted by Cameron McKenzie <mc...@gmail.com>.
client.getCuratorListenable().addListener(new CuratorListener() {
    public void eventReceived(CuratorFramework curator, CuratorEvent event)
{
        //Your event handling code here.
    }
});

client.getChildren().watched().forPath("/bla");

Note the 'watched()' call.

This will return you all children currently under the path, and you will
receive a watch event whenever children are added or removed from the
parent node. Remember that you need to rewatch the parent node after each
event.




On Sat, Nov 9, 2013 at 7:17 AM, Techy Teck <co...@gmail.com> wrote:

> I recently started working with Zookeeper and I am using Curator library
> for this. I am able to create all kind of nodes and delete nodes as well.
>
> Now I started working on Watches but I am not able to get any simple
> example how to use Curator library to do watches on any parent nodes and if
> any new child nodes get added to these parent nodes, watches should get
> triggered and perform some activity then.
>
> Is there any simple example that I can see on how watches works using
> Curator library? Any github link will also be great. Thanks.
>

Re: How to use Curator for Watching the nodes and trigger purpose

Posted by Jordan Zimmerman <jo...@jordanzimmerman.com>.
There are numerous Curator examples here:

	http://curator.apache.org/curator-examples/index.html

Setting watches, etc. is no different with Curator than with ZooKeeper.

-JZ

On Nov 8, 2013, at 12:17 PM, Techy Teck <co...@gmail.com> wrote:

> I recently started working with Zookeeper and I am using Curator library for this. I am able to create all kind of nodes and delete nodes as well.
> 
> Now I started working on Watches but I am not able to get any simple example how to use Curator library to do watches on any parent nodes and if any new child nodes get added to these parent nodes, watches should get triggered and perform some activity then.
> 
> Is there any simple example that I can see on how watches works using Curator library? Any github link will also be great. Thanks.