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 20:56:08 GMT
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