Class MongoClientSettings.Builder
- Enclosing class:
- MongoClientSettings
MongoClientSettings so that MongoClientSettings can be immutable, and to support easier construction
through chaining.-
Method Summary
Modifier and TypeMethodDescriptionaddCommandListener(CommandListener commandListener) Adds the given command listener.applicationName(String applicationName) Sets the logical name of the application using this MongoClient.applyConnectionString(ConnectionString connectionString) Takes the settings from the givenConnectionStringand applies them to the builderApplies theClusterSettings.Builderblock and then sets the clusterSettings.Applies theConnectionPoolSettings.Builderblock and then sets the connectionPoolSettings.Applies theLoggerSettings.Builderblock and then sets the loggerSettings.Applies theServerSettings.Builderblock and then sets the serverSettings.Applies theSocketSettings.Builderblock and then sets the socketSettings.Applies theSslSettings.Builderblock and then sets the sslSettings.autoEncryptionSettings(AutoEncryptionSettings autoEncryptionSettings) Sets the auto-encryption settingsbuild()Build an instance ofMongoClientSettings.codecRegistry(CodecRegistry codecRegistry) Sets the codec registrycommandListenerList(List<CommandListener> commandListeners) Sets the command listenerscompressorList(List<MongoCompressor> compressorList) Sets the compressors to use for compressing messages to the server.contextProvider(ContextProvider contextProvider) Sets the context providercredential(MongoCredential credential) Sets the credential.Sets theDnsClientto use for resolving DNS queries.enableOverloadRetargeting(boolean enableOverloadRetargeting) Sets whether to enable overload retargeting.inetAddressResolver(InetAddressResolver inetAddressResolver) Sets theInetAddressResolverto use for looking up theInetAddressinstances for each host.maxAdaptiveRetries(Integer maxAdaptiveRetries) Sets the maximum number of retry attempts when executing a command and encountering an error having the "SystemOverloadedError" and "RetryableError" labels.observabilitySettings(ObservabilitySettings observabilitySettings) Sets theObservabilitySettingsto apply.readConcern(ReadConcern readConcern) Sets the read concern.readPreference(ReadPreference readPreference) Sets the read preference.retryReads(boolean retryReads) Sets whether attempts to execute read commands should be retried if they fail due to a retryable error.retryWrites(boolean retryWrites) Sets whether attempts to execute write commands should be retried if they fail due to a retryable error.Sets the server API to use when sending commands to the server.Sets the time limit for the full execution of an operation.transportSettings(TransportSettings transportSettings) Sets theTransportSettingsto apply.uuidRepresentation(UuidRepresentation uuidRepresentation) Sets the UUID representation to use when encoding instances ofUUIDand when decoding BSON binary values with subtype of 3.writeConcern(WriteConcern writeConcern) Sets the write concern.
-
Method Details
-
applyConnectionString
Takes the settings from the givenConnectionStringand applies them to the builder- Parameters:
connectionString- the connection string containing details of how to connect to MongoDB- Returns:
- this
-
applyToLoggerSettings
Applies theLoggerSettings.Builderblock and then sets the loggerSettings.- Parameters:
block- the block to apply to the LoggerSettings.- Returns:
- this
- Since:
- 4.9
- See Also:
-
applyToClusterSettings
Applies theClusterSettings.Builderblock and then sets the clusterSettings.- Parameters:
block- the block to apply to the ClusterSettings.- Returns:
- this
- See Also:
-
applyToSocketSettings
Applies theSocketSettings.Builderblock and then sets the socketSettings.- Parameters:
block- the block to apply to the SocketSettings.- Returns:
- this
- See Also:
-
applyToConnectionPoolSettings
public MongoClientSettings.Builder applyToConnectionPoolSettings(Block<ConnectionPoolSettings.Builder> block) Applies theConnectionPoolSettings.Builderblock and then sets the connectionPoolSettings.- Parameters:
block- the block to apply to the ConnectionPoolSettings.- Returns:
- this
- See Also:
-
applyToServerSettings
Applies theServerSettings.Builderblock and then sets the serverSettings.- Parameters:
block- the block to apply to the ServerSettings.- Returns:
- this
- See Also:
-
applyToSslSettings
Applies theSslSettings.Builderblock and then sets the sslSettings.- Parameters:
block- the block to apply to the SslSettings.- Returns:
- this
- See Also:
-
readPreference
Sets the read preference.- Parameters:
readPreference- read preference- Returns:
- this
- See Also:
-
writeConcern
Sets the write concern.- Parameters:
writeConcern- the write concern- Returns:
- this
- See Also:
-
retryWrites
Sets whether attempts to execute write commands should be retried if they fail due to a retryable error.The errors having the "RetryableError" label are not the only ones considered retryable here: unlike applications, which may retry operations, the driver retries commands, which gives it more control and allows it to safely retry attempts failed due to a broader set of errors than what applications may safely retry.
For more information on how transactions affect retries, see the documentation of the "TransientTransactionError", "UnknownTransactionCommitResult" error labels.
Starting with the 3.11.0 release, the default value is true
- Parameters:
retryWrites- sets if write commands should be retried if they fail due to a retryable error.- Returns:
- this
- See Also:
- MongoDB documentation
- Retryable Writes
- Since server release
- 3.6
-
retryReads
Sets whether attempts to execute read commands should be retried if they fail due to a retryable error.The errors having the "RetryableError" label are not the only ones considered retryable here: unlike applications, which may retry operations, the driver retries commands, which gives it more control and allows it to safely retry attempts failed due to a broader set of errors than what applications may safely retry.
For more information on how transactions affect retries, see the documentation of the "TransientTransactionError", "UnknownTransactionCommitResult" error labels.
Default is
true.- Parameters:
retryReads- sets if read commands should be retried if they fail due to a retryable error.- Returns:
- this
- Since:
- 3.11
- See Also:
- MongoDB documentation
- Retryable Reads
- Since server release
- 3.6
-
maxAdaptiveRetries
@Beta(CLIENT) public MongoClientSettings.Builder maxAdaptiveRetries(@Nullable Integer maxAdaptiveRetries) Sets the maximum number of retry attempts when executing a command and encountering an error having the "SystemOverloadedError" and "RetryableError" labels. Such errors are referred to as retryable overload errors.Default is
null, implies the value 2 and the above retry behavior. The implied value and behavior may change in the future in a minor version. This means, there is no guarantee that not setting a value is equivalent to setting the value 2. The value 0 results in not retrying the attempts failed due to retryable overload errors.Interactions with retryWrites(boolean)/retryReads(boolean)Command kind Interaction write The attempts failed due to retryable overload errors are retried only if retryWrites(boolean)istrue.read The attempts failed due to retryable overload errors are retried only if retryReads(boolean)istrue.Executing a write operation, for example,
MongoCluster.bulkWrite, may involve executing not only write commands, but also read commands. In such a situation, just like in other situations, the behavior related to retries depends on the known kind of command, not on the kind of operation.unknown The attempts failed due to retryable overload errors are retried only if retryWrites(boolean)istrueandretryReads(boolean)istrue.The command kind is unknown if and only if a command is executed via the
MongoDatabase.runCommandoperation.- Parameters:
maxAdaptiveRetries- Sets the maximum number of retry attempts when encountering a retryable overload error.- Returns:
this.- Since:
- 5.12
- See Also:
-
enableOverloadRetargeting
@Beta(CLIENT) public MongoClientSettings.Builder enableOverloadRetargeting(boolean enableOverloadRetargeting) Sets whether to enable overload retargeting.When enabled, the previously selected servers on which attempts failed with an error having the "SystemOverloadedError" label may be deprioritized during server selection on subsequent retry attempts. This applies to reads when retryReads is enabled, and to writes when retryWrites is enabled.
This setting does not take effect for sharded clusters.
Defaults to
false.- Parameters:
enableOverloadRetargeting- whether to enable overload retargeting.- Returns:
- this
- Since:
- 5.12
- See Also:
- Since server release
- 9.0
-
readConcern
Sets the read concern.- Parameters:
readConcern- the read concern- Returns:
- this
- MongoDB documentation
- Read Concern
- Since server release
- 3.2
-
credential
Sets the credential.- Parameters:
credential- the credential- Returns:
- this
-
codecRegistry
Sets the codec registryThe
CodecRegistryconfigured by this method is effectively treated by the driver as an instance ofCodecProvider, whichCodecRegistryextends. So there is no benefit to defining a class that implementsCodecRegistry. Rather, an application should always createCodecRegistryinstances using the factory methods inCodecRegistries.- Parameters:
codecRegistry- the codec registry- Returns:
- this
- See Also:
-
transportSettings
Sets theTransportSettingsto apply.- Parameters:
transportSettings- the transport settings- Returns:
- this
- See Also:
-
observabilitySettings
public MongoClientSettings.Builder observabilitySettings(ObservabilitySettings observabilitySettings) Sets theObservabilitySettingsto apply.- Parameters:
observabilitySettings- the observability settings- Returns:
- this
- Since:
- 5.7
- See Also:
-
addCommandListener
Adds the given command listener.- Parameters:
commandListener- the command listener- Returns:
- this
-
commandListenerList
Sets the command listeners- Parameters:
commandListeners- the list of command listeners- Returns:
- this
-
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
- See Also:
- Since server release
- 3.4
-
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
- See Also:
- Since server release
- 3.4
-
uuidRepresentation
Sets the UUID representation to use when encoding instances ofUUIDand when decoding BSON binary values with subtype of 3.See
MongoClientSettings.getUuidRepresentation()for recommendations on settings this value- Parameters:
uuidRepresentation- the UUID representation, which may not be null- Returns:
- this
- Since:
- 3.12
-
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
-
autoEncryptionSettings
public MongoClientSettings.Builder autoEncryptionSettings(@Nullable AutoEncryptionSettings autoEncryptionSettings) Sets the auto-encryption settingsA separate, internal
MongoClientis created if any of the following are true:AutoEncryptionSettings.keyVaultClientis not passedAutoEncryptionSettings.bypassAutomaticEncryptionisfalse
MongoClientis created, it is configured with the same options as the parentMongoClientexceptminPoolSizeis set to0andAutoEncryptionSettingsis omitted.- Parameters:
autoEncryptionSettings- the auto-encryption settings- Returns:
- this
- Since:
- 3.11
- See Also:
-
contextProvider
Sets the context providerWhen used with the synchronous driver, this must be an instance of
com.mongodb.client.SynchronousContextProvider. When used with the reactive streams driver, this must be an instance ofcom.mongodb.reactivestreams.client.ReactiveContextProvider.- Parameters:
contextProvider- the context provider- Returns:
- this
- Since:
- 4.4
-
dnsClient
Sets theDnsClientto use for resolving DNS queries.If set, it will be used to resolve SRV and TXT records for mongodb+srv connections. Otherwise, implementation of
DnsClientProviderwill be discovered viaServiceLoaderand used to create an instance ofDnsClient. If no implementation is discovered, thencom.sun.jndi.dns.DnsContextFactorywill be used to resolve these records.If applying a connection string to these settings, care must be taken to also pass the same
DnsClientas an argument to theConnectionStringconstructor.- Parameters:
dnsClient- the DNS client- Returns:
- the DNS client
- Since:
- 4.10
- See Also:
-
inetAddressResolver
public MongoClientSettings.Builder inetAddressResolver(@Nullable InetAddressResolver inetAddressResolver) Sets theInetAddressResolverto use for looking up theInetAddressinstances for each host.If set, it will be used to look up the
InetAddressfor each host, viaInetAddressResolver.lookupByName(String). Otherwise, an implementation ofInetAddressResolverProviderwill be discovered viaServiceLoaderand used to create an instance ofInetAddressResolver. If no implementation is discovered,InetAddress.getAllByName(String)will be used to lookup theInetAddressinstances for a host.- Parameters:
inetAddressResolver- the InetAddress provider- Returns:
- the
InetAddressresolver - Since:
- 4.10
- See Also:
-
timeout
Sets the time limit for the full execution of an operation.nullmeans 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 availablesocketTimeoutMS: 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 singlecommitTransactioncommand to execute. See:TransactionOptions.getMaxCommitTime(java.util.concurrent.TimeUnit).
0means infinite timeout.> 0The time limit to use for the full execution of an operation.
Note: When using synchronous API, this timeout does not limit socket writes, therefore there is a possibility that the operation might not be timed out when expected. This limitation does not apply to the reactive streams API.
- Parameters:
timeout- the timeouttimeUnit- the time unit- Returns:
- this
- Since:
- 5.2
- See Also:
-
build
Build an instance ofMongoClientSettings.- Returns:
- the settings from this builder
-