public static ZooKeeperItf connect(String connectString, int sessionTimeout) throws ZkConnectException { ZooKeeperImpl zooKeeper; try { zooKeeper = new ZooKeeperImpl(connectString, sessionTimeout); } catch (IOException e) { throw new ZkConnectException("Failed to connect with Zookeeper @ '" + connectString + "'", e); } long waitUntil = System.currentTimeMillis() + sessionTimeout; boolean connected = (States.CONNECTED).equals(zooKeeper.getState()); while (!connected && waitUntil > System.currentTimeMillis()) { try { Thread.sleep(100); } catch (InterruptedException e) { connected = (States.CONNECTED).equals(zooKeeper.getState()); break; } connected = (States.CONNECTED).equals(zooKeeper.getState()); } if (!connected) { System.out.println("Failed to connect to Zookeeper within timeout: Dumping stack: "); Thread.dumpStack(); zooKeeper.close(); throw new ZkConnectException("Failed to connect with Zookeeper @ '" + connectString + "' within timeout " + sessionTimeout); } return zooKeeper; }
public static ZooKeeperItf connect(String connectString, int sessionTimeout) throws ZkConnectException { ZooKeeperImpl zooKeeper; try { zooKeeper = new ZooKeeperImpl(connectString, sessionTimeout, new DefaultACLProvider()); } catch (IOException e) { throw new ZkConnectException("Failed to connect with Zookeeper @ '" + connectString + "'", e); } long waitUntil = System.currentTimeMillis() + sessionTimeout; boolean connected = (States.CONNECTED).equals(zooKeeper.getState()); while (!connected && waitUntil > System.currentTimeMillis()) { try { Thread.sleep(100); } catch (InterruptedException e) { connected = (States.CONNECTED).equals(zooKeeper.getState()); break; } connected = (States.CONNECTED).equals(zooKeeper.getState()); } if (!connected) { System.out.println("Failed to connect to Zookeeper within timeout: Dumping stack: "); Thread.dumpStack(); zooKeeper.close(); throw new ZkConnectException("Failed to connect with Zookeeper @ '" + connectString + "' within timeout " + sessionTimeout); } return zooKeeper; }