Curator tutorial (1) watcher snooping
1.watcher mode 1 (listens for changes to the current node)
String address = "mini1:2181,mini2:2181,mini3:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000, 10); CuratorFramework client = CuratorFrameworkFactory.newClient (address, new ExponentialBackoffRetry (1000, 3)); client.start () / / create node watcher listening final NodeCache cache = new NodeCache (client, "/ super", false); cache.start (true) Cache.getListenable () .addListener (new NodeCacheListener () {public void nodeChanged () throws Exception {System.out.println (cache.getCurrentData () .getPath ()); System.out.println (cache.getCurrentData () .getStat () })
2.watcher mode 2 (listening for changes in the child nodes of the current node)
String address = "mini1:2181,mini2:2181,mini3:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000, 10); CuratorFramework client = CuratorFrameworkFactory.newClient (address, new ExponentialBackoffRetry (1000, 3)); client.start () / / create child node watcher listening PathChildrenCache cache = new PathChildrenCache (client, "/ super", true); cache.start (StartMode.POST_INITIALIZED_EVENT) Cache.getListenable () .addListener (new PathChildrenCacheListener () {public void childEvent (CuratorFramework cf, PathChildrenCacheEvent event) throws Exception {System.out.println (event.getType ());}}); Thread.sleep (Long.MAX_VALUE)