/**
* watch循环调用观察者模式(默认一次性运行)
*/
@Test
public void testWatch() throws Exception {
final ZooKeeper zk = new ZooKeeper("192.168.xxx.xxx:2181",5000,null);
Stat st = new Stat();
//回调
Watcher w = new Watcher() {
public void process(WatchedEvent watchedEvent) {
try {
System.out.println("修改了!!");
zk.getData("/a/a1",this,null);
} catch (Exception e) {
e.printStackTrace();
}
}
};
byte [] data = zk.getData("/a/a1",w,st);
System.out.println(new String(data));
//观察效果,线程睡眠
while(true){
Thread.sleep(1000);
}
}
结果:
tomsen
修改了!!
修改了!!
修改了!!
修改了!!
Process finished with exit code -1
完成对节点数据变化的实时监控。