Search in sources :

Example 46 with CuratorEvent

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

the class SetACLBuilderImpl method performBackgroundOperation.

@Override
public void performBackgroundOperation(final OperationAndData<String> operationAndData) throws Exception {
    try {
        final OperationTrace trace = client.getZookeeperClient().startAdvancedTracer("SetACLBuilderImpl-Background");
        String path = operationAndData.getData();
        client.getZooKeeper().setACL(path, acling.getAclList(path), version, new AsyncCallback.StatCallback() {

            @SuppressWarnings({ "unchecked" })
            @Override
            public void processResult(int rc, String path, Object ctx, Stat stat) {
                trace.setReturnCode(rc).setPath(path).setStat(stat).commit();
                CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.SET_ACL, rc, path, null, ctx, stat, null, null, null, null);
                client.processBackgroundOperation(operationAndData, event);
            }
        }, backgrounding.getContext());
    } catch (Throwable e) {
        backgrounding.checkError(e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) AsyncCallback(org.apache.zookeeper.AsyncCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) OperationTrace(org.apache.curator.drivers.OperationTrace)

Example 47 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent 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 48 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent 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)

Example 49 with CuratorEvent

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

the class CrudExamples method setDataAsync.

public static void setDataAsync(CuratorFramework client, String path, byte[] payload) throws Exception {
    // this is one method of getting event/async notifications
    CuratorListener listener = new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
        // examine event for details
        }
    };
    client.getCuratorListenable().addListener(listener);
    // set data for the given node asynchronously. The completion notification
    // is done via the CuratorListener.
    client.setData().inBackground().forPath(path, payload);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent)

Example 50 with CuratorEvent

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

the class TestLeaderSelectorEdges method createProtectedNodeInBackgroundTest.

/**
 * Create a protected node in background with a retry policy
 */
@Test
public void createProtectedNodeInBackgroundTest() throws Exception {
    final CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryNTimes(2, 1)).connectionTimeoutMs(100).sessionTimeoutMs(60000).build();
    final CountDownLatch latch = new CountDownLatch(1);
    client.start();
    try {
        client.create().forPath(ChaosMonkeyCnxnFactory.CHAOS_ZNODE);
        client.create().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).inBackground(new BackgroundCallback() {

            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                log.info("Receive event {}", event.toString());
                if (event.getResultCode() == KeeperException.Code.CONNECTIONLOSS.intValue()) {
                    latch.countDown();
                }
            }
        }).forPath(ChaosMonkeyCnxnFactory.CHAOS_ZNODE_PREFIX + "foo-");
        Assert.assertTrue(latch.await(30, TimeUnit.SECONDS), "Callback has not been called");
        // Wait for the znode to be deleted
        Thread.sleep(ChaosMonkeyCnxnFactory.LOCKOUT_DURATION_MS * 2);
        // Check that there is no znode
        final int children = client.getChildren().forPath(ChaosMonkeyCnxnFactory.CHAOS_ZNODE).size();
        Assert.assertEquals(children, 0, "Still " + children + " znodes under " + ChaosMonkeyCnxnFactory.CHAOS_ZNODE + " lock");
    } finally {
        client.close();
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Aggregations

CuratorEvent (org.apache.curator.framework.api.CuratorEvent)51 CuratorFramework (org.apache.curator.framework.CuratorFramework)48 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)32 CountDownLatch (java.util.concurrent.CountDownLatch)28 Test (org.testng.annotations.Test)21 CuratorListener (org.apache.curator.framework.api.CuratorListener)18 RetryOneTime (org.apache.curator.retry.RetryOneTime)18 KeeperException (org.apache.zookeeper.KeeperException)12 Test (org.junit.Test)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Watcher (org.apache.zookeeper.Watcher)5 Timing (org.apache.curator.test.Timing)4 WatchedEvent (org.apache.zookeeper.WatchedEvent)4 IOException (java.io.IOException)3 RetryNTimes (org.apache.curator.retry.RetryNTimes)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Message (com.google.protobuf.Message)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2