19.9.12

A very simple ZooKeeper Connection Test Program

Just to demonstrate how to use ZooKeeper API in a very simple way. Works as a HelloWorld program.

Just Try ZK's API

From:

http://www.slideshare.net/scottleber/apache-zookeeper
Slide 11/76


@Test
public void test() throws Exception {
String hosts = "192.168.119.139:2181";
int sessionTimeout = 100;

final CountDownLatch signal = new CountDownLatch(1);
ZooKeeper zk = new ZooKeeper(hosts, sessionTimeout, new Watcher() {

@Override
public void process(WatchedEvent event) {
if(event.getState() == Watcher.Event.KeeperState.SyncConnected) {
signal.countDown();
}
}

});

signal.await();
zk.close();
}

Another HelloWorld from GitHub


Need to update the following things:
  • dependency for zookeeper. GroupID is org.apache.zookeeper now.
  • add maven-compiler-plugin if you need it.

FAQ: NumberFormatException?

No comments: