Package com.mongodb

Class MongoClientOptions.Builder

java.lang.Object
com.mongodb.MongoClientOptions.Builder
Enclosing class:
MongoClientOptions

@NotThreadSafe public static class MongoClientOptions.Builder extends Object
A builder for MongoClientOptions so that MongoClientOptions can be immutable, and to support easier construction through chaining.
Since:
2.10.0
  • Constructor Details

    • Builder

      public Builder()
      Creates a Builder for MongoClientOptions.
    • Builder

      public Builder(MongoClientOptions options)
      Creates a Builder from an existing MongoClientOptions.
      Parameters:
      options - create a builder from existing options
  • Method Details

    • applicationName

      public MongoClientOptions.Builder applicationName(@Nullable String applicationName)
      Sets the logical name of the application using this MongoClient. The application name may be used by the client to identify the application to the server, for use in server logs, slow query logs, and profile collection.
      Parameters:
      applicationName - the logical name of the application using this MongoClient. It may be null. The UTF-8 encoding may not exceed 128 bytes.
      Returns:
      this
      Since:
      3.4
      See Also:
      Since server release
      3.4
    • compressorList

      public MongoClientOptions.Builder compressorList(List<com.mongodb.MongoCompressor> compressorList)
      Sets the compressors to use for compressing messages to the server. The driver will use the first compressor in the list that the server is configured to support.
      Parameters:
      compressorList - the list of compressors to request
      Returns:
      this
      Since:
      3.6
      See Also:
      Since server release
      3.4
    • minConnectionsPerHost

      public MongoClientOptions.Builder minConnectionsPerHost(int minConnectionsPerHost)
      Sets the minimum number of connections per host.
      Parameters:
      minConnectionsPerHost - minimum number of connections
      Returns:
      this
      Throws:
      IllegalArgumentException - if minConnectionsPerHost < 0
      Since:
      2.12
      See Also:
    • connectionsPerHost

      public MongoClientOptions.Builder connectionsPerHost(int connectionsPerHost)
      Sets the maximum number of connections per host.
      Parameters:
      connectionsPerHost - the maximum size of the connection pool per host; if 0, then there is no limit.
      Returns:
      this
      Throws:
      IllegalArgumentException - if connectionsPerHost < 0
      See Also:
    • serverSelectionTimeout

      public MongoClientOptions.Builder serverSelectionTimeout(int serverSelectionTimeout)

      Sets the server selection timeout in milliseconds, which defines how long the driver will wait for server selection to succeed before throwing an exception.

      A value of 0 means that it will timeout immediately if no server is available. A negative value means to wait indefinitely.

      Parameters:
      serverSelectionTimeout - the server selection timeout, in milliseconds
      Returns:
      this
      See Also:
    • maxWaitTime

      public MongoClientOptions.Builder maxWaitTime(int maxWaitTime)
      Sets the maximum time that a thread will block waiting for a connection.
      Parameters:
      maxWaitTime - the maximum wait time, in milliseconds
      Returns:
      this
      See Also:
    • maxConnectionIdleTime

      public MongoClientOptions.Builder maxConnectionIdleTime(int maxConnectionIdleTime)
      Sets the maximum idle time for a pooled connection.
      Parameters:
      maxConnectionIdleTime - the maximum idle time, in milliseconds, which must be >= 0. A zero value indicates no limit to the life time.
      Returns:
      this
      Throws:
      IllegalArgumentException - if maxConnectionIdleTime < 0
      Since:
      2.12
      See Also:
    • maxConnectionLifeTime

      public MongoClientOptions.Builder maxConnectionLifeTime(int maxConnectionLifeTime)
      Sets the maximum life time for a pooled connection.
      Parameters:
      maxConnectionLifeTime - the maximum life time, in milliseconds, which must be >= 0. A zero value indicates no limit to the life time.
      Returns:
      this
      Throws:
      IllegalArgumentException - if maxConnectionLifeTime < 0
      Since:
      2.12
      See Also:
    • maxConnecting

      public MongoClientOptions.Builder maxConnecting(int maxConnecting)
      Sets the maximum number of connections a pool may be establishing concurrently.
      Parameters:
      maxConnecting - The maximum number of connections a pool may be establishing concurrently. Must be positive.
      Returns:
      this.
      Since:
      4.4
      See Also:
    • maintenanceInitialDelay

      public MongoClientOptions.Builder maintenanceInitialDelay(long maintenanceInitialDelay)
      The period of time to wait before running the first maintenance job on each connection pool.
      Parameters:
      maintenanceInitialDelay - the time period to wait in milliseconds
      Returns:
      this.
      Since:
      4.7
      See Also:
      • ConnectionPoolSettings.Builder.maintenanceInitialDelay(long, java.util.concurrent.TimeUnit)
    • maintenanceFrequency

      public MongoClientOptions.Builder maintenanceFrequency(long maintenanceFrequency)
      The time period between runs of the maintenance job on each connection pool.
      Parameters:
      maintenanceFrequency - the time period between runs of the maintenance job in milliseconds
      Returns:
      this
      Since:
      4.7
      See Also:
      • ConnectionPoolSettings.Builder.maintenanceFrequency(long, java.util.concurrent.TimeUnit)
    • connectTimeout

      public MongoClientOptions.Builder connectTimeout(int connectTimeout)
      Sets the connection timeout.
      Parameters:
      connectTimeout - the connection timeout, in milliseconds, which must be > 0
      Returns:
      this
      Throws:
      IllegalArgumentException - if connectTimeout <= 0
      See Also:
    • socketTimeout

      public MongoClientOptions.Builder socketTimeout(int socketTimeout)
      Sets the socket timeout.
      Parameters:
      socketTimeout - the socket timeout, in milliseconds
      Returns:
      this
      See Also:
    • sslEnabled

      public MongoClientOptions.Builder sslEnabled(boolean sslEnabled)
      Sets whether to use SSL.
      Parameters:
      sslEnabled - set to true if using SSL
      Returns:
      this
      Since:
      3.0
      See Also:
    • sslInvalidHostNameAllowed

      public MongoClientOptions.Builder sslInvalidHostNameAllowed(boolean sslInvalidHostNameAllowed)
      Define whether invalid host names should be allowed. Defaults to false. Take care before setting this to true, as it makes the application susceptible to man-in-the-middle attacks.
      Parameters:
      sslInvalidHostNameAllowed - whether invalid host names are allowed in SSL certificates.
      Returns:
      this
    • sslContext

      public MongoClientOptions.Builder sslContext(SSLContext sslContext)
      Sets the SSLContext to be used with SSL is enabled. This property is ignored when either sslEnabled is false or socketFactory is non-null.
      Parameters:
      sslContext - the SSLContext to be used for SSL connections
      Returns:
      this
      Since:
      3.5
    • readPreference

      public MongoClientOptions.Builder readPreference(com.mongodb.ReadPreference readPreference)
      Sets the read preference.
      Parameters:
      readPreference - read preference
      Returns:
      this
      See Also:
    • writeConcern

      public MongoClientOptions.Builder writeConcern(com.mongodb.WriteConcern writeConcern)
      Sets the write concern.
      Parameters:
      writeConcern - the write concern
      Returns:
      this
      See Also:
    • retryWrites

      public MongoClientOptions.Builder retryWrites(boolean retryWrites)
      Sets whether writes should be retried if they fail due to a network error.

      Starting with the 3.11.0 release, the default value is true

      Parameters:
      retryWrites - sets if writes should be retried if they fail due to a network error.
      Returns:
      this
      Since:
      3.6
      See Also:
      Since server release
      3.6
    • retryReads

      public MongoClientOptions.Builder retryReads(boolean retryReads)
      Sets whether reads should be retried if they fail due to a network error.
      Parameters:
      retryReads - sets if reads should be retried if they fail due to a network error.
      Returns:
      this
      Since:
      3.11
      See Also:
      Since server release
      3.6
    • readConcern

      public MongoClientOptions.Builder readConcern(com.mongodb.ReadConcern readConcern)
      Sets the read concern.
      Parameters:
      readConcern - the read concern.
      Returns:
      this
      Since:
      3.2
      See Also:
      MongoDB documentation
      Read Concern
      Since server release
      3.2
    • codecRegistry

      public MongoClientOptions.Builder codecRegistry(CodecRegistry codecRegistry)
      Sets the codec registry

      Note that instances of DB and DBCollection do not use the registry, so it's not necessary to include a codec for DBObject in the registry.

      Parameters:
      codecRegistry - the codec registry
      Returns:
      this
      Since:
      3.0
      See Also:
    • uuidRepresentation

      public MongoClientOptions.Builder uuidRepresentation(UuidRepresentation uuidRepresentation)
      Sets the UUID representation to use when encoding instances of UUID and when decoding BSON binary values with subtype of 3.

      See MongoClientOptions.getUuidRepresentation() for recommendations on settings this value

      Parameters:
      uuidRepresentation - the UUID representation, which may not be null
      Returns:
      this
      Since:
      3.12
    • serverApi

      public MongoClientOptions.Builder serverApi(com.mongodb.ServerApi serverApi)
      Sets the server API to use when sending commands to the server.

      This is required for some MongoDB deployments.

      Parameters:
      serverApi - the server API, which may not be null
      Returns:
      this
      Since:
      4.3
    • serverSelector

      public MongoClientOptions.Builder serverSelector(com.mongodb.selector.ServerSelector serverSelector)
      Sets a server selector that augments the normal server selection rules applied by the driver when determining which server to send an operation to. See MongoClientOptions.getServerSelector() for further details.
      Parameters:
      serverSelector - the server selector
      Returns:
      this
      Since:
      3.6
      See Also:
    • addCommandListener

      public MongoClientOptions.Builder addCommandListener(com.mongodb.event.CommandListener commandListener)
      Adds the given command listener.
      Parameters:
      commandListener - the non-null command listener
      Returns:
      this
      Since:
      3.1
    • addConnectionPoolListener

      public MongoClientOptions.Builder addConnectionPoolListener(com.mongodb.event.ConnectionPoolListener connectionPoolListener)
      Adds the given connection pool listener.
      Parameters:
      connectionPoolListener - the non-null connection pool listener
      Returns:
      this
      Since:
      3.5
    • addClusterListener

      public MongoClientOptions.Builder addClusterListener(com.mongodb.event.ClusterListener clusterListener)
      Adds the given cluster listener.
      Parameters:
      clusterListener - the non-null cluster listener
      Returns:
      this
      Since:
      3.3
    • addServerListener

      public MongoClientOptions.Builder addServerListener(com.mongodb.event.ServerListener serverListener)
      Adds the given server listener.
      Parameters:
      serverListener - the non-null server listener
      Returns:
      this
      Since:
      3.3
    • addServerMonitorListener

      public MongoClientOptions.Builder addServerMonitorListener(com.mongodb.event.ServerMonitorListener serverMonitorListener)
      Adds the given server monitor listener.
      Parameters:
      serverMonitorListener - the non-null server monitor listener
      Returns:
      this
      Since:
      3.3
    • cursorFinalizerEnabled

      public MongoClientOptions.Builder cursorFinalizerEnabled(boolean cursorFinalizerEnabled)
      Sets whether cursor finalizers are enabled.
      Parameters:
      cursorFinalizerEnabled - whether cursor finalizers are enabled.
      Returns:
      this
      See Also:
    • dbDecoderFactory

      public MongoClientOptions.Builder dbDecoderFactory(DBDecoderFactory dbDecoderFactory)
      Sets the decoder factory.
      Parameters:
      dbDecoderFactory - the decoder factory
      Returns:
      this
      See Also:
    • dbEncoderFactory

      public MongoClientOptions.Builder dbEncoderFactory(DBEncoderFactory dbEncoderFactory)
      Sets the encoder factory.
      Parameters:
      dbEncoderFactory - the encoder factory
      Returns:
      this
      See Also:
    • heartbeatFrequency

      public MongoClientOptions.Builder heartbeatFrequency(int heartbeatFrequency)
      Sets the heartbeat frequency. This is the frequency that the driver will attempt to determine the current state of each server in the cluster. The default value is 10,000 milliseconds
      Parameters:
      heartbeatFrequency - the heartbeat frequency for the cluster, in milliseconds, which must be > 0
      Returns:
      this
      Throws:
      IllegalArgumentException - if heartbeatFrequency is not > 0
      Since:
      2.12
      See Also:
    • minHeartbeatFrequency

      public MongoClientOptions.Builder minHeartbeatFrequency(int minHeartbeatFrequency)
      Sets the minimum heartbeat frequency. In the event that the driver has to frequently re-check a server's availability, it will wait at least this long since the previous check to avoid wasted effort. The default value is 500 milliseconds.
      Parameters:
      minHeartbeatFrequency - the minimum heartbeat frequency, in milliseconds, which must be > 0
      Returns:
      this
      Throws:
      IllegalArgumentException - if minHeartbeatFrequency <= 0
      Since:
      2.13
      See Also:
    • heartbeatConnectTimeout

      public MongoClientOptions.Builder heartbeatConnectTimeout(int connectTimeout)
      Sets the connect timeout for connections used for the cluster heartbeat.
      Parameters:
      connectTimeout - the connection timeout, in milliseconds
      Returns:
      this
      Since:
      2.12
      See Also:
    • heartbeatSocketTimeout

      public MongoClientOptions.Builder heartbeatSocketTimeout(int socketTimeout)
      Sets the socket timeout for connections used for the cluster heartbeat.
      Parameters:
      socketTimeout - the socket timeout, in milliseconds
      Returns:
      this
      Since:
      2.12
      See Also:
    • localThreshold

      public MongoClientOptions.Builder localThreshold(int localThreshold)
      Sets the local threshold.
      Parameters:
      localThreshold - the acceptable latency difference, in milliseconds, which must be >= 0
      Returns:
      this
      Throws:
      IllegalArgumentException - if localThreshold < 0
      Since:
      2.13.0
      See Also:
    • requiredReplicaSetName

      public MongoClientOptions.Builder requiredReplicaSetName(String requiredReplicaSetName)
      Sets the required replica set name for the cluster.
      Parameters:
      requiredReplicaSetName - the required replica set name for the replica set.
      Returns:
      this
      Since:
      2.12
      See Also:
    • autoEncryptionSettings

      public MongoClientOptions.Builder autoEncryptionSettings(com.mongodb.AutoEncryptionSettings autoEncryptionSettings)
      Set options for auto-encryption.
      Parameters:
      autoEncryptionSettings - auto encryption settings
      Returns:
      this
      Since:
      3.11
    • srvMaxHosts

      public MongoClientOptions.Builder srvMaxHosts(Integer srvMaxHosts)
      Sets the maximum number of hosts to connect to when using SRV protocol.
      Parameters:
      srvMaxHosts - the maximum number of hosts to connect to when using SRV protocol
      Returns:
      this
      Since:
      4.5
    • srvServiceName

      public MongoClientOptions.Builder srvServiceName(String srvServiceName)
      Sets the SRV service name.

      The SRV resource record (RFC 2782) service name, which is limited to 15 characters (RFC 6335 section 5.1). If specified, it is combined with the single host name as follows: _srvServiceName._tcp.hostName. The combined string is an SRV resource record name (RFC 1035 section 2.3.1), which is limited to 255 characters (RFC 1035 section 2.3.4).

      Parameters:
      srvServiceName - the SRV service name
      Returns:
      this
      Since:
      4.5
    • timeout

      @Alpha(CLIENT) public MongoClientOptions.Builder timeout(long timeoutMS)
      Sets the time limit, in milliseconds for the full execution of an operation.
      • null means that the timeout mechanism for operations will defer to using:
        • waitQueueTimeoutMS: The maximum wait time in milliseconds that a thread may wait for a connection to become available
        • socketTimeoutMS: How long a send or receive on a socket can take before timing out.
        • wTimeoutMS: How long the server will wait for the write concern to be fulfilled before timing out.
        • maxTimeMS: The cumulative time limit for processing operations on a cursor. See: cursor.maxTimeMS.
        • maxCommitTimeMS: The maximum amount of time to allow a single commitTransaction command to execute. See: TransactionOptions.getMaxCommitTime(java.util.concurrent.TimeUnit).
      • 0 means infinite timeout.
      • > 0 The time limit to use for the full execution of an operation.
      Parameters:
      timeoutMS - the timeout in milliseconds
      Returns:
      this
      Since:
      5.2
      See Also:
    • build

      public MongoClientOptions build()
      Build an instance of MongoClientOptions.
      Returns:
      the options from this builder