List of usage examples for org.apache.zookeeper Watcher Watcher
Watcher
From source file:com.nearinfinity.blur.utils.BlurUtil.java
License:Apache License
public static String lockForSafeMode(ZooKeeper zookeeper, String nodeName, String cluster) throws KeeperException, InterruptedException { LOG.info("Getting safe mode lock."); final Object lock = new Object(); String blurSafemodePath = ZookeeperPathConstants.getSafemodePath(cluster); String newPath = zookeeper.create(blurSafemodePath + "/safemode-", nodeName.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); Watcher watcher = new Watcher() { @Override/* w w w . ja v a2 s. co m*/ public void process(WatchedEvent event) { synchronized (lock) { lock.notifyAll(); } } }; while (true) { synchronized (lock) { List<String> children = new ArrayList<String>(zookeeper.getChildren(blurSafemodePath, watcher)); Collections.sort(children); if (newPath.equals(blurSafemodePath + "/" + children.get(0))) { LOG.info("Lock aquired."); return newPath; } else { lock.wait(BlurConstants.ZK_WAIT_TIME); } } } }
From source file:com.nearinfinity.blur.zookeeper.ZkUtils.java
License:Apache License
public static void waitUntilExists(ZooKeeper zooKeeper, String path) { final Object o = new Object(); try {/*w w w . j a va2 s . c o m*/ while (true) { Stat stat = zooKeeper.exists(path, new Watcher() { @Override public void process(WatchedEvent event) { synchronized (o) { o.notifyAll(); } } }); if (stat == null) { synchronized (o) { o.wait(); } } else { return; } } } catch (KeeperException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } }
From source file:com.nearinfinity.blur.zookeeper.ZkUtilsTest.java
License:Apache License
@Before public void setUp() throws IOException, InterruptedException { final Object lock = new Object(); synchronized (lock) { _zooKeeper = new ZooKeeper("127.0.0.1:10101", 10000, new Watcher() { @Override/*from www.j a v a2s . c om*/ public void process(WatchedEvent event) { synchronized (lock) { lock.notifyAll(); } } }); lock.wait(); } }
From source file:com.netflix.curator.BasicTests.java
License:Apache License
@Test public void testExpiredSession() throws Exception { // see http://wiki.apache.org/hadoop/ZooKeeper/FAQ#A4 final Timing timing = new Timing(); final CountDownLatch latch = new CountDownLatch(1); Watcher watcher = new Watcher() { @Override//from w w w. j a v a 2 s . c o m public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.Expired) { latch.countDown(); } } }; final CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), watcher, new RetryOneTime(2)); client.start(); try { final AtomicBoolean firstTime = new AtomicBoolean(true); RetryLoop.callWithRetry(client, new Callable<Object>() { @Override public Object call() throws Exception { if (firstTime.compareAndSet(true, false)) { try { client.getZooKeeper().create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException ignore) { // ignore } KillSession.kill(client.getZooKeeper(), server.getConnectString()); Assert.assertTrue(timing.awaitLatch(latch)); } ZooKeeper zooKeeper = client.getZooKeeper(); client.blockUntilConnectedOrTimedOut(); Assert.assertNotNull(zooKeeper.exists("/foo", false)); return null; } }); } finally { client.close(); } }
From source file:com.netflix.curator.CuratorZookeeperClient.java
License:Apache License
void internalBlockUntilConnectedOrTimedOut() throws InterruptedException { long waitTimeMs = connectionTimeoutMs; while (!state.isConnected() && (waitTimeMs > 0)) { final CountDownLatch latch = new CountDownLatch(1); Watcher tempWatcher = new Watcher() { @Override//from w ww .j a va 2 s . c o m public void process(WatchedEvent event) { latch.countDown(); } }; state.addParentWatcher(tempWatcher); long startTimeMs = System.currentTimeMillis(); try { latch.await(1, TimeUnit.SECONDS); } finally { state.removeParentWatcher(tempWatcher); } long elapsed = Math.max(1, System.currentTimeMillis() - startTimeMs); waitTimeMs -= elapsed; } }
From source file:com.netflix.curator.framework.imps.CuratorFrameworkImpl.java
License:Apache License
public CuratorFrameworkImpl(CuratorFrameworkFactory.Builder builder) { this.client = new CuratorZookeeperClient(builder.getZookeeperFactory(), builder.getEnsembleProvider(), builder.getSessionTimeoutMs(), builder.getConnectionTimeoutMs(), new Watcher() { @Override//w w w .ja va2 s .c o m public void process(WatchedEvent watchedEvent) { CuratorEvent event = new CuratorEventImpl(CuratorFrameworkImpl.this, CuratorEventType.WATCHED, watchedEvent.getState().getIntValue(), unfixForNamespace(watchedEvent.getPath()), null, null, null, null, null, watchedEvent, null); processEvent(event); } }, builder.getRetryPolicy()); listeners = new ListenerContainer<CuratorListener>(); unhandledErrorListeners = new ListenerContainer<UnhandledErrorListener>(); backgroundOperations = new DelayQueue<OperationAndData<?>>(); namespace = new NamespaceImpl(this, builder.getNamespace()); threadFactory = getThreadFactory(builder); connectionStateManager = new ConnectionStateManager(this, builder.getThreadFactory()); compressionProvider = builder.getCompressionProvider(); aclProvider = builder.getAclProvider(); namespaceFacadeCache = new NamespaceFacadeCache(this); byte[] builderDefaultData = builder.getDefaultData(); defaultData = (builderDefaultData != null) ? Arrays.copyOf(builderDefaultData, builderDefaultData.length) : new byte[0]; if (builder.getAuthScheme() != null) { authInfo.set(new AuthInfo(builder.getAuthScheme(), builder.getAuthValue())); } failedDeleteManager = new FailedDeleteManager(this); }
From source file:com.netflix.curator.framework.imps.KillSession.java
License:Apache License
static void kill(String connectString, long sessionId, byte[] sessionPassword) throws Exception { final CountDownLatch zkLatch = new CountDownLatch(1); Watcher zkWatcher = new Watcher() { @Override/*w ww.java 2 s. com*/ public void process(WatchedEvent event) { zkLatch.countDown(); } }; ZooKeeper zk = new ZooKeeper(connectString, 10000, zkWatcher, sessionId, sessionPassword); try { Assert.assertTrue(zkLatch.await(10, TimeUnit.SECONDS)); } finally { zk.close(); // this should cause a session error in the main client } }
From source file:com.netflix.curator.framework.imps.TestFramework.java
License:Apache License
@Test public void testIt() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();/*from w ww .j a va 2 s . c om*/ Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("yep"); } }; client.checkExists().usingWatcher(watcher).forPath("/hey"); server.close(); Thread.sleep(10000); }
From source file:com.netflix.curator.framework.imps.TestFramework.java
License:Apache License
@Test public void testNamespaceWithWatcher() throws Exception { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); CuratorFramework client = builder.connectString(server.getConnectString()).namespace("aisa") .retryPolicy(new RetryOneTime(1)).build(); client.start();/*from w ww . ja va 2 s. co m*/ try { final BlockingQueue<String> queue = new LinkedBlockingQueue<String>(); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { try { queue.put(event.getPath()); } catch (InterruptedException e) { throw new Error(e); } } }; client.create().forPath("/base"); client.getChildren().usingWatcher(watcher).forPath("/base"); client.create().forPath("/base/child"); String path = queue.take(); Assert.assertEquals(path, "/base"); } finally { client.close(); } }
From source file:com.netflix.curator.framework.imps.TestFrameworkEdges.java
License:Apache License
@Test public void testSessionKilled() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();/* w w w . j av a2s. c o m*/ try { client.create().forPath("/sessionTest"); final AtomicBoolean sessionDied = new AtomicBoolean(false); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.Expired) { sessionDied.set(true); } } }; client.checkExists().usingWatcher(watcher).forPath("/sessionTest"); KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString()); Assert.assertNotNull(client.checkExists().forPath("/sessionTest")); Assert.assertTrue(sessionDied.get()); } finally { client.close(); } }