Class ChangeStreamRequest<T>
java.lang.Object
org.springframework.data.mongodb.core.messaging.ChangeStreamRequest<T>
- All Implemented Interfaces:
SubscriptionRequest<com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.Document>,
T, ChangeStreamRequest.ChangeStreamRequestOptions>
public class ChangeStreamRequest<T>
extends Object
implements SubscriptionRequest<com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.Document>,T,ChangeStreamRequest.ChangeStreamRequestOptions>
SubscriptionRequest
implementation to be used for listening to
Change Streams via a MessageListenerContainer
using the synchronous MongoDB Java driver.
The most trivial use case is subscribing to all events of a specific
collection
ChangeStreamRequest<Document> request = new ChangeStreamRequest<>(System.out::println, () -> "collection-name");
or MongoDatabase
which receives events from all collections
in that database.
ChangeStreamRequest<Document> request = new ChangeStreamRequest<>(System.out::println, RequestOptions.justDatabase("test"));
For more advanced scenarios ChangeStreamOptions
offers abstractions for options like filtering, resuming,...
ChangeStreamOptions options = ChangeStreamOptions.builder()
.filter(newAggregation(match(where("age").is(7))))
.returnFullDocumentOnUpdate()
.build();
ChangeStreamRequest<Document> request = new ChangeStreamRequest<>(System.out::println, new ChangeStreamRequestOptions("collection-name", options));
ChangeStreamRequest.ChangeStreamRequestBuilder
offers a fluent API for creating ChangeStreamRequest
with
ChangeStreamOptions
in one go.
ChangeStreamRequest<Document> request = ChangeStreamRequest.builder()
.collection("collection-name")
.publishTo(System.out::println)
.filter(newAggregation(match(where("age").is(7))))
.fullDocumentLookup(UPDATE_LOOKUP)
.build();
Messges
passed to the MessageListener
contain the ChangeStreamDocument
within their
raw value
while the fullDocument
is extracted into the messages body
. Unless otherwise specified (via ChangeStreamOptions.getFullDocumentLookup()
the
message body
for update events
will be empty for a Document
target type.
Message.getBody()
Message bodies} that map to a different target type automatically enforce an
FullDocument.UPDATE_LOOKUP
.- Since:
- 2.1
- Author:
- Christoph Strobl, Mark Paluch, Myroslav Kosinskyi
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Builder for creatingChangeStreamRequest
.static class
SubscriptionRequest.RequestOptions
implementation specific to aChangeStreamRequest
.Nested classes/interfaces inherited from interface org.springframework.data.mongodb.core.messaging.SubscriptionRequest
SubscriptionRequest.RequestOptions
-
Constructor Summary
ConstructorDescriptionChangeStreamRequest
(MessageListener<com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.Document>, ? super T> messageListener, SubscriptionRequest.RequestOptions options) -
Method Summary
Modifier and TypeMethodDescriptionbuilder()
Obtain a shiny newChangeStreamRequest.ChangeStreamRequestBuilder
and start defining yourChangeStreamRequest
in this fancy fluent way.static <T> ChangeStreamRequest.ChangeStreamRequestBuilder<T>
builder
(MessageListener<com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.Document>, ? super T> listener) Obtain a shiny newChangeStreamRequest.ChangeStreamRequestBuilder
and start defining yourChangeStreamRequest
in this fancy fluent way.MessageListener<com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.Document>,
? super T> Obtain theMessageListener
to publishmessages
to.Get theSubscriptionRequest.RequestOptions
specifying the requests behaviour.
-
Constructor Details
-
ChangeStreamRequest
public ChangeStreamRequest(MessageListener<com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.Document>, ? super T> messageListener, SubscriptionRequest.RequestOptions options) - Parameters:
messageListener
- must not be null.options
- must not be null.
-
-
Method Details
-
getMessageListener
public MessageListener<com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.Document>,? super T> getMessageListener()Description copied from interface:SubscriptionRequest
Obtain theMessageListener
to publishmessages
to.- Specified by:
getMessageListener
in interfaceSubscriptionRequest<com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.Document>,
T, ChangeStreamRequest.ChangeStreamRequestOptions> - Returns:
- never null.
-
getRequestOptions
Description copied from interface:SubscriptionRequest
Get theSubscriptionRequest.RequestOptions
specifying the requests behaviour.- Specified by:
getRequestOptions
in interfaceSubscriptionRequest<com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.Document>,
T, ChangeStreamRequest.ChangeStreamRequestOptions> - Returns:
- never null.
-
builder
Obtain a shiny newChangeStreamRequest.ChangeStreamRequestBuilder
and start defining yourChangeStreamRequest
in this fancy fluent way. Just don't forget to callbuild()
when done.- Returns:
- new instance of
ChangeStreamRequest
.
-
builder
public static <T> ChangeStreamRequest.ChangeStreamRequestBuilder<T> builder(MessageListener<com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.Document>, ? super T> listener) Obtain a shiny newChangeStreamRequest.ChangeStreamRequestBuilder
and start defining yourChangeStreamRequest
in this fancy fluent way. Just don't forget to callbuild()
when done.- Returns:
- new instance of
ChangeStreamRequest
.
-