|
33 | 33 | import java.lang.management.ManagementFactory; |
34 | 34 | import java.util.Set; |
35 | 35 |
|
| 36 | +/** |
| 37 | + * A simple class that formats Mongo Java driver connection pool statistics in an easily-accessible JSON format. |
| 38 | + * It can be used to get statistics on connection pool in the same VM by using the no-args constructor, or in any |
| 39 | + * VM by using the constructor that takes an MBeanServerConnection. |
| 40 | + * <p> |
| 41 | + * This class also exposes a command line interface modeled after mongostat. For usage, run: |
| 42 | +
|
| 43 | + * <pre> java -cp mongo.jar com.mongodb.util.management.jmx.ConnectionPoolStat --help}</pre> |
| 44 | + * |
| 45 | + * @mongodb.driver.manual reference/mongostat mongostat |
| 46 | + * |
| 47 | + */ |
36 | 48 | public class ConnectionPoolStat { |
37 | 49 |
|
| 50 | + /** |
| 51 | + * Use the given MBean server connection to access statistics for connection pools. |
| 52 | + * |
| 53 | + * @param mBeanConnection the MBean server to connect to |
| 54 | + */ |
38 | 55 | public ConnectionPoolStat(MBeanServerConnection mBeanConnection) { |
39 | 56 | this.mBeanConnection = mBeanConnection; |
40 | 57 | } |
41 | 58 |
|
| 59 | + /** |
| 60 | + * Use the platform MBean server. This is useful if you want to access statistics |
| 61 | + * for connection pools in the same virtual machine. |
| 62 | + * |
| 63 | + * @see java.lang.management.ManagementFactory#getPlatformMBeanServer() |
| 64 | + */ |
42 | 65 | public ConnectionPoolStat() { |
43 | 66 | this.mBeanConnection = ManagementFactory.getPlatformMBeanServer(); |
44 | 67 | } |
45 | 68 |
|
46 | 69 | /** |
| 70 | + * Gets the statistics for all Mongo connection pools registered with the MBean server used |
| 71 | + * by this instance. The format will always be JSON, but the specific JSON fields may change in a |
| 72 | + * future release. An example of the output, which should not be taken as a specification: |
| 73 | + * |
| 74 | + * <pre> |
| 75 | + { pools : [ |
| 76 | + { objectName: 'com.mongodb:type=ConnectionPool,host=localhost/127.0.0.1,port=27018,instance=1', |
| 77 | + host: 'localhost', port: 27018, maxSize: 10, total: 10, inUse: 3, |
| 78 | + inUseConnections: [ |
| 79 | + { namespace: 'FindContention.test', opCode: 'OP_QUERY', query: { }, numDocuments: 1, threadName: 'pool-2-thread-19', durationMS: 843, localPort: 64062 }, |
| 80 | + { namespace: 'FindContention.test', opCode: 'OP_QUERY', query: { }, numDocuments: 1, threadName: 'pool-2-thread-1', durationMS: 4331, localPort: 64095 }, |
| 81 | + { namespace: 'FindContention.test', opCode: 'OP_QUERY', query: { }, numDocuments: 1, threadName: 'pool-2-thread-16', durationMS: 4343, localPort: 64087 } |
| 82 | + ] |
| 83 | + }, |
| 84 | + { objectName: 'com.mongodb:type=ConnectionPool,host=localhost/127.0.0.1,port=27017,instance=1', |
| 85 | + host: 'localhost', port: 27017, maxSize: 10, total: 10, inUse: 2, |
| 86 | + inUseConnections: [ |
| 87 | + { namespace: 'FindContention.test', opCode: 'OP_QUERY', query: { }, numDocuments: 1, threadName: 'pool-2-thread-5', durationMS: 920, localPort: 64093 }, |
| 88 | + { namespace: 'FindContention.test', opCode: 'OP_QUERY', query: { }, numDocuments: 1, threadName: 'pool-2-thread-11', durationMS: 1468, localPort: 64068 }, |
| 89 | + ] |
| 90 | + } |
| 91 | + ] |
| 92 | + }</pre> |
47 | 93 | * |
48 | 94 | * @return JSON-formatted stats for all connection pools registered in JMX |
49 | | - * @throws Exception |
| 95 | + * @throws JMException for any JMX-related exceptions |
| 96 | + * @throws IOException for any I/O exceptions |
50 | 97 | */ |
51 | 98 | public String getStats() throws JMException, IOException { |
52 | 99 | CharArrayWriter charArrayWriter = new CharArrayWriter(); |
|
0 commit comments