Server Settings
On this page
Overview
In this guide, you can learn about how the Java driver manages server settings.
Configuring Server Settings
Include the following parameters in your connection string to modify the driver's behavior when interacting with the server:
Option Name | Type | Description |
---|---|---|
| string | Specifies the name of the application provided to MongoDB instances during the connection handshake. Can be used for server logs and profiling. Default: |
| string | Specifies which server monitoring protocol the driver uses. When set
to Default: |
| integer | Specifies the frequency, in milliseconds that the driver will wait between attempts to determine the current state of each server in the cluster. Default: |
This example specifies that the cluster monitor will attempt to reach a server every 15 seconds:
ConnectionString connectionString = "mongodb://<host>:<port>/?heartbeatFrequencyMS=15000" MongoClient mongoClient = MongoClients.create(connectionString)
For more information about these parameters, see the ConnectionString API documentation.
Chain the applyToServerSettings() method to modify the driver's behavior when monitoring each MongoDB deployment.
The following table describes the methods you can chain to your settings to modify the driver's behavior:
Method | Description |
---|---|
| Adds a listener for server-related events. |
| Adds a listener for server monitor-related events. |
| Uses the server settings specified in a |
| Sets the interval for a cluster monitor to attempt reaching a server. Default: |
| Sets the minimum interval for server monitoring checks. Default: |
This example specifies the following driver behavior in a MongoDB deployment:
The minimum interval for server monitoring checks to be at least
700 MILLISECONDS
The cluster monitor to attempt reaching a server every
15 SECONDS
MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder().applyConnectionString(new ConnectionString("<your connection string>")) .applyToServerSettings(builder -> builder.minHeartbeatFrequency(700, MILLISECONDS) .heartbeatFrequency(15, SECONDS)) .build());
For more information about the chained methods, see the MongoClientSettings.Builder API documentation.