use of com.mongodb.client.model.changestream.ChangeStreamDocument in project mongo-java-driver by mongodb.
the class AbstractChangeStreamsTest method shouldPassAllOutcomes.
@Test
public void shouldPassAllOutcomes() throws InterruptedException {
BsonDocument result = definition.getDocument("result");
MongoCursor<ChangeStreamDocument<BsonDocument>> cursor = createCursor(result);
commandListener.waitForFirstCommandCompletion();
handleOperations();
if (cursor != null) {
try {
checkStreamValues(result, cursor);
} finally {
cursor.close();
}
}
checkExpectations();
}
use of com.mongodb.client.model.changestream.ChangeStreamDocument in project mongo-java-driver by mongodb.
the class DocumentationSamples method testWatch.
@Test
public void testWatch() throws InterruptedException {
assumeTrue(isDiscoverableReplicaSet() && serverVersionAtLeast(3, 6));
final MongoCollection<Document> inventory = collection;
final AtomicBoolean stop = new AtomicBoolean(false);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (!stop.get()) {
collection.insertMany(asList(new Document("username", "alice"), new Document()));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// ignore
}
collection.deleteOne(new Document("username", "alice"));
}
}
});
thread.start();
// Start Changestream Example 1
MongoCursor<ChangeStreamDocument<Document>> cursor = inventory.watch().iterator();
ChangeStreamDocument<Document> next = cursor.next();
// End Changestream Example 1
cursor.close();
// Start Changestream Example 2
cursor = inventory.watch().fullDocument(FullDocument.UPDATE_LOOKUP).iterator();
next = cursor.next();
// End Changestream Example 2
cursor.close();
// Start Changestream Example 3
BsonDocument resumeToken = next.getResumeToken();
cursor = inventory.watch().resumeAfter(resumeToken).iterator();
next = cursor.next();
// End Changestream Example 3
cursor.close();
// Start Changestream Example 4
List<Bson> pipeline = asList(match(Document.parse("{'fullDocument.username': 'alice'}")), addFields(new Field<String>("newField", "this is an added field!")));
cursor = inventory.watch(pipeline).iterator();
next = cursor.next();
// End Changestream Example 4
cursor.close();
stop.set(true);
thread.join();
}
use of com.mongodb.client.model.changestream.ChangeStreamDocument in project spring-data-mongodb by spring-projects.
the class ChangeStreamTests method plainAggregationPipelineToFilterMessages.
// DATAMONGO-1803
@Test
void plainAggregationPipelineToFilterMessages() throws InterruptedException {
CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener = new CollectingMessageListener<>();
ChangeStreamRequest<User> request = //
ChangeStreamRequest.builder().collection(//
"user").publishTo(//
messageListener).filter(//
new Document("$match", new Document("fullDocument.user_name", "sugarSplashy"))).maxAwaitTime(//
Duration.ofMillis(10)).build();
Subscription subscription = container.register(request, User.class);
awaitSubscription(subscription);
template.save(jellyBelly);
template.save(sugarSplashy);
template.save(huffyFluffy);
awaitMessages(messageListener, 1);
List<User> messageBodies = messageListener.getMessages().stream().map(Message::getBody).collect(Collectors.toList());
assertThat(messageBodies).hasSize(1).containsExactly(sugarSplashy);
}
use of com.mongodb.client.model.changestream.ChangeStreamDocument in project spring-data-mongodb by spring-projects.
the class ChangeStreamTests method readsAndConvertsUpdateMessageBodyCorrectly.
// DATAMONGO-1803
@Test
void readsAndConvertsUpdateMessageBodyCorrectly() throws InterruptedException {
CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener = new CollectingMessageListener<>();
ChangeStreamRequest<User> request = new ChangeStreamRequest<>(messageListener, new ChangeStreamRequestOptions(null, "user", Duration.ofMillis(10), ChangeStreamOptions.builder().build()));
Subscription subscription = container.register(request, User.class);
awaitSubscription(subscription);
template.save(jellyBelly);
template.update(User.class).matching(query(where("id").is(jellyBelly.id))).apply(Update.update("age", 8)).first();
awaitMessages(messageListener, 2);
assertThat(messageListener.getFirstMessage().getBody()).isEqualTo(jellyBelly);
assertThat(messageListener.getLastMessage().getBody()).isNotNull().hasFieldOrPropertyWithValue("age", 8);
}
use of com.mongodb.client.model.changestream.ChangeStreamDocument in project spring-data-mongodb by spring-projects.
the class ChangeStreamTests method readsAndConvertsMessageBodyCorrectly.
// DATAMONGO-1803
@Test
void readsAndConvertsMessageBodyCorrectly() throws InterruptedException {
CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener = new CollectingMessageListener<>();
ChangeStreamRequest<User> request = new ChangeStreamRequest<>(messageListener, new ChangeStreamRequestOptions(null, "user", Duration.ofMillis(10), ChangeStreamOptions.builder().build()));
Subscription subscription = container.register(request, User.class);
awaitSubscription(subscription);
template.save(jellyBelly);
awaitMessages(messageListener, 1);
Message<ChangeStreamDocument<Document>, User> message1 = messageListener.getFirstMessage();
assertThat(message1.getRaw()).isNotNull();
assertThat(message1.getProperties()).isEqualTo(MessageProperties.builder().collectionName("user").databaseName("change-stream-tests").build());
assertThat(message1.getBody()).isEqualTo(jellyBelly);
}
Aggregations