Skip to content

Commit c3c802e

Browse files
committed
Polished replica set examples
1 parent 7b842fe commit c3c802e

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package de.codecentric;
2+
3+
import com.mongodb.MongoClientURI;
4+
5+
abstract public class Connection {
6+
7+
public static final MongoClientURI URI = new MongoClientURI(
8+
"mongodb://mongo1:27001,mongo2:27002,mongo3:27003/test?replicatSet=dev0"
9+
// "mongodb://localhost:27001,localhost:27002,localhost:27003/test?replicatSet=dev0"
10+
);
11+
12+
}

mongodriver/src/main/java/de/codecentric/notifications/DocumentProducer.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
import org.bson.Document;
44

55
import com.mongodb.MongoClient;
6-
import com.mongodb.MongoClientURI;
76
import com.mongodb.client.MongoCollection;
87

8+
import de.codecentric.Connection;
9+
910
public class DocumentProducer {
1011

1112
public static void main(String[] args) throws Exception {
1213

13-
MongoCollection<Document> eventCollection =
14-
new MongoClient(
15-
new MongoClientURI("mongodb://localhost:27001,localhost:27002,localhost:27003/test?replicatSet=demo-dev")
16-
).getDatabase("test").getCollection("events");
14+
try (MongoClient client = new MongoClient(Connection.URI)) {
15+
MongoCollection<Document> eventCollection =
16+
client.getDatabase("test").getCollection("events");
1717

1818
long i = 0;
1919
while (true) {
@@ -24,5 +24,6 @@ public static void main(String[] args) throws Exception {
2424
//System.out.println("inserted: " + doc);
2525
Thread.sleep(2000L + (long)(1000*Math.random()));
2626
}
27+
}
2728
}
2829
}
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
package de.codecentric.notifications;
22

3+
import static com.mongodb.client.model.Filters.and;
4+
import static com.mongodb.client.model.Filters.eq;
5+
import static com.mongodb.client.model.Filters.in;
36
import static java.util.Arrays.asList;
47

58
import org.bson.Document;
69

7-
import com.mongodb.Block;
810
import com.mongodb.MongoClient;
9-
import com.mongodb.MongoClientURI;
1011
import com.mongodb.client.ChangeStreamIterable;
1112
import com.mongodb.client.MongoCollection;
1213
import com.mongodb.client.model.Aggregates;
13-
import static com.mongodb.client.model.Filters.*;
14-
import com.mongodb.client.model.changestream.ChangeStreamDocument;
14+
15+
import de.codecentric.Connection;
1516

1617
public class EventListener {
18+
1719
public static void main(String[] args) throws Exception {
18-
20+
try (MongoClient client = new MongoClient(Connection.URI)) {
1921
MongoCollection<Document> eventCollection =
20-
new MongoClient(
21-
new MongoClientURI("mongodb://localhost:27001,localhost:27002,localhost:27003/test?replicatSet=demo-dev")
22-
).getDatabase("test").getCollection("events");
22+
client.getDatabase("test").getCollection("events");
2323

2424
ChangeStreamIterable<Document> changes = eventCollection.watch(asList(
2525
Aggregates.match( and( asList(
2626
in("operationType", asList("insert")),
2727
eq("fullDocument.even", 1L)))
2828
)));
2929

30-
changes.forEach(new Block<ChangeStreamDocument<Document>>() {
31-
@Override
32-
public void apply(ChangeStreamDocument<Document> t) {
33-
System.out.println("received: " + t.getFullDocument());
34-
}
35-
});
30+
changes.iterator().forEachRemaining(
31+
change -> System.out.println("received: " + change.getFullDocument())
32+
);
33+
}
3634
}
3735
}

mongodriver/src/main/java/de/codecentric/transaction/SimpleTransaction.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import org.bson.Document;
77

88
import com.mongodb.MongoClient;
9-
import com.mongodb.MongoClientURI;
109
import com.mongodb.MongoException;
1110
import com.mongodb.client.ClientSession;
1211
import com.mongodb.client.MongoCollection;
1312
import com.mongodb.client.MongoDatabase;
1413

14+
import de.codecentric.Connection;
15+
1516
/**
1617
* Simple transaction example.
1718
*/
@@ -27,10 +28,7 @@ public class SimpleTransaction {
2728
* @throws UnsupportedEncodingException
2829
*/
2930
public static void main(String[] argv) throws UnknownHostException, MongoException, UnsupportedEncodingException {
30-
try (MongoClient client = new MongoClient(
31-
// Replica set
32-
new MongoClientURI("mongodb://mongo1:27001,mongo2:27002,mongo3:27003/replicaSet=dev0")
33-
)) {
31+
try (MongoClient client = new MongoClient(Connection.URI)) {
3432
// use database "test"
3533
MongoDatabase db = client.getDatabase("test");
3634
// collection must be created beforehand *outside* the transaction!

0 commit comments

Comments
 (0)