@@ -418,6 +418,25 @@ public boolean isConnected() {
418418 }
419419 }
420420
421+ /**
422+ * Creates a builder with given connect string and session timeout.
423+ *
424+ * @param connectString
425+ * comma separated host:port pairs, each corresponding to a zk
426+ * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
427+ * If the optional chroot suffix is used the example would look
428+ * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a"
429+ * where the client would be rooted at "/app/a" and all paths
430+ * would be relative to this root - ie getting/setting/etc...
431+ * "/foo/bar" would result in operations being run on
432+ * "/app/a/foo/bar" (from the server perspective).
433+ * @param sessionTimeout
434+ * session timeout
435+ */
436+ public static ZooKeeperBuilder builder (String connectString , Duration sessionTimeout ) {
437+ return new ZooKeeperBuilder (connectString , sessionTimeout );
438+ }
439+
421440 /**
422441 * To create a ZooKeeper client object, the application needs to pass a
423442 * connection string containing a comma separated list of host:port pairs,
@@ -460,9 +479,11 @@ public boolean isConnected() {
460479 * in cases of network failure
461480 * @throws IllegalArgumentException
462481 * if an invalid chroot path is specified
482+ *
483+ * @see #builder(String, Duration) for builder style construction
463484 */
464485 public ZooKeeper (String connectString , int sessionTimeout , Watcher watcher ) throws IOException {
465- this (new ZooKeeperBuilder (connectString , Duration .ofMillis (sessionTimeout ))
486+ this (builder (connectString , Duration .ofMillis (sessionTimeout ))
466487 .withDefaultWatcher (watcher )
467488 .toOptions ());
468489 }
@@ -511,13 +532,15 @@ public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) thro
511532 * in cases of network failure
512533 * @throws IllegalArgumentException
513534 * if an invalid chroot path is specified
535+ *
536+ * @see #builder(String, Duration) for builder style construction
514537 */
515538 public ZooKeeper (
516539 String connectString ,
517540 int sessionTimeout ,
518541 Watcher watcher ,
519542 ZKClientConfig conf ) throws IOException {
520- this (new ZooKeeperBuilder (connectString , Duration .ofMillis (sessionTimeout ))
543+ this (builder (connectString , Duration .ofMillis (sessionTimeout ))
521544 .withDefaultWatcher (watcher )
522545 .withClientConfig (conf )
523546 .toOptions ());
@@ -579,14 +602,16 @@ public ZooKeeper(
579602 * in cases of network failure
580603 * @throws IllegalArgumentException
581604 * if an invalid chroot path is specified
605+ *
606+ * @see #builder(String, Duration) for builder style construction
582607 */
583608 public ZooKeeper (
584609 String connectString ,
585610 int sessionTimeout ,
586611 Watcher watcher ,
587612 boolean canBeReadOnly ,
588613 HostProvider aHostProvider ) throws IOException {
589- this (new ZooKeeperBuilder (connectString , Duration .ofMillis (sessionTimeout ))
614+ this (builder (connectString , Duration .ofMillis (sessionTimeout ))
590615 .withDefaultWatcher (watcher )
591616 .withCanBeReadOnly (canBeReadOnly )
592617 .withHostProvider (ignored -> aHostProvider )
@@ -651,6 +676,8 @@ public ZooKeeper(
651676 * in cases of network failure
652677 * @throws IllegalArgumentException
653678 * if an invalid chroot path is specified
679+ *
680+ * @see #builder(String, Duration) for builder style construction
654681 */
655682 public ZooKeeper (
656683 String connectString ,
@@ -660,7 +687,7 @@ public ZooKeeper(
660687 HostProvider hostProvider ,
661688 ZKClientConfig clientConfig
662689 ) throws IOException {
663- this (new ZooKeeperBuilder (connectString , Duration .ofMillis (sessionTimeout ))
690+ this (builder (connectString , Duration .ofMillis (sessionTimeout ))
664691 .withDefaultWatcher (watcher )
665692 .withCanBeReadOnly (canBeReadOnly )
666693 .withHostProvider (ignored -> hostProvider )
@@ -740,13 +767,15 @@ ClientCnxn createConnection(
740767 * in cases of network failure
741768 * @throws IllegalArgumentException
742769 * if an invalid chroot path is specified
770+ *
771+ * @see #builder(String, Duration) for builder style construction
743772 */
744773 public ZooKeeper (
745774 String connectString ,
746775 int sessionTimeout ,
747776 Watcher watcher ,
748777 boolean canBeReadOnly ) throws IOException {
749- this (new ZooKeeperBuilder (connectString , Duration .ofMillis (sessionTimeout ))
778+ this (builder (connectString , Duration .ofMillis (sessionTimeout ))
750779 .withDefaultWatcher (watcher )
751780 .withCanBeReadOnly (canBeReadOnly )
752781 .toOptions ());
@@ -805,14 +834,16 @@ public ZooKeeper(
805834 * in cases of network failure
806835 * @throws IllegalArgumentException
807836 * if an invalid chroot path is specified
837+ *
838+ * @see #builder(String, Duration) for builder style construction
808839 */
809840 public ZooKeeper (
810841 String connectString ,
811842 int sessionTimeout ,
812843 Watcher watcher ,
813844 boolean canBeReadOnly ,
814845 ZKClientConfig conf ) throws IOException {
815- this (new ZooKeeperBuilder (connectString , Duration .ofMillis (sessionTimeout ))
846+ this (builder (connectString , Duration .ofMillis (sessionTimeout ))
816847 .withDefaultWatcher (watcher )
817848 .withCanBeReadOnly (canBeReadOnly )
818849 .withClientConfig (conf )
@@ -870,14 +901,16 @@ public ZooKeeper(
870901 * @throws IOException in cases of network failure
871902 * @throws IllegalArgumentException if an invalid chroot path is specified
872903 * @throws IllegalArgumentException for an invalid list of ZooKeeper hosts
904+ *
905+ * @see #builder(String, Duration) for builder style construction
873906 */
874907 public ZooKeeper (
875908 String connectString ,
876909 int sessionTimeout ,
877910 Watcher watcher ,
878911 long sessionId ,
879912 byte [] sessionPasswd ) throws IOException {
880- this (new ZooKeeperBuilder (connectString , Duration .ofMillis (sessionTimeout ))
913+ this (builder (connectString , Duration .ofMillis (sessionTimeout ))
881914 .withDefaultWatcher (watcher )
882915 .withSession (sessionId , sessionPasswd )
883916 .toOptions ());
@@ -946,6 +979,8 @@ public ZooKeeper(
946979 * use this as HostProvider to enable custom behaviour.
947980 * @throws IOException in cases of network failure
948981 * @throws IllegalArgumentException if an invalid chroot path is specified
982+ *
983+ * @see #builder(String, Duration) for builder style construction
949984 */
950985 public ZooKeeper (
951986 String connectString ,
@@ -955,7 +990,7 @@ public ZooKeeper(
955990 byte [] sessionPasswd ,
956991 boolean canBeReadOnly ,
957992 HostProvider aHostProvider ) throws IOException {
958- this (new ZooKeeperBuilder (connectString , Duration .ofMillis (sessionTimeout ))
993+ this (builder (connectString , Duration .ofMillis (sessionTimeout ))
959994 .withDefaultWatcher (watcher )
960995 .withSession (sessionId , sessionPasswd )
961996 .withCanBeReadOnly (canBeReadOnly )
@@ -1031,6 +1066,8 @@ public ZooKeeper(
10311066 * @throws IllegalArgumentException if an invalid chroot path is specified
10321067 *
10331068 * @since 3.5.5
1069+ *
1070+ * @see #builder(String, Duration) for builder style construction
10341071 */
10351072 public ZooKeeper (
10361073 String connectString ,
@@ -1041,7 +1078,7 @@ public ZooKeeper(
10411078 boolean canBeReadOnly ,
10421079 HostProvider hostProvider ,
10431080 ZKClientConfig clientConfig ) throws IOException {
1044- this (new ZooKeeperBuilder (connectString , Duration .ofMillis (sessionTimeout ))
1081+ this (builder (connectString , Duration .ofMillis (sessionTimeout ))
10451082 .withSession (sessionId , sessionPasswd )
10461083 .withDefaultWatcher (watcher )
10471084 .withCanBeReadOnly (canBeReadOnly )
@@ -1053,6 +1090,8 @@ public ZooKeeper(
10531090 /**
10541091 * Create a ZooKeeper client and establish session asynchronously.
10551092 *
1093+ * <p>This is private and export for internal usage.
1094+ *
10561095 * <p>This constructor will initiate connection to the server and return
10571096 * immediately - potentially (usually) before the session is fully established.
10581097 * The watcher from options will be notified of any changes in state. This
@@ -1180,6 +1219,8 @@ public ZooKeeper(ZooKeeperOptions options) throws IOException {
11801219 * majority in the background.
11811220 * @throws IOException in cases of network failure
11821221 * @throws IllegalArgumentException if an invalid chroot path is specified
1222+ *
1223+ * @see #builder(String, Duration) for builder style construction
11831224 */
11841225 public ZooKeeper (
11851226 String connectString ,
@@ -1188,7 +1229,7 @@ public ZooKeeper(
11881229 long sessionId ,
11891230 byte [] sessionPasswd ,
11901231 boolean canBeReadOnly ) throws IOException {
1191- this (new ZooKeeperBuilder (connectString , Duration .ofMillis (sessionTimeout ))
1232+ this (builder (connectString , Duration .ofMillis (sessionTimeout ))
11921233 .withDefaultWatcher (watcher )
11931234 .withSession (sessionId , sessionPasswd )
11941235 .withCanBeReadOnly (canBeReadOnly )
0 commit comments