use of org.apache.curator.framework.api.CuratorListener in project druid by druid-io.
the class AnnouncerTest method testSessionKilled.
@Test(timeout = 60_000L)
public void testSessionKilled() throws Exception {
curator.start();
curator.blockUntilConnected();
Announcer announcer = new Announcer(curator, exec);
try {
curator.inTransaction().create().forPath("/somewhere").and().commit();
announcer.start();
final byte[] billy = "billy".getBytes();
final String testPath1 = "/test1";
final String testPath2 = "/somewhere/test2";
final Set<String> paths = Sets.newHashSet(testPath1, testPath2);
announcer.announce(testPath1, billy);
announcer.announce(testPath2, billy);
Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1));
Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath2));
final CountDownLatch latch = new CountDownLatch(1);
curator.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.CREATE) {
paths.remove(event.getPath());
if (paths.isEmpty()) {
latch.countDown();
}
}
}
});
KillSession.kill(curator.getZookeeperClient().getZooKeeper(), server.getConnectString());
Assert.assertTrue(timing.forWaiting().awaitLatch(latch));
Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1));
Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath2));
announcer.stop();
while ((curator.checkExists().forPath(testPath1) != null) || (curator.checkExists().forPath(testPath2) != null)) {
Thread.sleep(100);
}
Assert.assertNull(curator.checkExists().forPath(testPath1));
Assert.assertNull(curator.checkExists().forPath(testPath2));
} finally {
announcer.stop();
}
}
use of org.apache.curator.framework.api.CuratorListener in project storm by apache.
the class Zookeeper method mkClientImpl.
public CuratorFramework mkClientImpl(Map conf, List<String> servers, Object port, String root, final WatcherCallBack watcher, Map authConf) {
CuratorFramework fk;
if (authConf != null) {
fk = Utils.newCurator(conf, servers, port, root, new ZookeeperAuthInfo(authConf));
} else {
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());
}
}
});
LOG.info("Staring ZK Curator");
fk.start();
return fk;
}
use of org.apache.curator.framework.api.CuratorListener in project xian by happyyangyuan.
the class TestFramework method testBackgroundCreate.
@Test
public void testBackgroundCreate() 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.CREATE) {
Assert.assertEquals(event.getPath(), "/test");
((CountDownLatch) event.getContext()).countDown();
}
}
});
CountDownLatch latch = new CountDownLatch(1);
client.create().inBackground(latch).forPath("/test", new byte[] { 1, 2, 3 });
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.api.CuratorListener in project xian by happyyangyuan.
the class TestBlockUntilConnected method testBlockUntilConnectedSessionExpired.
/**
* Test that we got disconnected before calling blockUntilConnected and we reconnect we receive a session expired event.
*/
@Test
public void testBlockUntilConnectedSessionExpired() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build();
final CountDownLatch lostLatch = new CountDownLatch(1);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
lostLatch.countDown();
}
}
});
final CountDownLatch expiredLatch = new CountDownLatch(1);
client.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.WATCHED && event.getWatchedEvent().getState() == Watcher.Event.KeeperState.Expired) {
expiredLatch.countDown();
}
}
});
ConnectionStateAccessor.setDebugWaitOnExpiredForClient(client);
try {
client.start();
// Block until we're connected
Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Failed to connect");
final long sessionTimeoutMs = client.getZookeeperClient().getConnectionTimeoutMs();
// Kill the server
CloseableUtils.closeQuietly(server);
// Wait until we hit the lost state
Assert.assertTrue(timing.awaitLatch(lostLatch), "Failed to reach LOST state");
Thread.sleep(sessionTimeoutMs);
server = new TestingServer(server.getPort(), server.getTempDirectory());
// Wait until we get expired event
Assert.assertTrue(timing.awaitLatch(expiredLatch), "Failed to get Expired event");
final boolean blockUntilConnected5Seconds = client.blockUntilConnected(5, TimeUnit.SECONDS);
Assert.assertTrue(client.getZookeeperClient().isConnected(), "ConnectionState.isConnected returned false");
Assert.assertTrue(blockUntilConnected5Seconds, "BlockUntilConnected returned false");
} catch (Exception e) {
Assert.fail("Unexpected exception " + e);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.api.CuratorListener 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);
}
Aggregations