Module spring.data.mongodb
Interface MessageListenerContainer
- All Superinterfaces:
Lifecycle,Phased,SmartLifecycle
- All Known Implementing Classes:
DefaultMessageListenerContainer
Internal abstraction used by the framework representing a message listener container. Not meant to
be implemented externally.
- Since:
- 2.1
- Author:
- Christoph Strobl
-
Field Summary
Fields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE -
Method Summary
Modifier and TypeMethodDescriptionstatic MessageListenerContainercreate(MongoTemplate template) Create a newMessageListenerContainergivenMongoTemplate.lookup(SubscriptionRequest<?, ?, ?> request) Lookup the givenSubscriptionRequestwithin the container and return the associatedSubscriptionif present.<S,T> Subscription register(SubscriptionRequest<S, ? super T, ? extends SubscriptionRequest.RequestOptions> request, Class<T> bodyType) Register a newSubscriptionRequestin the container.<S,T> Subscription register(SubscriptionRequest<S, ? super T, ? extends SubscriptionRequest.RequestOptions> request, Class<T> bodyType, ErrorHandler errorHandler) Register a newSubscriptionRequestin the container.default <T> Subscriptionregister(SubscriptionRequest<T, Object, ? extends SubscriptionRequest.RequestOptions> request) Register a newSubscriptionRequestin the container.voidremove(Subscription subscription) Unregister a givenSubscriptionfrom the container.Methods inherited from interface org.springframework.context.SmartLifecycle
getPhase, isAutoStartup, stop
-
Method Details
-
create
Create a newMessageListenerContainergivenMongoTemplate.- Parameters:
template- must not be null.- Returns:
- a new
MessageListenerContainerusingMongoTemplate.
-
register
default <T> Subscription register(SubscriptionRequest<T, Object, ? extends SubscriptionRequest.RequestOptions> request) Register a newSubscriptionRequestin the container. If theis already runningtheSubscriptionwill be added and run immediately, otherwise it'll be scheduled and started once the container is actuallystarted.
Errors duringMessageListenerContainer container = ... MessageListener<ChangeStreamDocument<Document>, Object> messageListener = (message) -> message.... ChangeStreamRequest<Object> request = new ChangeStreamRequest<>(messageListener, () -> "collection-name"); Subscription subscription = container.register(request);Messageretrieval lead tocannelationof the underlying task.- Parameters:
request- must not be null.- Returns:
- never null.
-
register
<S,T> Subscription register(SubscriptionRequest<S, ? super T, ? extends SubscriptionRequest.RequestOptions> request, Class<T> bodyType) Register a newSubscriptionRequestin the container. If theis already runningtheSubscriptionwill be added and run immediately, otherwise it'll be scheduled and started once the container is actuallystarted.
OnMessageListenerContainer container = ... MessageListener<ChangeStreamDocument<Document>, Document> messageListener = (message) -> message.getBody().toJson(); ChangeStreamRequest<Document> request = new ChangeStreamRequest<>(messageListener, () -> "collection-name"); Subscription subscription = container.register(request, Document.class);Lifecycle.stop()allsubscriptionsare cancelled prior to shutting down the container itself.
Registering the very sameSubscriptionRequestmore than once simply returns the already existingSubscription.
Unless aSubscriptionisremovedform the container, theSubscriptionis restarted once the container itself is restarted.
Errors duringMessageretrieval lead tocannelationof the underlying task.- Parameters:
request- must not be null.bodyType- the exact target or a more concrete type of theMessage.getBody().- Returns:
- never null.
-
register
<S,T> Subscription register(SubscriptionRequest<S, ? super T, ? extends SubscriptionRequest.RequestOptions> request, Class<T> bodyType, ErrorHandler errorHandler) Register a newSubscriptionRequestin the container. If theis already runningtheSubscriptionwill be added and run immediately, otherwise it'll be scheduled and started once the container is actuallystarted.
OnMessageListenerContainer container = ... MessageListener<ChangeStreamDocument<Document>, Document> messageListener = (message) -> message.getBody().toJson(); ChangeStreamRequest<Document> request = new ChangeStreamRequest<>(messageListener, () -> "collection-name"); Subscription subscription = container.register(request, Document.class);Lifecycle.stop()allsubscriptionsare cancelled prior to shutting down the container itself.
Registering the very sameSubscriptionRequestmore than once simply returns the already existingSubscription.
Unless aSubscriptionisremovedform the container, theSubscriptionis restarted once the container itself is restarted.
Errors duringMessageretrieval are delegated to the givenErrorHandler.- Parameters:
request- must not be null.bodyType- the exact target or a more concrete type of theMessage.getBody(). Must not be null.errorHandler- the callback to invoke when retrieving theMessagefrom the data source fails for some reason.- Returns:
- never null.
-
remove
Unregister a givenSubscriptionfrom the container. This prevents theSubscriptionto be restarted in a potentialstop/startscenario.
Anactivesubcriptioniscancelledprior to removal.- Parameters:
subscription- must not be null.
-
lookup
Lookup the givenSubscriptionRequestwithin the container and return the associatedSubscriptionif present.- Parameters:
request- must not be null.- Returns:
Optional.empty()if not set.
-