Hi,
I am facing one issue in my project when I am creating zookepper client and
if its not created due to zookepper server url is down. It's keep on trying
to connect and further sending the "Watcher" object mentioning the state is
"disconnected". I am not sure how to kill this endless callback if I want
to pass new zookeeper URL with is up.
Here is the code which I have written.
Class ZookeeperConnection {
final String zkConnectionStr;
public void ZookeeperConnection(String zkConnectionStr ){
Zookeeper zkConnectionStr =zkConnectionStr;
}
private ZooKeeper newZkConnection() {
CountDownLatch zkConnectionLatch = new CountDownLatch(1);
try {
ZooKeeper zkClient = new ZooKeeper(zkConnectionStr, 40000, new
Watcher() {
int i = 0;
@Override
public void process(WatchedEvent watchedEvent) {
//
String zkSessionID = getZkSessionID();
//
switch (watchedEvent.getState()) {
case ConnectedReadOnly:
break;
case SyncConnected:
// Wait for connection to ZooKeeper
zkConnectionLatch.countDown();
break;
case Disconnected:
break;
case Expired:
restartZkServices(); //calling the same new
connection method again
break;
case AuthFailed:
break;
}
}
});
//
long zkConnectionTimeout = 30;
zkConnectionLatch.await(zkConnectionTimeout, TimeUnit.SECONDS);
// Wait 30 seconds
//
if (zkClient.getState().isConnected()) {
return zkClient;
}
//
} catch (Exception ex) {
LOG.error("An exception happened during connecting to ZooKeeper
[{}].", zkConnectionStr, ex);
}
//
return null;
}
In this code I tried to create object of ZookeeperConnection class by
passing the zookeeper server URL and latter if user want to change the
zookepper server URL we again create a new method of ZookeeperConnection
class so if Zookeeeper is up I am able to connect through new object. But
old object which uses old zookepper url is still active and gets callback
and continuously show disconnected.
Please help me on this.
Thanks.
Devendra Jain
|