public void createNode(String data) { try { if (zk.exists(znode, this) != null) { System.out.println(" exists znode " + znode); try { zk.setData(znode, data.getBytes(), -1); } catch (KeeperException | InterruptedException e) { e.printStackTrace(); } } else { System.out.println("not exists znode " + znode); zk.create(znode, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } catch (KeeperException | InterruptedException e1) { e1.printStackTrace(); } }
public void consume() { while (true) { if (zooKeeper != null) { synchronized (mutex) { try { List<String> children = zooKeeper.getChildren(root, true); if (children.size() == 0) { mutex.wait(); } else { Stat stat = new Stat(); String minElement = root + "/" + min(children); String value = new String(zooKeeper.getData(minElement, false, stat)); System.out.println(value); zooKeeper.delete(minElement, stat.getVersion()); } } catch (KeeperException | InterruptedException e) { e.printStackTrace(); } } } } }