Search in sources :

Example 11 with CuratorListener

use of org.apache.curator.framework.api.CuratorListener in project xian by happyyangyuan.

the class TestMultiClient method testNotify.

@Test
public void testNotify() throws Exception {
    CuratorFramework client1 = null;
    CuratorFramework client2 = null;
    try {
        client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        client1.start();
        client2.start();
        final CountDownLatch latch = new CountDownLatch(1);
        client1.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.WATCHED) {
                    if (event.getWatchedEvent().getType() == Watcher.Event.EventType.NodeDataChanged) {
                        if (event.getPath().equals("/test")) {
                            latch.countDown();
                        }
                    }
                }
            }
        });
        client1.create().forPath("/test", new byte[] { 1, 2, 3 });
        client1.checkExists().watched().forPath("/test");
        client2.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.SYNC) {
                    client.setData().forPath("/test", new byte[] { 10, 20 });
                }
            }
        });
        client2.sync().forPath("/test");
        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    } finally {
        CloseableUtils.closeQuietly(client1);
        CloseableUtils.closeQuietly(client2);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 12 with CuratorListener

use of org.apache.curator.framework.api.CuratorListener in project curator by Netflix.

the class CuratorZKClientBridge method connect.

@Override
public void connect(final Watcher watcher) {
    if (watcher != null) {
        CuratorListener localListener = new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getWatchedEvent() != null) {
                    watcher.process(event.getWatchedEvent());
                }
            }
        };
        curator.getCuratorListenable().addListener(localListener);
        listener.set(localListener);
        try {
            BackgroundCallback callback = new BackgroundCallback() {

                @Override
                public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                    WatchedEvent fakeEvent = new WatchedEvent(Watcher.Event.EventType.None, curator.getZookeeperClient().isConnected() ? Watcher.Event.KeeperState.SyncConnected : Watcher.Event.KeeperState.Disconnected, null);
                    watcher.process(fakeEvent);
                }
            };
            curator.checkExists().inBackground(callback).forPath("/foo");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) KeeperException(org.apache.zookeeper.KeeperException)

Example 13 with CuratorListener

use of org.apache.curator.framework.api.CuratorListener in project druid by druid-io.

the class CuratorInventoryManagerTest method testSanity.

@Test
public void testSanity() throws Exception {
    final MapStrategy strategy = new MapStrategy();
    CuratorInventoryManager<Map<String, Integer>, Integer> manager = new CuratorInventoryManager<>(curator, new StringInventoryManagerConfig("/container", "/inventory"), exec, strategy);
    curator.start();
    curator.blockUntilConnected();
    manager.start();
    Assert.assertTrue(Iterables.isEmpty(manager.getInventory()));
    CountDownLatch containerLatch = new CountDownLatch(1);
    strategy.setNewContainerLatch(containerLatch);
    curator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/container/billy", new byte[] {});
    Assert.assertTrue(timing.awaitLatch(containerLatch));
    strategy.setNewContainerLatch(null);
    final Iterable<Map<String, Integer>> inventory = manager.getInventory();
    Assert.assertTrue(Iterables.getOnlyElement(inventory).isEmpty());
    CountDownLatch inventoryLatch = new CountDownLatch(2);
    strategy.setNewInventoryLatch(inventoryLatch);
    curator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/1", Ints.toByteArray(100));
    curator.create().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/bob", Ints.toByteArray(2287));
    Assert.assertTrue(timing.awaitLatch(inventoryLatch));
    strategy.setNewInventoryLatch(null);
    verifyInventory(manager);
    CountDownLatch deleteLatch = new CountDownLatch(1);
    strategy.setDeadInventoryLatch(deleteLatch);
    curator.delete().forPath("/inventory/billy/1");
    Assert.assertTrue(timing.awaitLatch(deleteLatch));
    strategy.setDeadInventoryLatch(null);
    Assert.assertEquals(1, manager.getInventoryValue("billy").size());
    Assert.assertEquals(2287, manager.getInventoryValue("billy").get("bob").intValue());
    inventoryLatch = new CountDownLatch(1);
    strategy.setNewInventoryLatch(inventoryLatch);
    curator.create().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/1", Ints.toByteArray(100));
    Assert.assertTrue(timing.awaitLatch(inventoryLatch));
    strategy.setNewInventoryLatch(null);
    verifyInventory(manager);
    final CountDownLatch latch = new CountDownLatch(1);
    curator.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework client, CuratorEvent event) {
            if (event.getType() == CuratorEventType.WATCHED && event.getWatchedEvent().getState() == Watcher.Event.KeeperState.Disconnected) {
                latch.countDown();
            }
        }
    });
    server.stop();
    Assert.assertTrue(timing.awaitLatch(latch));
    verifyInventory(manager);
    // Wait a bit
    Thread.sleep(50);
    verifyInventory(manager);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.Test)

Example 14 with CuratorListener

use of org.apache.curator.framework.api.CuratorListener in project jstorm by alibaba.

the class Zookeeper method mkClient.

/**
 * connect ZK, register watchers
 */
public CuratorFramework mkClient(Map conf, List<String> servers, Object port, String root, final WatcherCallBack watcher) {
    CuratorFramework fk = Utils.newCurator(conf, servers, port, root);
    fk.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework _fk, CuratorEvent e) throws Exception {
            if (e.getType().equals(CuratorEventType.WATCHED)) {
                WatchedEvent event = e.getWatchedEvent();
                watcher.execute(event.getState(), event.getType(), event.getPath());
            }
        }
    });
    fk.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() {

        @Override
        public void unhandledError(String msg, Throwable error) {
            String errmsg = "Unrecoverable zookeeper error, halting process: " + msg;
            LOG.error(errmsg, error);
            JStormUtils.halt_process(1, "Unrecoverable zookeeper error");
        }
    });
    fk.start();
    return fk;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 15 with CuratorListener

use of org.apache.curator.framework.api.CuratorListener in project dubbo by alibaba.

the class ZKTools method main.

public static void main(String[] args) throws Exception {
    client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", 60 * 1000, 60 * 1000, new ExponentialBackoffRetry(1000, 3));
    client.start();
    client.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
            System.out.println("event notification: " + event.getPath());
            System.out.println(event);
        }
    }, executor);
    testMigrationRule();
// tesConditionRule();
// testStartupConfig();
// testProviderConfig();
// testPathCache();
// testTreeCache();
// testCuratorListener();
// Thread.sleep(100000);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)23 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)23 CuratorListener (org.apache.curator.framework.api.CuratorListener)23 CountDownLatch (java.util.concurrent.CountDownLatch)16 RetryOneTime (org.apache.curator.retry.RetryOneTime)9 KeeperException (org.apache.zookeeper.KeeperException)9 Test (org.testng.annotations.Test)9 Test (org.junit.Test)6 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 IOException (java.io.IOException)2 Map (java.util.Map)2 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)2 BindException (java.net.BindException)1 UnknownHostException (java.net.UnknownHostException)1 TreeMap (java.util.TreeMap)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)1 UnhandledErrorListener (org.apache.curator.framework.api.UnhandledErrorListener)1 CuratorOp (org.apache.curator.framework.api.transaction.CuratorOp)1 CuratorTransactionResult (org.apache.curator.framework.api.transaction.CuratorTransactionResult)1