Docs Menu
Docs Home
/ / /
Java Reactive Streams Driver
/

JMX Monitoring

On this page

  • Command Monitoring
  • Cluster Monitoring
  • Connection Pool Monitoring

The driver uses JMX to create MXBeans that allow you to monitor various aspects of the driver.

The driver creates MXBean instances of a single type, ConnectionPoolStatisticsMBean. The driver registers one ConnectionPoolStatisticsMBean instance for each server it connects to. For example, when connected to a replica set, the driver creates an instance for each non-hidden member of the replica set.

Each MXBean instance is required to be registered with a unique object name, which consists of a domain and a set of named properties. All MXBean instances created by the driver are under the domain org.mongodb.driver. Instances of ConnectionPoolStatisticsMBean have the following properties:

  • clusterId: a client-generated unique identifier, required to ensure object name uniqueness in situations where an application has multiple MongoClient instances connected to the same MongoDB server deployment

  • host: the hostname of the server

  • port: the port on which the server is listening

  • minSize: the minimum allowed size of the pool, including idle and in-use members

  • maxSize: the maximum allowed size of the pool, including idle and in-use members

  • size: the current size of the pool, including idle and in-use members

  • checkedOutCount: the current count of connections that are currently in use

JMX connection pool monitoring is disabled by default. To enable it add a com.mongodb.management.JMXConnectionPoolListener instance when creating a MongoClientSettings instance:

MongoClientSettings settings =
MongoClientSettings.builder()
.applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(new JMXConnectionPoolListener()))
.build();

The driver implements the command monitoring specification, allowing an application to be notified when a command starts and when it either succeeds or fails.

An application registers command listeners with a MongoClient by configuring a MongoClientSettings instance with instances of classes that implement the CommandListener interface. The following example is a simple implementation of the CommandListener interface:

public class TestCommandListener implements CommandListener {
@Override
public void commandStarted(final CommandStartedEvent event) {
System.out.println(String.format("Sent command '%s:%s' with id %s to database '%s' "
+ "on connection '%s' to server '%s'",
event.getCommandName(),
event.getCommand().get(event.getCommandName()),
event.getRequestId(),
event.getDatabaseName(),
event.getConnectionDescription()
.getConnectionId(),
event.getConnectionDescription().getServerAddress()));
}
@Override
public void commandSucceeded(final CommandSucceededEvent event) {
System.out.println(String.format("Successfully executed command '%s' with id %s "
+ "on connection '%s' to server '%s'",
event.getCommandName(),
event.getRequestId(),
event.getConnectionDescription()
.getConnectionId(),
event.getConnectionDescription().getServerAddress()));
}
@Override
public void commandFailed(final CommandFailedEvent event) {
System.out.println(String.format("Failed execution of command '%s' with id %s "
+ "on connection '%s' to server '%s' with exception '%s'",
event.getCommandName(),
event.getRequestId(),
event.getConnectionDescription()
.getConnectionId(),
event.getConnectionDescription().getServerAddress(),
event.getThrowable()));
}
}

The following example creates an instance of MongoClientSettings configured with an instance of TestCommandListener:

MongoClientSettings settings = MongoClientSettings.builder()
.addCommandListener(new TestCommandListener())
.build();
MongoClient client = MongoClients.create(settings);

A MongoClient configured with these options prints a message to System.out before sending each command to a MongoDB server, and prints another message upon either successful completion or failure of each command.

The driver implements the SDAM Monitoring specification, allowing an application to be notified when the driver detects changes to the topology of the MongoDB cluster to which it is connected.

An application registers listeners with a MongoClient by configuring MongoClientSettings with instances of classes that implement any of the ClusterListener, ServerListener, or ServerMonitorListener interfaces.

The following code demonstrates how to create a cluster listener:

public class TestClusterListener implements ClusterListener {
private final ReadPreference readPreference;
private boolean isWritable;
private boolean isReadable;
public TestClusterListener(final ReadPreference readPreference) {
this.readPreference = readPreference;
}
@Override
public void clusterOpening(final ClusterOpeningEvent clusterOpeningEvent) {
System.out.println(String.format("Cluster with unique client identifier %s opening",
clusterOpeningEvent.getClusterId()));
}
@Override
public void clusterClosed(final ClusterClosedEvent clusterClosedEvent) {
System.out.println(String.format("Cluster with unique client identifier %s closed",
clusterClosedEvent.getClusterId()));
}
@Override
public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
if (!isWritable) {
if (event.getNewDescription().hasWritableServer()) {
isWritable = true;
System.out.println("Writable server available!");
}
} else {
if (!event.getNewDescription().hasWritableServer()) {
isWritable = false;
System.out.println("No writable server available!");
}
}
if (!isReadable) {
if (event.getNewDescription().hasReadableServer(readPreference)) {
isReadable = true;
System.out.println("Readable server available!");
}
} else {
if (!event.getNewDescription().hasReadableServer(readPreference)) {
isReadable = false;
System.out.println("No readable server available!");
}
}
}
}

The following example creates an instance of MongoClientSettings configured with an instance of TestClusterListener:

List<ServerAddress> seedList = ...
MongoClientSettings settings = MongoClientSettings.builder()
.applyToClusterSettings(builder ->
builder.addClusterListener(new TestClusterListener(ReadPreference.secondary())))
.build();
MongoClient client = MongoClients.create(settings);

A MongoClient configured with these options prints a message to System.out when the MongoClient is created with these options, and when that MongoClient is closed. In addition, it prints a message when the client enters any of the following states:

  • Has an available server that will accept writes

  • Is without an available server that will accept writes

  • Has an available server that will accept reads by using the configured ReadPreference

  • Is without an available server that will accept reads by using the configured ReadPreference

The driver supports monitoring of connection pool-related events.

An application registers listeners with a MongoClient by configuring MongoClientSettings with instances of classes that implement the ConnectionPoolListener interface.

The following code demonstrates how to create a connection pool listener:

public class TestConnectionPoolListener implements ConnectionPoolListener {
@Override
public void connectionPoolOpened(final ConnectionPoolOpenedEvent event) {
System.out.println(event);
}
@Override
public void connectionPoolClosed(final ConnectionPoolClosedEvent event) {
System.out.println(event);
}
@Override
public void connectionCheckedOut(final ConnectionCheckedOutEvent event) {
System.out.println(event);
}
@Override
public void connectionCheckedIn(final ConnectionCheckedInEvent event) {
System.out.println(event);
}
@Override
public void connectionAdded(final ConnectionAddedEvent event) {
System.out.println(event);
}
@Override
public void connectionRemoved(final ConnectionRemovedEvent event) {
System.out.println(event);
}
}

The following example creates an instance of MongoClientSettings configured with an instance of TestConnectionPoolListener:

List<ServerAddress> seedList = ...
MongoClientSettings settings = MongoClientSettings.builder()
.applyToConnectionPoolSettings(builder ->
builder.addConnectionPoolListener(new TestConnectionPoolListener()))
.build();
MongoClient client = MongoClients.create(settings);

A MongoClient configured with these options prints a message to System.out for each connection pool-related event for each MongoDB server to which the MongoClient is connected.

Back

Logging

Next

Validate Driver Artifact Signatures