|
@@ -32,6 +32,8 @@ import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.NoSuchElementException;
|
|
import java.util.NoSuchElementException;
|
|
|
|
+import java.util.concurrent.CountDownLatch;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
import java.util.regex.Pattern;
|
|
import java.util.stream.Stream;
|
|
import java.util.stream.Stream;
|
|
@@ -68,6 +70,7 @@ public class ZooKeeperMain {
|
|
|
|
|
|
protected ZooKeeper zk;
|
|
protected ZooKeeper zk;
|
|
protected String host = "";
|
|
protected String host = "";
|
|
|
|
+ private CountDownLatch connectLatch = null;
|
|
|
|
|
|
public boolean getPrintWatches() {
|
|
public boolean getPrintWatches() {
|
|
return printWatches;
|
|
return printWatches;
|
|
@@ -106,6 +109,13 @@ public class ZooKeeperMain {
|
|
ZooKeeperMain.printMessage("WATCHER::");
|
|
ZooKeeperMain.printMessage("WATCHER::");
|
|
ZooKeeperMain.printMessage(event.toString());
|
|
ZooKeeperMain.printMessage(event.toString());
|
|
}
|
|
}
|
|
|
|
+ if (connectLatch != null) {
|
|
|
|
+ // connection success
|
|
|
|
+ if (event.getType() == Event.EventType.None
|
|
|
|
+ && event.getState() == Event.KeeperState.SyncConnected) {
|
|
|
|
+ connectLatch.countDown();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
@@ -168,6 +178,8 @@ public class ZooKeeperMain {
|
|
options.put("readonly", "true");
|
|
options.put("readonly", "true");
|
|
} else if (opt.equals("-client-configuration")) {
|
|
} else if (opt.equals("-client-configuration")) {
|
|
options.put("client-configuration", it.next());
|
|
options.put("client-configuration", it.next());
|
|
|
|
+ } else if (opt.equals("-waitforconnection")) {
|
|
|
|
+ options.put("waitforconnection", "true");
|
|
}
|
|
}
|
|
} catch (NoSuchElementException e) {
|
|
} catch (NoSuchElementException e) {
|
|
System.err.println("Error: no argument found for option " + opt);
|
|
System.err.println("Error: no argument found for option " + opt);
|
|
@@ -261,7 +273,19 @@ public class ZooKeeperMain {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- zk = new ZooKeeperAdmin(host, Integer.parseInt(cl.getOption("timeout")), new MyWatcher(), readOnly, clientConfig);
|
|
|
|
|
|
+ if (cl.getOption("waitforconnection") != null) {
|
|
|
|
+ connectLatch = new CountDownLatch(1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int timeout = Integer.parseInt(cl.getOption("timeout"));
|
|
|
|
+ zk = new ZooKeeperAdmin(host, timeout, new MyWatcher(), readOnly, clientConfig);
|
|
|
|
+ if (connectLatch != null) {
|
|
|
|
+ if (!connectLatch.await(timeout, TimeUnit.MILLISECONDS)) {
|
|
|
|
+ zk.close();
|
|
|
|
+ throw new IOException(KeeperException.create(KeeperException.Code.CONNECTIONLOSS));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws IOException, InterruptedException {
|
|
public static void main(String[] args) throws IOException, InterruptedException {
|