Class MongoClientOptions
- java.lang.Object
-
- com.mongodb.MongoClientOptions
-
@Immutable public class MongoClientOptions extends Object
Various settings to control the behavior of a
MongoClient
.- Since:
- 2.10.0
- See Also:
MongoClient
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MongoClientOptions.Builder
A builder for MongoClientOptions so that MongoClientOptions can be immutable, and to support easier construction through chaining.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MongoClientSettings
asMongoClientSettings(List<ServerAddress> hosts, String srvHost, ClusterConnectionMode clusterConnectionMode, MongoCredential credential)
Translate this instance intoMongoClientSettings
.static MongoClientOptions.Builder
builder()
Creates a builder instance.static MongoClientOptions.Builder
builder(MongoClientOptions options)
Creates a builder instance.static MongoClientOptions.Builder
builder(MongoClientSettings settings)
Creates a builder instance from aMongoClientSettings
instance.boolean
equals(Object o)
String
getApplicationName()
Gets the logical name of the application using this MongoClient.AutoEncryptionSettings
getAutoEncryptionSettings()
Gets the auto-encryption settingsList<ClusterListener>
getClusterListeners()
Gets the list of addedClusterListener
.CodecRegistry
getCodecRegistry()
The codec registry to use.List<CommandListener>
getCommandListeners()
Gets the list of addedCommandListener
.List<MongoCompressor>
getCompressorList()
Gets the compressors to use for compressing messages to the server.List<ConnectionPoolListener>
getConnectionPoolListeners()
Gets the list of addedConnectionPoolListener
.int
getConnectionsPerHost()
The maximum number of connections allowed per host for this MongoClient instance.int
getConnectTimeout()
The connection timeout in milliseconds.DBDecoderFactory
getDbDecoderFactory()
Override the decoder factory.DBEncoderFactory
getDbEncoderFactory()
Override the encoder factory.int
getHeartbeatConnectTimeout()
Gets the connect timeout for connections used for the cluster heartbeat.int
getHeartbeatFrequency()
Gets the heartbeat frequency.int
getHeartbeatSocketTimeout()
Gets the socket timeout for connections used for the cluster heartbeat.int
getLocalThreshold()
Gets the local threshold.int
getMaxConnectionIdleTime()
The maximum idle time of a pooled connection.int
getMaxConnectionLifeTime()
The maximum life time of a pooled connection.int
getMaxWaitTime()
The maximum wait time in milliseconds that a thread may wait for a connection to become available.int
getMinConnectionsPerHost()
The minimum number of connections per host for this MongoClient instance.int
getMinHeartbeatFrequency()
Gets the minimum heartbeat frequency.ReadConcern
getReadConcern()
The read concern to use.ReadPreference
getReadPreference()
The read preference to use for queries, map-reduce, aggregation, and count.String
getRequiredReplicaSetName()
Gets the required replica set name.boolean
getRetryReads()
Returns true if reads should be retried if they fail due to a network error or other retryable error.boolean
getRetryWrites()
Returns true if writes should be retried if they fail due to a network error or other retryable error.ServerApi
getServerApi()
Gets the server API to use when sending commands to the server.List<ServerListener>
getServerListeners()
Gets the list of addedServerListener
.List<ServerMonitorListener>
getServerMonitorListeners()
Gets the list of addedServerMonitorListener
.int
getServerSelectionTimeout()
Gets the server selection timeout in milliseconds, which defines how long the driver will wait for server selection to succeed before throwing an exception.ServerSelector
getServerSelector()
Gets the server selector.int
getSocketTimeout()
The socket timeout in milliseconds.SSLContext
getSslContext()
Returns the SSLContext.UuidRepresentation
getUuidRepresentation()
Gets the UUID representation to use when encoding instances ofUUID
and when decoding BSON binary values with subtype of 3.WriteConcern
getWriteConcern()
The write concern to use.int
hashCode()
boolean
isCursorFinalizerEnabled()
Gets whether there is a a finalize method created that cleans up instances of DBCursor that the client does not close.boolean
isSslEnabled()
Whether to use SSL.boolean
isSslInvalidHostNameAllowed()
Returns whether invalid host names should be allowed if SSL is enabled.String
toString()
-
-
-
Method Detail
-
builder
public static MongoClientOptions.Builder builder()
Creates a builder instance.- Returns:
- a builder
- Since:
- 3.0.0
-
builder
public static MongoClientOptions.Builder builder(MongoClientOptions options)
Creates a builder instance.- Parameters:
options
- existing MongoClientOptions to default the builder settings on.- Returns:
- a builder
- Since:
- 3.0.0
-
builder
public static MongoClientOptions.Builder builder(MongoClientSettings settings)
Creates a builder instance from aMongoClientSettings
instance.- Parameters:
settings
- the settings to from which to initialize the builder- Returns:
- a builder
- Since:
- 4.2
-
asMongoClientSettings
public MongoClientSettings asMongoClientSettings(@Nullable List<ServerAddress> hosts, @Nullable String srvHost, ClusterConnectionMode clusterConnectionMode, @Nullable MongoCredential credential)
Translate this instance intoMongoClientSettings
.- Parameters:
hosts
- the seed list of hosts to connect to, which must be null if srvHost is notsrvHost
- the SRV host name, which must be null if hosts is notclusterConnectionMode
- the connection modecredential
- the credential, which may be null- Returns:
- the settings
- Since:
- 4.2
- See Also:
MongoClientSettings
-
getApplicationName
@Nullable public String getApplicationName()
Gets 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.Default is null.
- Returns:
- the application name, which may be null
- Since:
- 3.4
- Since server release
- 3.4
-
getCompressorList
public List<MongoCompressor> getCompressorList()
Gets 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.Default is the empty list.
- Returns:
- the compressors
- Since:
- 3.6
- Since server release
- 3.4
-
getConnectionsPerHost
public int getConnectionsPerHost()
The maximum number of connections allowed per host for this MongoClient instance. Those connections will be kept in a pool when idle. Once the pool is exhausted, any operation requiring a connection will block waiting for an available connection.
Default is 100.
- Returns:
- the maximum size of the connection pool per host
-
getMinConnectionsPerHost
public int getMinConnectionsPerHost()
The minimum number of connections per host for this MongoClient instance. Those connections will be kept in a pool when idle, and the pool will ensure over time that it contains at least this minimum number.
Default is 0.
- Returns:
- the minimum size of the connection pool per host
-
getServerSelectionTimeout
public int getServerSelectionTimeout()
Gets the server selection timeout in milliseconds, which defines how long the driver will wait for server selection to succeed before throwing an exception.
Default is 30,000 milliseconds. A value of 0 means that it will timeout immediately if no server is available. A negative value means to wait indefinitely.
- Returns:
- the server selection timeout in milliseconds.
-
getMaxWaitTime
public int getMaxWaitTime()
The maximum wait time in milliseconds that a thread may wait for a connection to become available.
Default is 120,000 milliseconds. A value of 0 means that it will not wait. A negative value means to wait indefinitely.
- Returns:
- the maximum wait time.
-
getMaxConnectionIdleTime
public int getMaxConnectionIdleTime()
The maximum idle time of a pooled connection. A zero value indicates no limit to the idle time. A pooled connection that has exceeded its idle time will be closed and replaced when necessary by a new connection.Default is 0, indicating no limit to the idle time.
- Returns:
- the maximum idle time, in milliseconds
- Since:
- 2.12
-
getMaxConnectionLifeTime
public int getMaxConnectionLifeTime()
The maximum life time of a pooled connection. A zero value indicates no limit to the life time. A pooled connection that has exceeded its life time will be closed and replaced when necessary by a new connection.Default is 0, indicating no limit to the life time.
- Returns:
- the maximum life time, in milliseconds
- Since:
- 2.12
-
getConnectTimeout
public int getConnectTimeout()
The connection timeout in milliseconds. A value of 0 means no timeout. It is used solely when establishing a new connection
Socket.connect(java.net.SocketAddress, int)
Default is 10,000 milliseconds.
- Returns:
- the socket connect timeout
-
getSocketTimeout
public int getSocketTimeout()
The socket timeout in milliseconds. It is used for I/O socket read and write operations
Socket.setSoTimeout(int)
Default is 0 and means no timeout.
- Returns:
- the socket timeout, in milliseconds
-
getHeartbeatFrequency
public int getHeartbeatFrequency()
Gets the heartbeat frequency. This is the frequency that the driver will attempt to determine the current state of each server in the cluster.Default is 10,000 milliseconds.
- Returns:
- the heartbeat frequency, in milliseconds
- Since:
- 2.12
-
getMinHeartbeatFrequency
public int getMinHeartbeatFrequency()
Gets 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.Default is 500 milliseconds.
- Returns:
- the minimum heartbeat frequency, in milliseconds
- Since:
- 2.13
-
getHeartbeatConnectTimeout
public int getHeartbeatConnectTimeout()
Gets the connect timeout for connections used for the cluster heartbeat.
Default is 20,000 milliseconds.
- Returns:
- the heartbeat connect timeout, in milliseconds
- Since:
- 2.12
-
getHeartbeatSocketTimeout
public int getHeartbeatSocketTimeout()
Gets the socket timeout for connections used for the cluster heartbeat.Default is 20,000 milliseconds.
- Returns:
- the heartbeat socket timeout, in milliseconds
- Since:
- 2.12
-
getLocalThreshold
public int getLocalThreshold()
Gets the local threshold. When choosing among multiple MongoDB servers to send a request, the MongoClient will only send that request to a server whose ping time is less than or equal to the server with the fastest ping time plus the local threshold.
For example, let's say that the client is choosing a server to send a query when the read preference is
ReadPreference.secondary()
, and that there are three secondaries, server1, server2, and server3, whose ping times are 10, 15, and 16 milliseconds, respectively. With a local threshold of 5 milliseconds, the client will send the query to either server1 or server2 (randomly selecting between the two).Default is 15 milliseconds.
- Returns:
- the local threshold, in milliseconds
- Since:
- 2.13.0
- MongoDB documentation
- Local Threshold
-
getRequiredReplicaSetName
@Nullable public String getRequiredReplicaSetName()
Gets the required replica set name. With this option set, the MongoClient instance will
- Connect in replica set mode, and discover all members of the set based on the given servers
- Make sure that the set name reported by all members matches the required set name.
- Refuse to service any requests if any member of the seed list is not part of a replica set with the required name.
- Returns:
- the required replica set name
- Since:
- 2.12
-
isSslEnabled
public boolean isSslEnabled()
Whether to use SSL.Default is
false
.- Returns:
- true if SSL should be used
- Since:
- 3.0
-
isSslInvalidHostNameAllowed
public boolean isSslInvalidHostNameAllowed()
Returns whether invalid host names should be allowed if SSL is enabled. Take care before setting this to true, as it makes the application susceptible to man-in-the-middle attacks.Default is
false
.- Returns:
- true if invalid host names are allowed.
-
getSslContext
@Nullable public SSLContext getSslContext()
Returns the SSLContext. This property is ignored when either sslEnabled is false or socketFactory is non-null.- Returns:
- the configured SSLContext, which may be null. In that case
SSLContext.getDefault()
will be used when SSL is enabled. - Since:
- 3.5
-
getReadPreference
public ReadPreference getReadPreference()
The read preference to use for queries, map-reduce, aggregation, and count.
Default is
ReadPreference.primary()
.- Returns:
- the read preference
- See Also:
ReadPreference.primary()
-
getWriteConcern
public WriteConcern getWriteConcern()
The write concern to use.
Default is
WriteConcern.ACKNOWLEDGED
.- Returns:
- the write concern
- See Also:
WriteConcern.ACKNOWLEDGED
-
getRetryWrites
public boolean getRetryWrites()
Returns true if writes should be retried if they fail due to a network error or other retryable error.Starting with the 3.11.0 release, the default value is true
- Returns:
- the retryWrites value
- Since:
- 3.6
- Since server release
- 3.6
-
getRetryReads
public boolean getRetryReads()
Returns true if reads should be retried if they fail due to a network error or other retryable error.- Returns:
- the retryReads value
- Since:
- 3.11
- Since server release
- 3.6
-
getReadConcern
public ReadConcern getReadConcern()
The read concern to use.
- Returns:
- the read concern
- Since:
- 3.2
- MongoDB documentation
- Read Concern
- Since server release
- 3.2
-
getCodecRegistry
public CodecRegistry getCodecRegistry()
The codec registry to use. By default, a
MongoClient
will be able to encode and decode instances ofDocument
.Note that instances of
DB
andDBCollection
do not use the registry, so it's not necessary to include a codec for DBObject in the registry.- Returns:
- the codec registry
- Since:
- 3.0
- See Also:
MongoClient.getDatabase(java.lang.String)
-
getUuidRepresentation
public UuidRepresentation getUuidRepresentation()
Gets the UUID representation to use when encoding instances ofUUID
and when decoding BSON binary values with subtype of 3.The default is
UuidRepresentation.UNSPECIFIED
, If your application stores UUID values in MongoDB, you must set this value to the desired representation. New applications should preferUuidRepresentation.STANDARD
, while existing Java applications should preferUuidRepresentation.JAVA_LEGACY
. Applications wishing to interoperate with existing Python or .NET applications should preferUuidRepresentation.PYTHON_LEGACY
orUuidRepresentation.C_SHARP_LEGACY
, respectively. Applications that do not store UUID values in MongoDB don't need to set this value.- Returns:
- the UUID representation, which may not be null
- Since:
- 3.12
-
getServerApi
@Nullable public ServerApi getServerApi()
Gets the server API to use when sending commands to the server.- Returns:
- the server API, which may be null
- Since:
- 4.3
-
getServerSelector
@Nullable public ServerSelector getServerSelector()
Gets the server selector.The server selector augments the normal server selection rules applied by the driver when determining which server to send an operation to. At the point that it's called by the driver, the
ClusterDescription
which is passed to it contains a list ofServerDescription
instances which satisfy either the configuredReadPreference
for any read operation or ones that can take writes (e.g. a standalone, mongos, or replica set primary).The server selector can then filter the
ServerDescription
list using whatever criteria that is required by the application.After this selector executes, two additional selectors are applied by the driver:
- select from within the latency window
- select a random server from those remaining
To skip the latency window selector, an application can:
- configure the local threshold to a sufficiently high value so that it doesn't exclude any servers
- return a list containing a single server from this selector (which will also make the random member selector a no-op)
- Returns:
- the server selector, which may be null
- Since:
- 3.6
-
getClusterListeners
public List<ClusterListener> getClusterListeners()
Gets the list of addedClusterListener
. The default is an empty list.- Returns:
- the unmodifiable list of cluster listeners
- Since:
- 3.3
-
getCommandListeners
public List<CommandListener> getCommandListeners()
Gets the list of addedCommandListener
.Default is an empty list.
- Returns:
- the unmodifiable list of command listeners
- Since:
- 3.1
-
getConnectionPoolListeners
public List<ConnectionPoolListener> getConnectionPoolListeners()
Gets the list of addedConnectionPoolListener
. The default is an empty list.- Returns:
- the unmodifiable list of connection pool listeners
- Since:
- 3.5
-
getServerListeners
public List<ServerListener> getServerListeners()
Gets the list of addedServerListener
. The default is an empty list.- Returns:
- the unmodifiable list of server listeners
- Since:
- 3.3
-
getServerMonitorListeners
public List<ServerMonitorListener> getServerMonitorListeners()
Gets the list of addedServerMonitorListener
. The default is an empty list.- Returns:
- the unmodifiable list of server monitor listeners
- Since:
- 3.3
-
getDbDecoderFactory
public DBDecoderFactory getDbDecoderFactory()
Override the decoder factory.Default is for the standard Mongo Java driver configuration.
- Returns:
- the decoder factory
-
getDbEncoderFactory
public DBEncoderFactory getDbEncoderFactory()
Override the encoder factory.Default is for the standard Mongo Java driver configuration.
- Returns:
- the encoder factory
-
isCursorFinalizerEnabled
public boolean isCursorFinalizerEnabled()
Gets whether there is a a finalize method created that cleans up instances of DBCursor that the client does not close. If you are careful to always call the close method of DBCursor, then this can safely be set to false.
Default is true.
- Returns:
- whether finalizers are enabled on cursors
- See Also:
DBCursor
,DBCursor.close()
-
getAutoEncryptionSettings
@Nullable public AutoEncryptionSettings getAutoEncryptionSettings()
Gets the auto-encryption settings- Returns:
- the auto-encryption settings, which may be null
- Since:
- 3.11
-
-