On Fri, Nov 8, 2013 at 1:36 PM, Cameron McKenzie <[email protected]>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);
}
}