Zookeeper Demo
1770 ワード
package com.example.demo.resource;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
public class ZookeeperTest {
static final String address = "192.168.1.24:2181,192.168.1.25:2181,192.168.1.26:2181";
static final int session_timeout = 5000;
static final CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper(address,session_timeout,new Watcher() {
@Override
public void process(WatchedEvent event) {
KeeperState keeperState = event.getState();
EventType eventType = event.getType();
if(KeeperState.SyncConnected == keeperState) {
if(EventType.None == eventType) {
countDownLatch.countDown();
System.out.println("zk ");
}
}
}
});
countDownLatch.await();
//
String returnStr = zk.create("/test", "hello".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(returnStr);
//
System.out.println(zk.getData("/test", false, null).toString());
//
zk.delete("/test", -1,new AsyncCallback.VoidCallback() {
@Override
public void processResult(int rc, String path, Object ctx) {
System.out.println(rc);
System.out.println(path);
System.out.println(ctx);
}
}, "a");
Thread.sleep(5000);
zk.close();
}
}