Docs Menu
Docs Home
/ / /
Java Sync Driver
/ /

Server Settings

On this page

  • Overview
  • Configuring Server Settings

In this guide, you can learn about how the Java driver manages 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

appName

string

Specifies the name of the application provided to MongoDB instances during the connection handshake. Can be used for server logs and profiling.

Default: null

serverMonitoringMode

string

Specifies which server monitoring protocol the driver uses. When set to auto, the monitoring mode is determined by the environment in which the driver is running. The driver uses poll mode in function-as-a-service (FaaS) environments and stream mode in other environments.

Default: auto

heartbeatFrequencyMS

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: 10000 (10 seconds)

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

addServerListener()

Adds a listener for server-related events.

addServerMonitorListener()

Adds a listener for server monitor-related events.

applySettings()

Uses the server settings specified in a ServerSettings object.

heartbeatFrequency()

Sets the interval for a cluster monitor to attempt reaching a server.

Default: 10 seconds

minHeartbeatFrequency()

Sets the minimum interval for server monitoring checks.

Default: 500 milliseconds

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.

Back

Cluster Settings