public static CountDownLatch connectedSemaphore = new CountDownLatch(1);
public static void main(String[] args) throws Exception { ZooKeeper zk = new ZooKeeper("master:2181", 5000, new ZkExample()); System.out.println(zk.getState()); try { connectedSemaphore.await(); zk.create("/app2", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new IStringCallback(), "This is context"); Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { System.out.println("Zk session established" + e); } }
@Override public void process(WatchedEvent watchedEvent) { System.out.println(watchedEvent); if (Event.KeeperState.SyncConnected == watchedEvent.getState()) { connectedSemaphore.countDown(); } } }
class IStringCallback implements AsyncCallback.StringCallback {
@Override public void processResult(int i, String s, Object o, String s1) { System.out.println("Create path result : [" + i + ", " + s + ", " + o + ", real path name : " + s1); } }