use of org.apache.curator.framework.api.CuratorListener in project xian by happyyangyuan.
the class TestMultiClient method testNotify.
@Test
public void testNotify() throws Exception {
CuratorFramework client1 = null;
CuratorFramework client2 = null;
try {
client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
client1.start();
client2.start();
final CountDownLatch latch = new CountDownLatch(1);
client1.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.WATCHED) {
if (event.getWatchedEvent().getType() == Watcher.Event.EventType.NodeDataChanged) {
if (event.getPath().equals("/test")) {
latch.countDown();
}
}
}
}
});
client1.create().forPath("/test", new byte[] { 1, 2, 3 });
client1.checkExists().watched().forPath("/test");
client2.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.SYNC) {
client.setData().forPath("/test", new byte[] { 10, 20 });
}
}
});
client2.sync().forPath("/test");
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
} finally {
CloseableUtils.closeQuietly(client1);
CloseableUtils.closeQuietly(client2);
}
}
use of org.apache.curator.framework.api.CuratorListener in project curator by Netflix.
the class CuratorZKClientBridge method connect.
@Override
public void connect(final Watcher watcher) {
if (watcher != null) {
CuratorListener localListener = new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getWatchedEvent() != null) {
watcher.process(event.getWatchedEvent());
}
}
};
curator.getCuratorListenable().addListener(localListener);
listener.set(localListener);
try {
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
WatchedEvent fakeEvent = new WatchedEvent(Watcher.Event.EventType.None, curator.getZookeeperClient().isConnected() ? Watcher.Event.KeeperState.SyncConnected : Watcher.Event.KeeperState.Disconnected, null);
watcher.process(fakeEvent);
}
};
curator.checkExists().inBackground(callback).forPath("/foo");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
use of org.apache.curator.framework.api.CuratorListener in project druid by druid-io.
the class CuratorInventoryManagerTest method testSanity.
@Test
public void testSanity() throws Exception {
final MapStrategy strategy = new MapStrategy();
CuratorInventoryManager<Map<String, Integer>, Integer> manager = new CuratorInventoryManager<>(curator, new StringInventoryManagerConfig("/container", "/inventory"), exec, strategy);
curator.start();
curator.blockUntilConnected();
manager.start();
Assert.assertTrue(Iterables.isEmpty(manager.getInventory()));
CountDownLatch containerLatch = new CountDownLatch(1);
strategy.setNewContainerLatch(containerLatch);
curator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/container/billy", new byte[] {});
Assert.assertTrue(timing.awaitLatch(containerLatch));
strategy.setNewContainerLatch(null);
final Iterable<Map<String, Integer>> inventory = manager.getInventory();
Assert.assertTrue(Iterables.getOnlyElement(inventory).isEmpty());
CountDownLatch inventoryLatch = new CountDownLatch(2);
strategy.setNewInventoryLatch(inventoryLatch);
curator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/1", Ints.toByteArray(100));
curator.create().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/bob", Ints.toByteArray(2287));
Assert.assertTrue(timing.awaitLatch(inventoryLatch));
strategy.setNewInventoryLatch(null);
verifyInventory(manager);
CountDownLatch deleteLatch = new CountDownLatch(1);
strategy.setDeadInventoryLatch(deleteLatch);
curator.delete().forPath("/inventory/billy/1");
Assert.assertTrue(timing.awaitLatch(deleteLatch));
strategy.setDeadInventoryLatch(null);
Assert.assertEquals(1, manager.getInventoryValue("billy").size());
Assert.assertEquals(2287, manager.getInventoryValue("billy").get("bob").intValue());
inventoryLatch = new CountDownLatch(1);
strategy.setNewInventoryLatch(inventoryLatch);
curator.create().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/1", Ints.toByteArray(100));
Assert.assertTrue(timing.awaitLatch(inventoryLatch));
strategy.setNewInventoryLatch(null);
verifyInventory(manager);
final CountDownLatch latch = new CountDownLatch(1);
curator.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) {
if (event.getType() == CuratorEventType.WATCHED && event.getWatchedEvent().getState() == Watcher.Event.KeeperState.Disconnected) {
latch.countDown();
}
}
});
server.stop();
Assert.assertTrue(timing.awaitLatch(latch));
verifyInventory(manager);
// Wait a bit
Thread.sleep(50);
verifyInventory(manager);
}
use of org.apache.curator.framework.api.CuratorListener in project jstorm by alibaba.
the class Zookeeper method mkClient.
/**
* connect ZK, register watchers
*/
public CuratorFramework mkClient(Map conf, List<String> servers, Object port, String root, final WatcherCallBack watcher) {
CuratorFramework 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());
}
}
});
fk.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() {
@Override
public void unhandledError(String msg, Throwable error) {
String errmsg = "Unrecoverable zookeeper error, halting process: " + msg;
LOG.error(errmsg, error);
JStormUtils.halt_process(1, "Unrecoverable zookeeper error");
}
});
fk.start();
return fk;
}
use of org.apache.curator.framework.api.CuratorListener in project dubbo by alibaba.
the class ZKTools method main.
public static void main(String[] args) throws Exception {
client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", 60 * 1000, 60 * 1000, new ExponentialBackoffRetry(1000, 3));
client.start();
client.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
System.out.println("event notification: " + event.getPath());
System.out.println(event);
}
}, executor);
testMigrationRule();
// tesConditionRule();
// testStartupConfig();
// testProviderConfig();
// testPathCache();
// testTreeCache();
// testCuratorListener();
// Thread.sleep(100000);
}
Aggregations