Let's start off by creating a minimal ZooKeeper client:
import java.io.*;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.*;
public class SimpleZoo {
static class MyWatcher implements Watcher {
public void process(WatchedEvent event) {
System.err.println(event);
}
}
public static void main(String[] args) throws Exception {
MyWatcher myWatcher = new MyWatcher();
ZooKeeper zk = new ZooKeeper(args[0], 10000, myWatcher);
Thread.sleep(10000);
zk.close();
}
}What events do you get when you start?
Lets create a program to create a znode (I'll use "/motd", but you can pick your own name). You'll need to add:
zk.create("/motd", "hi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);What happens if you try to create /motd twice?
Create a new simple client to set "/motd":
zk.setData("/motd", "new data".getBytes(), -1);Create a new simple client to read "/motd".
Stat stat = new Stat();
byte data[] = zk.getData("/motd", true, stat);
System.out.println(new String(data));Try changing /data and watch the events you get. What if you change /data twice in a row? Could this be used for messaging?