API
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
public class ZKAPi {
public static void main(String[] args) throws Exception {
String coon="master:2181,node1:2181,node2:2181";
ZooKeeper zk = new ZooKeeper(coon, 10000, null);
zk.create("/test","test".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT );
}
}
监听
import org.apache.zookeeper.*;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static java.lang.Math.E;
public class ZKAPI01 {
ZooKeeper zk;
@Before
public void init() throws Exception {
zk = new ZooKeeper("master:2181,node1:2181,node2:2181", 3000, null);
}
@Test
public void create() throws KeeperException, InterruptedException {
zk.create("/test01","test01".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
}
@Test
public void children()throws Exception{
zk.getChildren("/test01", new Watcher() {
public void process(WatchedEvent watchedEvent) {
System.out.println("当前节点不存在");
}
});
while (true){}
}
}
|