public class ZKDataListener implements IZkDataListener {
public void handleDataChange(String dataPath, Object data) throws Exception {
System.out.println(dataPath+":"+data.toString());
}
public void handleDataDeleted(String dataPath) throws Exception {
System.out.println(dataPath);
}
}
public class ZkTest {
public static void main(String[] args) throws InterruptedException {
ZkClient zkClient = new ZkClient("127.0.0.1:2181");
zkClient.setZkSerializer(new MyZkSerializer());
zkClient.subscribeDataChanges("/test", new ZKDataListener());
zkClient.writeData("/test", 1);
Thread.sleep(3000);
zkClient.writeData("/test", 2);
Thread.sleep(3000);
zkClient.writeData("/test", 3);
while(true) {}
}
}
setdata以后不休眠的话,只会有一次回调,休眠正常回调三次。
大佬知道神马原因吗?