public static ZkClient getZkClient(String zkServer)
{
// happy path that we cache the zkclient and it's still connected
if (_zkClientMap.containsKey(zkServer))
{
ZkClient zkClient = _zkClientMap.get(zkServer);
if (zkClient.getConnection().getZookeeperState() == States.CONNECTED)
{
return zkClient;
}
}
synchronized (_zkClientMap)
{
// if we cache a stale zkclient, purge it
if (_zkClientMap.containsKey(zkServer))
{
ZkClient zkClient = _zkClientMap.get(zkServer);
if (zkClient.getConnection().getZookeeperState() != States.CONNECTED)
{
_zkClientMap.remove(zkServer);
}
}
// get a new zkclient
if (!_zkClientMap.containsKey(zkServer))
{
ZkClient zkClient = new ZkClient(zkServer, DEFAULT_SESSION_TIMEOUT,
ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
_zkClientMap.put(zkServer, zkClient);
}
return _zkClientMap.get(zkServer);