Skip to content

Commit 471ac7e

Browse files
authored
[Kotlin Sync] Monitoring > Cluster Monitoring (#65)
1 parent 1704182 commit 471ac7e

File tree

6 files changed

+562
-0
lines changed

6 files changed

+562
-0
lines changed

source/includes/monitoring_JMX.kt

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import com.mongodb.kotlin.client.MongoClient
2+
import org.bson.Document
3+
import com.mongodb.MongoClientSettings
4+
import com.mongodb.ConnectionString
5+
import com.mongodb.management.JMXConnectionPoolListener
6+
7+
fun main() {
8+
val uri = "<connection string uri>"
9+
10+
// Instantiate your JMX listener
11+
val connectionPoolListener = JMXConnectionPoolListener()
12+
13+
// Include the listener in your client settings
14+
val settings = MongoClientSettings.builder()
15+
.applyConnectionString(ConnectionString(uri))
16+
.applyToConnectionPoolSettings {
17+
it.addConnectionPoolListener(connectionPoolListener)
18+
}
19+
.build()
20+
21+
try {
22+
// Connect to your database
23+
val mongoClient = MongoClient.create(settings)
24+
val database = mongoClient.getDatabase("sample_mflix")
25+
val collection = database.getCollection<Document>("movies")
26+
collection.find().firstOrNull()
27+
collection.find().firstOrNull()
28+
println("Navigate to JConsole to see your connection pools...")
29+
30+
// Pause execution
31+
Thread.sleep(Long.MAX_VALUE)
32+
mongoClient.close()
33+
} catch (e: Exception) {
34+
e.printStackTrace()
35+
}
36+
}

source/includes/monitoring_counter.kt

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import com.mongodb.kotlin.client.MongoClient
2+
import org.bson.Document
3+
import com.mongodb.event.*
4+
import com.mongodb.MongoClientSettings
5+
import com.mongodb.ConnectionString
6+
7+
class CommandCounter : CommandListener {
8+
private val commands = mutableMapOf<String, Int>()
9+
10+
override fun commandSucceeded(event: CommandSucceededEvent) {
11+
val commandName = event.commandName
12+
val count = commands[commandName] ?: 0
13+
commands[commandName] = count + 1
14+
println(commands.toString())
15+
}
16+
17+
override fun commandFailed(event: CommandFailedEvent) {
18+
println("Failed execution of command '${event.commandName}' with id ${event.requestId}")
19+
}
20+
}
21+
22+
23+
fun main() {
24+
val uri = "<connection string uri>"
25+
26+
// Instantiate your new listener
27+
val commandCounter = CommandCounter()
28+
29+
// Include the listener in your client settings
30+
val settings = MongoClientSettings.builder()
31+
.applyConnectionString(ConnectionString(uri))
32+
.addCommandListener(commandCounter)
33+
.build()
34+
35+
// Connect to your database
36+
val mongoClient = MongoClient.create(settings)
37+
val database = mongoClient.getDatabase("sample_mflix")
38+
val collection = database.getCollection<Document>("movies")
39+
40+
// Run some commands to test the counter
41+
collection.find().firstOrNull()
42+
collection.find().firstOrNull()
43+
44+
mongoClient.close()
45+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import com.mongodb.kotlin.client.MongoClient
2+
import org.bson.Document
3+
import com.mongodb.event.*
4+
import com.mongodb.MongoClientSettings
5+
import com.mongodb.ConnectionString
6+
7+
class ConnectionPoolLibrarian : ConnectionPoolListener {
8+
override fun connectionCheckedOut(event: ConnectionCheckedOutEvent) {
9+
println("Let me get you the connection with id ${event.connectionId.localValue}...")
10+
}
11+
override fun connectionCheckOutFailed(event: ConnectionCheckOutFailedEvent) {
12+
println("Something went wrong! Failed to checkout connection.")
13+
}
14+
}
15+
16+
17+
fun main() {
18+
val uri = "<connection string uri>"
19+
20+
// Instantiate your new listener
21+
val cpListener = ConnectionPoolLibrarian()
22+
23+
// Include the listener in your client settings
24+
val settings = MongoClientSettings.builder()
25+
.applyConnectionString(ConnectionString(uri))
26+
.applyToConnectionPoolSettings({
27+
it.addConnectionPoolListener(cpListener)
28+
})
29+
.build()
30+
31+
// Connect to your database
32+
val mongoClient = MongoClient.create(settings)
33+
val database = mongoClient.getDatabase("sample_mflix")
34+
val collection = database.getCollection<Document>("movies")
35+
36+
// Run some commands to test the counter
37+
collection.find().firstOrNull()
38+
39+
mongoClient.close()
40+
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import com.mongodb.kotlin.client.MongoClient
2+
import org.bson.Document
3+
import com.mongodb.event.*
4+
import com.mongodb.MongoClientSettings
5+
import com.mongodb.ConnectionString
6+
7+
class IsWriteable : ClusterListener {
8+
private var isWritable = false
9+
10+
override fun clusterDescriptionChanged(event: ClusterDescriptionChangedEvent) {
11+
if (!isWritable) {
12+
if (event.newDescription.hasWritableServer()) {
13+
isWritable = true
14+
println("Able to write to cluster")
15+
}
16+
} else {
17+
if (!event.newDescription.hasWritableServer()) {
18+
isWritable = false
19+
println("Unable to write to cluster")
20+
}
21+
}
22+
}
23+
}
24+
25+
26+
fun main() {
27+
val uri = "<connection string uri>"
28+
29+
// Instantiate your new listener
30+
val clusterListener = IsWriteable()
31+
32+
// Include the listener in your client settings
33+
val settings = MongoClientSettings.builder()
34+
.applyConnectionString(ConnectionString(uri))
35+
.applyToClusterSettings { builder ->
36+
builder.addClusterListener(clusterListener)
37+
}
38+
.build()
39+
40+
// Connect to your database
41+
val mongoClient = MongoClient.create(settings)
42+
val database = mongoClient.getDatabase("sample_mflix")
43+
val collection = database.getCollection<Document>("movies")
44+
45+
// Run a command to trigger a ClusterDescriptionChangedEvent event
46+
collection.find().firstOrNull()
47+
48+
mongoClient.close()
49+
}

source/index.txt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
Specialized Data Formats </data-formats>
2424
Builders </builders>
2525
Run a Command </run-command>
26+
Monitoring </monitoring>
2627
Security </security>
2728
In-Use Encryption </security/encrypt-fields>
2829
Compatibility </compatibility>

0 commit comments

Comments
 (0)