import org.apache.zookeeper.*;
public class ZooKeeperExample {
private static final String ZOOKEEPER_ADDRESS = "localhost:2181";
private static final int SESSION_TIMEOUT = 5000;
public static void main(String[] args) throws Exception {
ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_ADDRESS, SESSION_TIMEOUT, null);
zooKeeper.create("/example", "Hello, World!".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Watcher watcher = event -> {
if (event.getType() == Watcher.Event.EventType.NodeDataChanged) {
try {
byte[] data = zooKeeper.getData(event.getPath(), false, null);
} catch (Exception e) {
e.printStackTrace();
}
}
};
zooKeeper.getData("/example", watcher, null);
Thread.sleep(Long.MAX_VALUE);
}
}