Class ZooKeeperBuilder

java.lang.Object
org.apache.zookeeper.client.ZooKeeperBuilder

@Public @Evolving public class ZooKeeperBuilder extends Object
Builder to construct ZooKeeper and ZooKeeperAdmin.
  • Constructor Details

  • Method Details

    • withDefaultWatcher

      public ZooKeeperBuilder withDefaultWatcher(Watcher watcher)
      Specified watcher to receive state changes, and node events if attached later.
      Parameters:
      watcher - a watcher object which will be notified of state changes, may also be notified for node events
      Returns:
      this
    • withHostProvider

      public ZooKeeperBuilder withHostProvider(Function<Collection<InetSocketAddress>,HostProvider> hostProvider)
      Specifies a function to construct a HostProvider with initial server addresses from connect string.
      Parameters:
      hostProvider - use this as HostProvider to enable custom behaviour.
      Returns:
      this
    • withCanBeReadOnly

      public ZooKeeperBuilder withCanBeReadOnly(boolean canBeReadOnly)
      Specifies whether the created client is allowed to go to read-only mode in case of partitioning.
      Parameters:
      canBeReadOnly - whether the created client is allowed to go to read-only mode in case of partitioning. Read-only mode basically means that if the client can't find any majority servers but there's partitioned server it could reach, it connects to one in read-only mode, i.e. read requests are allowed while write requests are not. It continues seeking for majority in the background.
      Returns:
      this
      Since:
      3.4
    • withSession

      public ZooKeeperBuilder withSession(long sessionId, byte[] sessionPasswd)
      Specifies session id and password in session reestablishment.
      Parameters:
      sessionId - session id to use if reconnecting, otherwise 0 to open new session
      sessionPasswd - password for this session
      Returns:
      this
      See Also:
    • withNewSessionTimeout

      public ZooKeeperBuilder withNewSessionTimeout(Duration timeout)
      Specifies timeout to establish a brand-new session.
      Parameters:
      timeout - timeout to get Watcher.Event.KeeperState.Expired in establishing a brand-new session. Duration.ofSeconds(Long.MAX_VALUE, 999_999_999L), which is the default, means endless retry until connected. Duration.ZERO means a sensible value deduced from specified session timeout, currently, it is approximate sessionTimeout * 4 / 3.
      Returns:
      this
      Since:
      3.10.0
    • withClientConfig

      public ZooKeeperBuilder withClientConfig(ZKClientConfig clientConfig)
      Specifies the client config used to construct ZooKeeper instances.
      Parameters:
      clientConfig - passing this conf object gives each client the flexibility of configuring properties differently compared to other instances
      Returns:
      this
      Since:
      3.5.2
    • toOptions

      @Private public ZooKeeperOptions toOptions()
      Creates a ZooKeeperOptions with configured options.

      This is private and export for internal usage.

    • build

      public ZooKeeper build() throws IOException
      Constructs an instance of ZooKeeper.
      Returns:
      an instance of ZooKeeper
      Throws:
      IOException - from constructor of ZooKeeper
    • buildAdmin

      public ZooKeeperAdmin buildAdmin() throws IOException
      Constructs an instance of ZooKeeperAdmin.
      Returns:
      an instance of ZooKeeperAdmin
      Throws:
      IOException - from constructor of ZooKeeperAdmin