@@ -8,7 +8,7 @@ title = "Monitoring"
8
8
pre = " <i class='fa'></i>"
9
9
+++
10
10
11
- # Monitoring
11
+ # JMX Monitoring
12
12
13
13
The driver uses [ JMX] ( http://docs.oracle.com/javase/8/docs/technotes/guides/jmx/ ) to create
14
14
[ MXBeans] ( http://docs.oracle.com/javase/tutorial/jmx/mbeans/mxbeans.html ) that allow an
@@ -32,3 +32,153 @@ application has multiple `MongoClient` instances connected to the same MongoDB s
32
32
- ` size ` : the current size of the pool, including idle and and in-use members
33
33
- ` waitQueueSize ` : the current size of the wait queue for a connection from this pool
34
34
- ` checkedOutCount ` : the current count of connections that are currently in use
35
+
36
+ # Command Monitoring
37
+
38
+ The driver implements the
39
+ [ command monitoring specification] ( https://github.com/mongodb/specifications/blob/master/source/command-monitoring/command-monitoring.rst ) ,
40
+ allowing an application to be notified when a command starts and when it either succeeds or fails.
41
+
42
+ An application registers command listeners with a ` MongoClient ` by configuring ` MongoClientSettings ` with instances of classes
43
+ that implement the [ ` CommandListener ` ] ({{< apiref "com/mongodb/event/CommandListener" >}}) interface. Consider the following, somewhat
44
+ simplistic, implementation of the ` CommandListener ` interface:
45
+
46
+ ``` java
47
+ public class TestCommandListener implements CommandListener {
48
+ @Override
49
+ public void commandStarted (final CommandStartedEvent event ) {
50
+ System . out. println(String . format(" Sent command '%s:%s' with id %s to database '%s' "
51
+ + " on connection '%s' to server '%s'" ,
52
+ event. getCommandName(),
53
+ event. getCommand(). get(event. getCommandName()),
54
+ event. getRequestId(),
55
+ event. getDatabaseName(),
56
+ event. getConnectionDescription()
57
+ .getConnectionId(),
58
+ event. getConnectionDescription(). getServerAddress()));
59
+ }
60
+
61
+ @Override
62
+ public void commandSucceeded (final CommandSucceededEvent event ) {
63
+ System . out. println(String . format(" Successfully executed command '%s' with id %s "
64
+ + " on connection '%s' to server '%s'" ,
65
+ event. getCommandName(),
66
+ event. getRequestId(),
67
+ event. getConnectionDescription()
68
+ .getConnectionId(),
69
+ event. getConnectionDescription(). getServerAddress()));
70
+ }
71
+
72
+ @Override
73
+ public void commandFailed (final CommandFailedEvent event ) {
74
+ System . out. println(String . format(" Failed execution of command '%s' with id %s "
75
+ + " on connection '%s' to server '%s' with exception '%s'" ,
76
+ event. getCommandName(),
77
+ event. getRequestId(),
78
+ event. getConnectionDescription()
79
+ .getConnectionId(),
80
+ event. getConnectionDescription(). getServerAddress(),
81
+ event. getThrowable()));
82
+ }
83
+ }
84
+ ```
85
+
86
+ and an instance of ` MongoClientSettings ` configured with an instance of ` TestCommandListener ` :
87
+
88
+ ``` java
89
+ ClusterSettings clusterSettings = ...
90
+ MongoClientSettings settings = MongoClientSettings . builder()
91
+ .clusterSettings(clusterSettings)
92
+ .addCommandListener(new TestCommandListener ())
93
+ .build();
94
+ MongoClient client = MongoClients . create(settings);
95
+ ```
96
+
97
+ A ` MongoClient ` configured with these options will print a message to ` System.out ` before sending each command to a MongoDB server, and
98
+ another message upon either successful completion or failure of each command.
99
+
100
+ # Cluster Monitoring
101
+
102
+ The driver implements the
103
+ [ SDAM Monitoring specification] ( https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring-monitoring.rst ) ,
104
+ allowing an application to be notified when the driver detects changes to the topology of the MongoDB cluster to which it is connected.
105
+
106
+ An application registers listeners with a ` MongoClient ` by configuring ` MongoClientSettings ` with instances of classes that
107
+ implement any of the [ ` ClusterListener ` ] ({{< apiref "com/mongodb/event/ClusterListener" >}}),
108
+ [ ` ServerListener ` ] ({{< apiref "com/mongodb/event/ServerListener" >}}),
109
+ or [ ` ServerMonitorListener ` ] ({{< apiref "com/mongodb/event/ServerMonitorListener" >}}) interfaces.
110
+
111
+ Consider the following, somewhat simplistic, example of a cluster listener:
112
+
113
+ ``` java
114
+ public class TestClusterListener implements ClusterListener {
115
+ private final ReadPreference readPreference;
116
+ private boolean isWritable;
117
+ private boolean isReadable;
118
+
119
+ public TestClusterListener (final ReadPreference readPreference ) {
120
+ this . readPreference = readPreference;
121
+ }
122
+
123
+ @Override
124
+ public void clusterOpening (final ClusterOpeningEvent clusterOpeningEvent ) {
125
+ System . out. println(String . format(" Cluster with unique client identifier %s opening" ,
126
+ clusterOpeningEvent. getClusterId()));
127
+ }
128
+
129
+ @Override
130
+ public void clusterClosed (final ClusterClosedEvent clusterClosedEvent ) {
131
+ System . out. println(String . format(" Cluster with unique client identifier %s closed" ,
132
+ clusterClosedEvent. getClusterId()));
133
+ }
134
+
135
+ @Override
136
+ public void clusterDescriptionChanged (final ClusterDescriptionChangedEvent event ) {
137
+ if (! isWritable) {
138
+ if (event. getNewDescription(). hasWritableServer()) {
139
+ isWritable = true ;
140
+ System . out. println(" Writable server available!" );
141
+ }
142
+ } else {
143
+ if (! event. getNewDescription(). hasWritableServer()) {
144
+ isWritable = false ;
145
+ System . out. println(" No writable server available!" );
146
+ }
147
+ }
148
+
149
+ if (! isReadable) {
150
+ if (event. getNewDescription(). hasReadableServer(readPreference)) {
151
+ isReadable = true ;
152
+ System . out. println(" Readable server available!" );
153
+ }
154
+ } else {
155
+ if (! event. getNewDescription(). hasReadableServer(readPreference)) {
156
+ isReadable = false ;
157
+ System . out. println(" No readable server available!" );
158
+ }
159
+ }
160
+ }
161
+ }
162
+ ```
163
+
164
+ and an instance of ` MongoClientSettings ` configured with an instance of ` TestClusterListener ` :
165
+
166
+ ``` java
167
+ List<ServerAddress > seedList = ...
168
+ ClusterSettings clusterSettings = ClusterSettings . builder()
169
+ .hosts(seedList)
170
+ .addClusterListener(new TestClusterListener (ReadPreference . secondary()))
171
+ .build();
172
+ MongoClientSettings settings = MongoClientSettings . builder()
173
+ .clusterSettings(clusterSettings)
174
+ .build();
175
+ MongoClient client = MongoClients . create(settings);
176
+ ```
177
+
178
+ A ` MongoClient ` configured with these settings will print a message to ` System.out ` when the MongoClient is created with these options,
179
+ and when that MongoClient is closed. In addition, it will print a message when the client enters a state:
180
+
181
+ * with an available server that will accept writes
182
+ * without an available server that will accept writes
183
+ * with an available server that will accept reads using the configured ` ReadPreference `
184
+ * without an available server that will accept reads using the configured ` ReadPreference `
0 commit comments