-
Notifications
You must be signed in to change notification settings - Fork 484
ZKClient事件注册机制有bug #83
Description
ZkClient在可以将事件通过Listener的方式注册,但是在初始时ZooKeeper连接是单独的线程执行,此时StateListener监听器还没有注册完成,所以Watcher执行时_stateListener变量的size为0,相应的event也就不会被触发。具体如下:
`private final static Logger LOG = LoggerFactory.getLogger(ZkClientTest.class);
private static String CONNECT_STRING = "172.16.93.2:2181,172.16.94.2:2181,172.16.95.2:2181";
private static int TIME_OUT = 5000;
private static String PATH = "/zk-book-zk3";
private ZkClient zkClient = null;
public ZkClientTest(){
this.zkClient = new ZkClient(CONNECT_STRING,TIME_OUT);
this.regListeners(zkClient);
}
private void regListeners(ZkClient zkClient){
LOG.info("--------regListeners start----------");
zkClient.subscribeStateChanges(new IZkStateListener() {
@Override
public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
LOG.info("handleStateChanged state="+state);
}
@Override
public void handleNewSession() throws Exception {
LOG.info(" handleNewSession ");
}
@Override
public void handleSessionEstablishmentError(Throwable error) throws Exception {
LOG.info("handleSessionEstablishmentError error="+error);
}
});
zkClient.subscribeChildChanges(PATH, new IZkChildListener() {
@Override
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
LOG.info("handleChildChange parentPath="+parentPath+","+"childs="+currentChilds);
}
});
zkClient.subscribeDataChanges(PATH, new IZkDataListener() {
@Override
public void handleDataChange(String dataPath, Object data) throws Exception {
LOG.info("handleDataChange dataPath="+dataPath+",data="+data);
}
@Override
public void handleDataDeleted(String dataPath) throws Exception {
LOG.info("handleDataDeleted dataPath="+dataPath);
}
});
LOG.info("--------regListeners end----------");
}`
执行相应的日志如下:
19/11/26 16:52:59 DEBUG [main] (ZkClient.java:1243) - Awaiting connection to Zookeeper server 19/11/26 16:52:59 INFO [main] (ZkClient.java:948) - Waiting for keeper state SyncConnected 19/11/26 16:53:02 DEBUG [main-EventThread] (ZkClient.java:620) - Received event: WatchedEvent state:SyncConnected type:None path:null 19/11/26 16:53:02 INFO [main-EventThread] (ZkClient.java:723) - zookeeper state changed (SyncConnected) 19/11/26 16:53:02 INFO [main-EventThread] (ZkClient.java:725) - zookeeper getShutdownTrigger (false) 19/11/26 16:53:02 DEBUG [main-EventThread] (ZkClient.java:754) - fireStateChangedEvent stateListener 0 19/11/26 16:53:02 DEBUG [main-EventThread] (ZkClient.java:669) - Leaving process event 19/11/26 16:53:02 DEBUG [main] (ZkClient.java:963) - State is SyncConnected 19/11/26 16:53:02 INFO [main] (ZkClientTest.java:37) - --------regListeners start---------- 19/11/26 16:53:02 DEBUG [main] (ZkClient.java:196) - Subscribed data changes for /zk-book-zk3 19/11/26 16:53:02 INFO [main] (ZkClientTest.java:73) - --------regListeners end----------
可以看到
19/11/26 16:53:02 DEBUG [main] (ZkClient.java:963) - State is SyncConnected连接完成才执行监听器的注册。