List of usage examples for org.apache.zookeeper Watcher Watcher
Watcher
From source file:com.jbrisbin.vpc.zk.GroovyZooKeeperHelper.java
License:Apache License
/** * Get a node's data and set a watch./*from www . j a v a2s . c o m*/ * * @param path * @param watcher * @param stat * @return * @throws InterruptedException * @throws KeeperException */ public Object getData(Object path, final Closure watcher, Stat stat) throws InterruptedException, KeeperException { return deserialize(zookeeper.getData(getPathAsString(path), new Watcher() { public void process(WatchedEvent event) { watcher.call(event); } }, stat)); }
From source file:com.jkoolcloud.tnt4j.streams.configure.zookeeper.ZKConfigManager.java
License:Apache License
/** * Initializes ZK ensemble node data monitoring (over {@link org.apache.zookeeper.Watcher}) and initial data * loading./*from w w w. java 2s. c om*/ * * @param zk * ZooKeeper instance * @param path * node path * @param zkCfgChangeListener * zookeeper node data change listener instance */ public static void handleZKStoredConfiguration(final ZooKeeper zk, final String path, final ZKConfigChangeListener zkCfgChangeListener) { Watcher watch = new Watcher() { @Override public void process(WatchedEvent watchedEvent) { if (path.equals(watchedEvent.getPath())) { if (watchedEvent.getType() == Event.EventType.NodeDataChanged) { LOGGER.log(OpLevel.DEBUG, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME, "ZKConfigManager.node.changed"), path); zkCfgChangeListener.reconfigure(zk, path, this); } else if (watchedEvent.getType() == Event.EventType.NodeDeleted) { LOGGER.log(OpLevel.DEBUG, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME, "ZKConfigManager.node.deleted"), path); zk.exists(path, this, null, null); } else if (watchedEvent.getType() == Event.EventType.NodeCreated) { LOGGER.log(OpLevel.DEBUG, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME, "ZKConfigManager.node.created"), path); zkCfgChangeListener.reconfigure(zk, path, this); } } } }; Stat nStat = null; try { nStat = zk.exists(path, false); } catch (Exception exc) { LOGGER.log(OpLevel.WARNING, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME, "ZKConfigManager.node.exists.failed"), path, exc); } if (nStat == null) { LOGGER.log(OpLevel.DEBUG, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME, "ZKConfigManager.node.create.wait"), path); zk.exists(path, watch, null, null); } else { zkCfgChangeListener.reconfigure(zk, path, watch); } }
From source file:com.jkoolcloud.tnt4j.streams.configure.zookeeper.ZKConnection.java
License:Apache License
/** * Connects to ZooKeeper ensemble. Waits until connection establishment is confirmed over watcher. * * @param connStr// ww w. j a v a2s. co m * ZooKeeper ensemble connection definition string * @param timeout * connection timeout * @return zookeeper instance * * @throws IOException * if I/O exception occurs while initializing ZooKeeper connection * @throws InterruptedException * if the current thread is interrupted while waiting */ public ZooKeeper connect(String connStr, int timeout) throws IOException, InterruptedException { final CountDownLatch connectedSignal = new CountDownLatch(1); zk = new ZooKeeper(connStr, timeout, new Watcher() { @Override public void process(WatchedEvent we) { if (we.getState() == Event.KeeperState.SyncConnected) { connectedSignal.countDown(); } } }); connectedSignal.await(); return zk; }
From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java
License:Apache License
@Test public void testSymlinkWithExistWatch() throws InterruptedException, ExecutionException { final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); final AsyncCallback.StatCallback existCallback = new AsyncCallback.StatCallback() { @Override/*from w ww . j a va2s .c o m*/ public void processResult(int rc, String path, Object ctx, Stat stat) { KeeperException.Code result = KeeperException.Code.get(rc); Assert.assertEquals(result, KeeperException.Code.OK); latch.countDown(); } }; Watcher existWatch = new Watcher() { @Override public void process(WatchedEvent event) { Assert.assertEquals(event.getType(), Event.EventType.NodeCreated); _zkClient.getZooKeeper().exists(event.getPath(), null, existCallback, null); } }; AsyncCallback.StatCallback existCallback2 = new AsyncCallback.StatCallback() { @Override public void processResult(int rc, String path, Object ctx, Stat stat) { KeeperException.Code result = KeeperException.Code.get(rc); Assert.assertEquals(result, KeeperException.Code.NONODE); latch2.countDown(); } }; // symlink: /foo/$link/newNode -> /foo/bar/newNode _zkClient.getZooKeeper().exists("/foo/$link/newNode", existWatch, existCallback2, null); latch2.await(30, TimeUnit.SECONDS); _zkClient.ensurePersistentNodeExists("/foo/bar/newNode", new FutureCallback<None>()); latch.await(30, TimeUnit.SECONDS); _zkClient.removeNodeUnsafe("/foo/bar/newNode", new FutureCallback<None>()); }
From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java
License:Apache License
@Test public void testSymlinkWithExistWatch2() throws InterruptedException, ExecutionException { final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); final AsyncCallback.StatCallback existCallback = new AsyncCallback.StatCallback() { @Override/*from ww w . ja v a2 s.c om*/ public void processResult(int rc, String path, Object ctx, Stat stat) { KeeperException.Code result = KeeperException.Code.get(rc); Assert.assertEquals(result, KeeperException.Code.OK); latch.countDown(); } }; Watcher existWatch = new Watcher() { @Override public void process(WatchedEvent event) { Assert.assertEquals(event.getType(), Event.EventType.NodeDataChanged); _zkClient.getZooKeeper().exists(event.getPath(), null, existCallback, null); } }; AsyncCallback.StatCallback existCallback2 = new AsyncCallback.StatCallback() { @Override public void processResult(int rc, String path, Object ctx, Stat stat) { KeeperException.Code result = KeeperException.Code.get(rc); Assert.assertEquals(result, KeeperException.Code.NONODE); latch2.countDown(); } }; // symlink: /foo/$link/foo -> /foo/bar/foo, which doesn't exist _zkClient.getZooKeeper().exists("/foo/$link/foo", existWatch, existCallback2, null); latch2.await(30, TimeUnit.SECONDS); // update symlink. now it points to /bar/foo, which does exist. _zkClient.setSymlinkData("/foo/$link", "/bar", new FutureCallback<None>()); latch.await(30, TimeUnit.SECONDS); // restore symlink _zkClient.setSymlinkData("/foo/$link", "/foo/bar", new FutureCallback<None>()); }
From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java
License:Apache License
@Test public void testSymlinkWithExistWatch3() throws InterruptedException, ExecutionException { final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); final AsyncCallback.StatCallback existCallback = new AsyncCallback.StatCallback() { @Override/* w w w . j a va 2 s.c om*/ public void processResult(int rc, String path, Object ctx, Stat stat) { KeeperException.Code result = KeeperException.Code.get(rc); Assert.assertEquals(result, KeeperException.Code.OK); latch.countDown(); } }; Watcher existWatch = new Watcher() { @Override public void process(WatchedEvent event) { Assert.assertEquals(event.getType(), Event.EventType.NodeCreated); _zkClient.getZooKeeper().exists(event.getPath(), null, existCallback, null); } }; AsyncCallback.StatCallback existCallback2 = new AsyncCallback.StatCallback() { @Override public void processResult(int rc, String path, Object ctx, Stat stat) { KeeperException.Code result = KeeperException.Code.get(rc); Assert.assertEquals(result, KeeperException.Code.NONODE); latch2.countDown(); } }; // symlink /$link doesn't exist. _zkClient.getZooKeeper().exists("/$link", existWatch, existCallback2, null); latch2.await(30, TimeUnit.SECONDS); // create symlink /$link -> /foo/bar. existWatch should be notified. _zkClient.createSymlink("/$link", "/foo/bar", new FutureCallback<None>()); latch.await(30, TimeUnit.SECONDS); // delete symlink /$link _zkClient.removeNodeUnsafe("/$link", new FutureCallback<None>()); }
From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java
License:Apache License
@Test public void testSymlinkWithChildrenWatch() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); final AsyncCallback.ChildrenCallback childrenCallback = new AsyncCallback.ChildrenCallback() { @Override/*from ww w . jav a2 s . com*/ public void processResult(int rc, String path, Object ctx, List<String> children) { KeeperException.Code result = KeeperException.Code.get(rc); Assert.assertEquals(result, KeeperException.Code.OK); Assert.assertEquals(children.size(), 11); latch.countDown(); } }; Watcher childrenWatch = new Watcher() { @Override public void process(WatchedEvent event) { Assert.assertEquals(event.getType(), Event.EventType.NodeChildrenChanged); _zkClient.getZooKeeper().getChildren(event.getPath(), null, childrenCallback, null); } }; AsyncCallback.ChildrenCallback childrenCallback2 = new AsyncCallback.ChildrenCallback() { @Override public void processResult(int rc, String path, Object ctx, List<String> children) { KeeperException.Code result = KeeperException.Code.get(rc); Assert.assertEquals(result, KeeperException.Code.OK); latch2.countDown(); } }; // symlink: /foo/$link -> /foo/bar _zkClient.getZooKeeper().getChildren("/foo/$link", childrenWatch, childrenCallback2, null); latch2.await(30, TimeUnit.SECONDS); _zkClient.ensurePersistentNodeExists("/foo/bar/newNode", new FutureCallback<None>()); latch.await(30, TimeUnit.SECONDS); _zkClient.removeNodeUnsafe("/foo/bar/newNode", new FutureCallback<None>()); }
From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java
License:Apache License
@Test public void testSymlinkWithChildrenWatcher2() throws ExecutionException, InterruptedException { final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); final AsyncCallback.ChildrenCallback callback2 = new AsyncCallback.ChildrenCallback() { @Override/*w ww .ja va2 s .co m*/ public void processResult(int rc, String path, Object ctx, List<String> children) { Assert.assertEquals(path, "/foo/$link"); Assert.assertEquals(children.size(), 5); latch2.countDown(); } }; Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { Assert.assertEquals(event.getType(), Event.EventType.NodeChildrenChanged); _zkClient.getZooKeeper().getChildren(event.getPath(), null, callback2, null); } }; AsyncCallback.ChildrenCallback callback = new AsyncCallback.ChildrenCallback() { @Override public void processResult(int rc, String path, Object ctx, List<String> children) { latch1.countDown(); } }; // set watcher to /foo/$link _zkClient.getZooKeeper().getChildren("/foo/$link", watcher, callback, null); latch1.await(30, TimeUnit.SECONDS); // update symlink _zkClient.setSymlinkData("/foo/$link", "/bar/foo", new FutureCallback<None>()); latch2.await(30, TimeUnit.SECONDS); FutureCallback<None> fcb = new FutureCallback<None>(); // restore symlink _zkClient.setSymlinkData("/foo/$link", "/foo/bar", fcb); fcb.get(); }
From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java
License:Apache License
@Test public void testSymlinkWithChildrenWatcher3() throws ExecutionException, InterruptedException, KeeperException { final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); Stat expectedStat1 = _zkClient.getZooKeeper().exists("/foo/bar", false); Stat expectedStat2 = _zkClient.getZooKeeper().exists("/bar/foo", false); final AsyncCallback.Children2Callback callback2 = new AsyncCallback.Children2Callback() { @Override//from w w w. j a va 2s. c o m public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) { Assert.assertEquals(path, "/foo/$link"); Assert.assertEquals(children.size(), 5); Assert.assertEquals(stat, expectedStat2); latch2.countDown(); } }; Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { Assert.assertEquals(event.getType(), Event.EventType.NodeChildrenChanged); _zkClient.getZooKeeper().getChildren(event.getPath(), null, callback2, null); } }; AsyncCallback.Children2Callback callback = new AsyncCallback.Children2Callback() { @Override public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) { Assert.assertEquals(stat, expectedStat1); latch1.countDown(); } }; // set watcher to /foo/$link _zkClient.getZooKeeper().getChildren("/foo/$link", watcher, callback, null); latch1.await(30, TimeUnit.SECONDS); // update symlink _zkClient.setSymlinkData("/foo/$link", "/bar/foo", new FutureCallback<None>()); latch2.await(30, TimeUnit.SECONDS); FutureCallback<None> fcb = new FutureCallback<None>(); // restore symlink _zkClient.setSymlinkData("/foo/$link", "/foo/bar", fcb); fcb.get(); }
From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java
License:Apache License
@Test public void testInvalidSymlinkWithExists() throws ExecutionException, InterruptedException { final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); final AsyncCallback.StatCallback callback = new AsyncCallback.StatCallback() { @Override/*from w w w . ja va 2 s . c o m*/ public void processResult(int rc, String path, Object ctx, Stat stat) { Assert.assertEquals(path, "/foo/$link"); KeeperException.Code result = KeeperException.Code.get(rc); Assert.assertEquals(result, KeeperException.Code.NONODE); latch1.countDown(); } }; final Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { Assert.assertEquals(event.getType(), Event.EventType.NodeDataChanged); latch2.countDown(); } }; FutureCallback<None> fcb = new FutureCallback<None>(); _zkClient.setSymlinkData("/foo/$link", "INVALID", fcb); fcb.get(); _zkClient.getZooKeeper().exists("/foo/$link", watcher, callback, null); latch1.await(30, TimeUnit.SECONDS); _zkClient.setSymlinkData("/foo/$link", "/foo/bar", fcb); if (!latch2.await(30, TimeUnit.SECONDS)) { Assert.fail("Exists Watch is not triggered"); } }