Search in sources :

Example 31 with BackgroundCallback

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

the class TestFramework method testSyncNew.

@Test
public void testSyncNew() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try {
        client.create().forPath("/head");
        Assert.assertNotNull(client.checkExists().forPath("/head"));
        final CountDownLatch latch = new CountDownLatch(1);
        BackgroundCallback callback = new BackgroundCallback() {

            @Override
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.SYNC) {
                    latch.countDown();
                }
            }
        };
        client.sync().inBackground(callback).forPath("/head");
        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) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 32 with BackgroundCallback

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

the class TestFrameworkEdges method connectionLossWithBackgroundTest.

@Test
public void connectionLossWithBackgroundTest() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 1, new RetryOneTime(1));
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        client.start();
        client.getZookeeperClient().blockUntilConnectedOrTimedOut();
        server.close();
        client.getChildren().inBackground(new BackgroundCallback() {

            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                latch.countDown();
            }
        }).forPath("/");
        Assert.assertTrue(timing.awaitLatch(latch));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 33 with BackgroundCallback

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

the class TestFailedDeleteManager method testGuaranteedDeleteOnNonExistentNodeInBackground.

@Test
public void testGuaranteedDeleteOnNonExistentNodeInBackground() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    final AtomicBoolean pathAdded = new AtomicBoolean(false);
    ((CuratorFrameworkImpl) client).getFailedDeleteManager().debugListener = new FailedDeleteManagerListener() {

        @Override
        public void pathAddedForDelete(String path) {
            pathAdded.set(true);
        }
    };
    final CountDownLatch backgroundLatch = new CountDownLatch(1);
    BackgroundCallback background = new BackgroundCallback() {

        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
            backgroundLatch.countDown();
        }
    };
    try {
        client.delete().guaranteed().inBackground(background).forPath("/nonexistent");
        backgroundLatch.await();
        // Exception is expected, the delete should not be retried
        Assert.assertFalse(pathAdded.get());
    } finally {
        client.close();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) FailedDeleteManagerListener(org.apache.curator.framework.imps.FailedDeleteManager.FailedDeleteManagerListener) RetryOneTime(org.apache.curator.retry.RetryOneTime) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 34 with BackgroundCallback

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

the class DistributedQueue method doPutInBackground.

private void doPutInBackground(final T item, String path, final MultiItem<T> givenMultiItem, byte[] bytes) throws Exception {
    BackgroundCallback callback = new BackgroundCallback() {

        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
            if (event.getResultCode() != KeeperException.Code.OK.intValue()) {
                return;
            }
            if (event.getType() == CuratorEventType.CREATE) {
                synchronized (putCount) {
                    putCount.decrementAndGet();
                    putCount.notifyAll();
                }
            }
            putListenerContainer.forEach(new Function<QueuePutListener<T>, Void>() {

                @Override
                public Void apply(QueuePutListener<T> listener) {
                    if (item != null) {
                        listener.putCompleted(item);
                    } else {
                        listener.putMultiCompleted(givenMultiItem);
                    }
                    return null;
                }
            });
        }
    };
    internalCreateNode(path, bytes, callback);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent)

Example 35 with BackgroundCallback

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

the class LeaderLatch method getChildren.

private void getChildren() throws Exception {
    BackgroundCallback callback = new BackgroundCallback() {

        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
            if (event.getResultCode() == KeeperException.Code.OK.intValue()) {
                checkLeadership(event.getChildren());
            }
        }
    };
    client.getChildren().inBackground(callback).forPath(ZKPaths.makePath(latchPath, null));
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent)

Aggregations

BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)37 CuratorFramework (org.apache.curator.framework.CuratorFramework)33 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)33 CountDownLatch (java.util.concurrent.CountDownLatch)18 Test (org.testng.annotations.Test)14 RetryOneTime (org.apache.curator.retry.RetryOneTime)11 KeeperException (org.apache.zookeeper.KeeperException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Watcher (org.apache.zookeeper.Watcher)6 Timing (org.apache.curator.test.Timing)4 Test (org.junit.Test)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 CreateBuilder (org.apache.curator.framework.api.CreateBuilder)3 RetryNTimes (org.apache.curator.retry.RetryNTimes)3 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 Message (com.google.protobuf.Message)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2