use of org.apache.zookeeper.WatchedEvent 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.zookeeper.WatchedEvent in project yyl_example by Relucent.
the class Test1 method main.
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper("localhost:2181", 1000, new Watcher() {
// 监控所有被触发的事件
public void process(WatchedEvent event) {
System.out.println("已经触发了" + event.getType() + "事件!");
}
});
// 创建一个目录节点
zk.create("/testRootPath", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// 创建一个子目录节点
zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(new String(zk.getData("/testRootPath", false, null)));
// 取出子目录节点列表
System.out.println(zk.getChildren("/testRootPath", true));
// 修改子目录节点数据
zk.setData("/testRootPath/testChildPathOne", "modifyChildDataOne".getBytes(), -1);
System.out.println("目录节点状态:[" + zk.exists("/testRootPath", true) + "]");
// 创建另外一个子目录节点
zk.create("/testRootPath/testChildPathTwo", "testChildDataTwo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(new String(zk.getData("/testRootPath/testChildPathTwo", true, null)));
// 删除子目录节点
zk.delete("/testRootPath/testChildPathTwo", -1);
zk.delete("/testRootPath/testChildPathOne", -1);
// 删除父目录节点
zk.delete("/testRootPath", -1);
// 关闭连接
zk.close();
}
use of org.apache.zookeeper.WatchedEvent in project yyl_example by Relucent.
the class Test3 method main.
public static void main(String[] args) {
ZooKeeper zk = null;
try {
zk = new ZooKeeper("localhost:2181", 1000, null);
zk.exists("/test", new Watcher() {
@Override
public void process(WatchedEvent e) {
System.out.println(e);
}
});
Thread.sleep(60 * 1000);
} catch (Exception e) {
e.printStackTrace();
try {
if (zk != null) {
zk.close();
}
} catch (Exception e1) {
}
}
}
use of org.apache.zookeeper.WatchedEvent in project distributedlog by twitter.
the class LocalDLMEmulator method connectZooKeeper.
public static ZooKeeper connectZooKeeper(String zkHost, int zkPort, int zkTimeoutSec) throws IOException, KeeperException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final String zkHostPort = zkHost + ":" + zkPort;
ZooKeeper zkc = new ZooKeeper(zkHostPort, zkTimeoutSec * 1000, new Watcher() {
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.SyncConnected) {
latch.countDown();
}
}
});
if (!latch.await(zkTimeoutSec, TimeUnit.SECONDS)) {
throw new IOException("Zookeeper took too long to connect");
}
return zkc;
}
use of org.apache.zookeeper.WatchedEvent in project distributedlog by twitter.
the class ZooKeeperClient method registerExpirationHandler.
/**
* Clients that need to re-establish state after session expiration can register an
* {@code onExpired} command to execute.
*
* @param onExpired the {@code Command} to register
* @return the new {@link Watcher} which can later be passed to {@link #unregister} for
* removal.
*/
public Watcher registerExpirationHandler(final ZooKeeperSessionExpireNotifier onExpired) {
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == EventType.None && event.getState() == KeeperState.Expired) {
try {
onExpired.notifySessionExpired();
} catch (Exception exc) {
// do nothing
}
}
}
};
register(watcher);
return watcher;
}
Aggregations