Search in sources :

Example 6 with CuratorListener

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

the class TestFramework method testBackgroundDeleteWithChildren.

@Test
public void testBackgroundDeleteWithChildren() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try {
        client.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.DELETE) {
                    Assert.assertEquals(event.getPath(), "/one/two");
                    ((CountDownLatch) event.getContext()).countDown();
                }
            }
        });
        client.create().creatingParentsIfNeeded().forPath("/one/two/three/four");
        Assert.assertNotNull(client.checkExists().forPath("/one/two/three/four"));
        CountDownLatch latch = new CountDownLatch(1);
        client.delete().deletingChildrenIfNeeded().inBackground(latch).forPath("/one/two");
        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
        Assert.assertNull(client.checkExists().forPath("/one/two"));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
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) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 7 with CuratorListener

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

the class TestFramework method testNamespaceInBackground.

@Test
public void testNamespaceInBackground() throws Exception {
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
    CuratorFramework client = builder.connectString(server.getConnectString()).namespace("aisa").retryPolicy(new RetryOneTime(1)).build();
    client.start();
    try {
        final BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
        CuratorListener listener = new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.EXISTS) {
                    queue.put(event.getPath());
                }
            }
        };
        client.getCuratorListenable().addListener(listener);
        client.create().forPath("/base");
        client.checkExists().inBackground().forPath("/base");
        String path = queue.poll(10, TimeUnit.SECONDS);
        Assert.assertEquals(path, "/base");
        client.getCuratorListenable().removeListener(listener);
        BackgroundCallback callback = new BackgroundCallback() {

            @Override
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                queue.put(event.getPath());
            }
        };
        client.getChildren().inBackground(callback).forPath("/base");
        path = queue.poll(10, TimeUnit.SECONDS);
        Assert.assertEquals(path, "/base");
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.testng.annotations.Test)

Example 8 with CuratorListener

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

the class TestFramework method testBackgroundGetDataWithWatch.

@Test
public void testBackgroundGetDataWithWatch() throws Exception {
    final byte[] data1 = { 1, 2, 3 };
    final byte[] data2 = { 4, 5, 6, 7 };
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try {
        final CountDownLatch watchedLatch = new CountDownLatch(1);
        client.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.GET_DATA) {
                    Assert.assertEquals(event.getPath(), "/test");
                    Assert.assertEquals(event.getData(), data1);
                    ((CountDownLatch) event.getContext()).countDown();
                } else if (event.getType() == CuratorEventType.WATCHED) {
                    if (event.getWatchedEvent().getType() == Watcher.Event.EventType.NodeDataChanged) {
                        Assert.assertEquals(event.getPath(), "/test");
                        watchedLatch.countDown();
                    }
                }
            }
        });
        client.create().forPath("/test", data1);
        CountDownLatch backgroundLatch = new CountDownLatch(1);
        client.getData().watched().inBackground(backgroundLatch).forPath("/test");
        Assert.assertTrue(backgroundLatch.await(10, TimeUnit.SECONDS));
        client.setData().forPath("/test", data2);
        Assert.assertTrue(watchedLatch.await(10, TimeUnit.SECONDS));
        byte[] checkData = client.getData().forPath("/test");
        Assert.assertEquals(checkData, data2);
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
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) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 9 with CuratorListener

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

the class TestFramework method testSync.

@Test
public void testSync() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try {
        client.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.SYNC) {
                    Assert.assertEquals(event.getPath(), "/head");
                    ((CountDownLatch) event.getContext()).countDown();
                }
            }
        });
        client.create().forPath("/head");
        Assert.assertNotNull(client.checkExists().forPath("/head"));
        CountDownLatch latch = new CountDownLatch(1);
        client.sync("/head", latch);
        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
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) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 10 with CuratorListener

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

the class TestFrameworkEdges method testNestedCalls.

@Test
public void testNestedCalls() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        client.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.EXISTS) {
                    Stat stat = client.checkExists().forPath("/yo/yo/yo");
                    Assert.assertNull(stat);
                    client.create().inBackground(event.getContext()).forPath("/what");
                } else if (event.getType() == CuratorEventType.CREATE) {
                    ((CountDownLatch) event.getContext()).countDown();
                }
            }
        });
        CountDownLatch latch = new CountDownLatch(1);
        client.checkExists().inBackground(latch).forPath("/hey");
        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Stat(org.apache.zookeeper.data.Stat) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

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