Skip to content

Instantly share code, notes, and snippets.

@definitelyMVP
Last active September 5, 2018 05:53
Show Gist options
  • Save definitelyMVP/4983881 to your computer and use it in GitHub Desktop.
Save definitelyMVP/4983881 to your computer and use it in GitHub Desktop.
a CuratorListener Example
import com.netflix.curator.framework.CuratorFramework;
import com.netflix.curator.framework.CuratorFrameworkFactory;
import com.netflix.curator.framework.api.CuratorEvent;
import com.netflix.curator.framework.api.CuratorListener;
import com.netflix.curator.retry.ExponentialBackoffRetry;
/**
* run this and change the data of "/yl", then you should see output like
* "WatchedEvent state:SyncConnected type:NodeDataChanged path:/yl watched"
*
* @author yl
* @date 2013-02-19
*/
public class CuratorListenerExample {
public static void main(String[] args) throws Exception {
ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, Integer.MAX_VALUE);
CuratorFramework curator = CuratorFrameworkFactory.newClient("10.12.136.235:2181", retryPolicy);
curator.start();
curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
curator.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
System.out.println(event.getWatchedEvent() + " watched.");
}
});
String path = "/yl";
// one-time watch
curator.getData().watched().forPath(path);
Thread.sleep(Integer.MAX_VALUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment