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);
}
}
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);
}
}
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();
}
}
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);
}
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));
}
Aggregations