如何使用 Java 驱动程序检查 MongoDB 的活动连接状态
How can I check for MongoDB's active connections status using Java driver
我需要在调用 REST 服务时发布(到 Graphite)'number of active connection available' 数据库实例的 Mongo 状态。我知道我们可以使用 db.serverStatus() 来了解服务器端连接的详细信息。
我希望使用 JAVA API 在客户端获取 'number of active connections available' 信息。 MongoDB Java Driver API 文档对此帮助不大。
db.serverStatus() 提供有关创建的连接数和可用连接数的信息。如下所示:
"connections" : {
"current" : 3,
"available" : 2045,
"totalCreated" : NumberLong(3)
}
您还可以使用 db.currentOp(true) 获取进行中的详细信息。
http://docs.mongodb.org/manual/reference/method/db.currentOp/
假设您正在使用 3.0.x 驱动程序,并在默认端口上连接到本地主机:
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");
我需要在调用 REST 服务时发布(到 Graphite)'number of active connection available' 数据库实例的 Mongo 状态。我知道我们可以使用 db.serverStatus() 来了解服务器端连接的详细信息。 我希望使用 JAVA API 在客户端获取 'number of active connections available' 信息。 MongoDB Java Driver API 文档对此帮助不大。
db.serverStatus() 提供有关创建的连接数和可用连接数的信息。如下所示:
"connections" : {
"current" : 3,
"available" : 2045,
"totalCreated" : NumberLong(3)
}
您还可以使用 db.currentOp(true) 获取进行中的详细信息。
http://docs.mongodb.org/manual/reference/method/db.currentOp/
假设您正在使用 3.0.x 驱动程序,并在默认端口上连接到本地主机:
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");