如何使用 Java 驱动程序检查 MongoDB 的活动连接状态

Pau*_*l C 3 java connection-pooling mongodb

我需要在调用 REST 服务时发布(到 Graphite)Mongo db 实例的“可用活动连接数”状态。我知道我们可以使用 db.serverStatus() 来了解服务器端连接的详细信息。我希望使用 JAVA API 在客户端获取“可用活动连接数”信息。MongoDB Java Driver API 文档对此没有太大帮助。

Ori*_*Dar 7

假设您使用的是 3.0.x 驱动程序,并在默认端口上连接到 localhost:

MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase("admin");
Document serverStatus = database.runCommand(new Document("serverStatus", 1));
Map connections = (Map) serverStatus.get("connections");
Integer current = (Integer) connections.get("current");
Run Code Online (Sandbox Code Playgroud)