Skip to content

Instantly share code, notes, and snippets.

@definitelyMVP
Last active July 1, 2016 10:39
Show Gist options
  • Save definitelyMVP/4982923 to your computer and use it in GitHub Desktop.
Save definitelyMVP/4982923 to your computer and use it in GitHub Desktop.
curator node cache example
import com.netflix.curator.framework.CuratorFramework;
import com.netflix.curator.framework.CuratorFrameworkFactory;
import com.netflix.curator.framework.recipes.cache.ChildData;
import com.netflix.curator.framework.recipes.cache.NodeCache;
import com.netflix.curator.framework.recipes.cache.NodeCacheListener;
import com.netflix.curator.retry.ExponentialBackoffRetry;
/**
* @author yl
* @date 2013-02-19
*/
public class PersistantWatcherExample {
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();
String path = "/yl";
System.out.println("the original data is " + new String(curator.getData().forPath(path)));
final NodeCache nodeCache = new NodeCache(curator, path);
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
ChildData currentData = nodeCache.getCurrentData();
System.out.println("data change watched, and current data = " + new String(currentData.getData()));
}
});
nodeCache.start();
Thread.sleep(Integer.MAX_VALUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment