org.springframework.data.mongodb.core.messaging

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 request = new ChangeStreamRequest<>(System.out::println, () -> "collection-name");
     
     
    or MongoDatabase which receives events from all collections in that database.
     
         ChangeStreamRequest 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 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 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