JMX监控
驱动程序使用JMX 来创建 MXBeans,允许您监控驱动程序的各种方面。
驱动程序创建MXBean
实例为一个单一类型,ConnectionPoolStatisticsMBean
。驱动程序为每个连接到的服务器注册一个 ConnectionPoolStatisticsMBean
实例。例如,当连接到副本集时,驱动程序为每个非隐藏成员创建一个实例。
每个 MXBean
实例都需要注册一个唯一的对象名称,该名称由一个域和一组命名属性组成。驱动程序创建的所有 MXBean
实例都在 org.mongodb.driver
域下。ConnectionPoolStatisticsMBean
的实例具有以下属性
clusterId
:一个客户端生成的唯一标识符,在应用程序具有多个连接到同一 MongoDB 服务器部署的MongoClient
实例的情况下,确保对象名称唯一性host
:服务器的主机名port
:服务器监听的端口minSize
:池中允许的最小大小,包括空闲和正在使用的成员maxSize
:池中允许的最大大小,包括空闲和正在使用的成员size
:池的当前大小,包括空闲和正在使用的成员checkedOutCount
:当前正在使用的连接数量
JMX连接池监控默认是禁用的。要启用它,在创建MongoClientSettings
实例时添加一个com.mongodb.management.JMXConnectionPoolListener
实例。
MongoClientSettings settings = MongoClientSettings.builder() .applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(new JMXConnectionPoolListener())) .build();
命令监控
驱动程序实现了命令监控规范,允许应用程序在命令开始执行以及命令成功或失败时被通知。
应用程序通过使用实现CommandListener
接口的类的实例配置MongoClientSettings
实例,在MongoClient
上注册命令监听器。以下是一个简单的CommandListener
接口实现示例
public class TestCommandListener implements CommandListener { 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())); } 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())); } 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())); } }
以下示例创建了一个配置了TestCommandListener
实例的MongoClientSettings
实例
MongoClientSettings settings = MongoClientSettings.builder() .addCommandListener(new TestCommandListener()) .build(); MongoClient client = MongoClients.create(settings);
配置了这些选项的MongoClient
在将每个命令发送到MongoDB服务器之前在System.out
中打印一条消息,并在每个命令成功完成或失败时打印另一条消息。
集群监控
驱动程序实现了SDAM监控规范,允许应用程序在驱动程序检测到与它连接的MongoDB集群拓扑结构变化时被通知。
应用程序通过使用实现ClusterListener
、ServerListener
或ServerMonitorListener
接口之一的类的实例配置MongoClientSettings
,在MongoClient
上注册监听器。
以下代码演示了如何创建一个集群监听器
public class TestClusterListener implements ClusterListener { private final ReadPreference readPreference; private boolean isWritable; private boolean isReadable; public TestClusterListener(final ReadPreference readPreference) { this.readPreference = readPreference; } public void clusterOpening(final ClusterOpeningEvent clusterOpeningEvent) { System.out.println(String.format("Cluster with unique client identifier %s opening", clusterOpeningEvent.getClusterId())); } public void clusterClosed(final ClusterClosedEvent clusterClosedEvent) { System.out.println(String.format("Cluster with unique client identifier %s closed", clusterClosedEvent.getClusterId())); } 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!"); } } } }
以下示例创建了一个配置了TestClusterListener
实例的MongoClientSettings
实例
List<ServerAddress> seedList = ... MongoClientSettings settings = MongoClientSettings.builder() .applyToClusterSettings(builder -> builder.addClusterListener(new TestClusterListener(ReadPreference.secondary()))) .build(); MongoClient client = MongoClients.create(settings);
配置了这些选项的MongoClient
在创建带有这些选项的MongoClient
时,在System.out
中打印一条消息,并在关闭该MongoClient
时打印另一条消息。此外,当客户端进入以下任何状态时,它还会打印一条消息
有一个可接受写入的可用服务器
没有可接受写入的可用服务器
拥有可用的服务器,可以通过配置的
ReadPreference
接受读取操作没有可用的服务器,无法通过配置的
ReadPreference
接受读取操作
连接池监控
驱动程序支持监控与连接池相关的事件。
应用程序通过配置实现 ConnectionPoolListener
接口类实例的 MongoClientSettings
,在 MongoClient
上注册监听器。
以下代码演示了如何创建连接池监听器
public class TestConnectionPoolListener implements ConnectionPoolListener { public void connectionPoolOpened(final ConnectionPoolOpenedEvent event) { System.out.println(event); } public void connectionPoolClosed(final ConnectionPoolClosedEvent event) { System.out.println(event); } public void connectionCheckedOut(final ConnectionCheckedOutEvent event) { System.out.println(event); } public void connectionCheckedIn(final ConnectionCheckedInEvent event) { System.out.println(event); } public void connectionAdded(final ConnectionAddedEvent event) { System.out.println(event); } public void connectionRemoved(final ConnectionRemovedEvent event) { System.out.println(event); } }
以下示例创建了一个配置了 TestConnectionPoolListener
实例的 MongoClientSettings
实例
List<ServerAddress> seedList = ... MongoClientSettings settings = MongoClientSettings.builder() .applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(new TestConnectionPoolListener())) .build(); MongoClient client = MongoClients.create(settings);
配置了这些选项的 MongoClient
将为连接到每个 MongoDB 服务器的每个连接池相关事件打印一条消息到 System.out
。