import org.apache.zookeeper.*;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;
import java.util.concurrent.CountDownLatch;
class DataMonitor implements Watcher, AsyncCallback.StatCallback{
public ZooKeeper zk = null;
private int i = 0;
public int iexistcheck = 0;
public DataMonitor(){
}
public void process(WatchedEvent event) {
String path = event.getPath();
System.out.println("process: iexistcheck="+iexistcheck+" - "+"i:="+i+",path:"+path+" - state:" + event.getState());
i++;
}
public void processResult(int rc, String path, Object ctx, Stat stat) {
System.out.println("processResult.1:iexistcheck="+iexistcheck+" - "+"i:="+i+",rc:" + rc + " - path:"+path+" - state:" + stat);
try {
System.out.println("processResult.2.getData:iexistcheck="+iexistcheck+" - "+"i:="+i+" - "+new String(zk.getData("/test", false, null)));
}catch (Exception e){
System.out.print("processResult.3:exception.....iexistcheck="+iexistcheck+" - "+"i:="+i);
}finally {
System.out.println("processResult.4:iexistcheck="+iexistcheck+" - "+"i:="+i+",end......end......end..");
}
}
}
public class ZooKeeperTest {
private static CountDownLatch downLatch = new CountDownLatch(1);
private static final int TIME_OUT = 300000;
private static final String HOST = "localhost:2181";
public static void main(String[] args) throws Exception{
DataMonitor dm = new DataMonitor();
ZooKeeper zookeeper = new ZooKeeper(HOST, TIME_OUT, dm);
dm.zk = zookeeper;
zookeeper.exists("/test", true, dm, dm);
Stat stat = null;
for(int i = 0; i < 30; i++){
String buf = "value="+i;
System.out.println("loop i="+i);
if(zookeeper.exists("/test", false) == null)
{
//PERSISTENT_SEQUENTIAL模式情况下,真正路径是从返回值获取。其格式为/test0000000028。
//EPHEMERAL模式下,该节点是临时节点,当超时或session断开时,它也跟随着消失。适合检验机器岩机与否等状态。防单点或执备切换等。
String node_real = zookeeper.create("/test", "znode1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("nodename:"+node_real);
}
//第三个参数为版本号,当前输入的版本号与目标版本的版本号是相同的情况,才能更新到数据。否则会更新失败。
if(stat == null){
stat = zookeeper.setData(node_real, buf.getBytes() , -1);
}else{
stat = zookeeper.setData(node_real, buf.getBytes() , stat.getVersion());
}
System.out.println("Czxid:" + stat.getCzxid() + " - Mzxid:" + stat.getMzxid() + " - Version:" + stat.getVersion());
if(i > 5){
if(i % 3 == 0){
dm.iexistcheck = i;
zookeeper.exists("/test", true, dm, dm);
}
}
Thread.sleep(1000);
}
zookeeper.delete("/test", -1);
Thread.sleep(5000);
zookeeper.close();
}
}
org.apache.zookeeper
zookeeper
3.4.8
com.sun.jmx
jmxri
com.sun.jdmk
jmxtools
javax.jms
jms
loop i=0
process: iexistcheck=0 - i:=0,path:null - state:SyncConnected
processResult.1:iexistcheck=0 - i:=1,rc:-101 - path:/test - state:null
processResult.3:exception.....iexistcheck=0 - i:=1processResult.4:iexistcheck=0 - i:=1,end......end......end..
process: iexistcheck=0 - i:=1,path:/test - state:SyncConnected
Czxid:1055 - Mzxid:1056 - Version:1
loop i=1
Czxid:1055 - Mzxid:1057 - Version:2
loop i=2
Czxid:1055 - Mzxid:1058 - Version:3
loop i=3
Czxid:1055 - Mzxid:1059 - Version:4
loop i=4
Czxid:1055 - Mzxid:1060 - Version:5
loop i=5
Czxid:1055 - Mzxid:1061 - Version:6
loop i=6
Czxid:1055 - Mzxid:1062 - Version:7
processResult.1:iexistcheck=6 - i:=1,rc:0 - path:/test0000000029 - state:1055,1062,1480343483194,1480343489294,7,0,0,0,7,0,1055
processResult.2.getData:iexistcheck=6 - i:=1 - value=6
processResult.4:iexistcheck=6 - i:=1,end......end......end..
loop i=7
process: iexistcheck=6 - i:=1,path:/test0000000029 - state:SyncConnected
Czxid:1055 - Mzxid:1063 - Version:8
loop i=8
Czxid:1055 - Mzxid:1064 - Version:9
loop i=9
Czxid:1055 - Mzxid:1065 - Version:10
processResult.1:iexistcheck=9 - i:=2,rc:0 - path:/test0000000029 - state:1055,1065,1480343483194,1480343492344,10,0,0,0,7,0,1055
processResult.2.getData:iexistcheck=9 - i:=2 - value=9
processResult.4:iexistcheck=9 - i:=2,end......end......end..