curator-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Techy Teck <comptechge...@gmail.com>
Subject Re: How to use Curator for Watching the nodes and trigger purpose
Date Fri, 08 Nov 2013 21:04:45 GMT
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 <mckenzie.cam@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 <comptechgeeky@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.
>>
>>
>>
>>
>

Mime
View raw message