Class DefaultMessageListenerContainer
java.lang.Object
org.springframework.data.mongodb.core.messaging.DefaultMessageListenerContainer
- All Implemented Interfaces:
Lifecycle, Phased, SmartLifecycle, MessageListenerContainer
Simple
This message container creates long-running tasks that are executed on
Executor based MessageListenerContainer implementation for running tasks like
listening to MongoDB Change Streams and tailable
cursors. This message container creates long-running tasks that are executed on
Executor.- Since:
- 2.1
- Author:
- Christoph Strobl, Mark Paluch, Junhyeok Lee
-
Field Summary
Fields inherited from interface SmartLifecycle
DEFAULT_PHASE -
Constructor Summary
ConstructorsConstructorDescriptionDefaultMessageListenerContainer(MongoTemplate template) Create a newDefaultMessageListenerContainer.DefaultMessageListenerContainer(MongoTemplate template, Executor taskExecutor) Create a newDefaultMessageListenerContainerrunningtasksvia the given taskExecutor.DefaultMessageListenerContainer(MongoTemplate template, Executor taskExecutor, @Nullable ErrorHandler errorHandler) Create a newDefaultMessageListenerContainerrunningtasksvia the given taskExecutor delegatingerrorsto the givenErrorHandler. -
Method Summary
Modifier and TypeMethodDescriptionintgetPhase()booleanbooleanlookup(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.register(SubscriptionRequest request, Task task) voidremove(Subscription subscription) Unregister a givenSubscriptionfrom the container.voidsetAutoStartup(boolean autoStartup) Set whether to auto-start this container.voidstart()voidstop()voidMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface MessageListenerContainer
registerMethods inherited from interface SmartLifecycle
isPauseable
-
Constructor Details
-
DefaultMessageListenerContainer
Create a newDefaultMessageListenerContainer.- Parameters:
template- must not be null.
-
DefaultMessageListenerContainer
Create a newDefaultMessageListenerContainerrunningtasksvia the given taskExecutor.- Parameters:
template- must not be null.taskExecutor- must not be null.
-
DefaultMessageListenerContainer
public DefaultMessageListenerContainer(MongoTemplate template, Executor taskExecutor, @Nullable ErrorHandler errorHandler) Create a newDefaultMessageListenerContainerrunningtasksvia the given taskExecutor delegatingerrorsto the givenErrorHandler.- Parameters:
template- must not be null. Used by theTaskFactory.taskExecutor- must not be null.errorHandler- the defaultErrorHandlerto be used by tasks inside the container. Can be null.
-
-
Method Details
-
isAutoStartup
public boolean isAutoStartup()- Specified by:
isAutoStartupin interfaceSmartLifecycle
-
setAutoStartup
public void setAutoStartup(boolean autoStartup) Set whether to auto-start this container.Default is
true.- Parameters:
autoStartup-trueto auto-start.- Since:
- 5.0
- See Also:
-
stop
- Specified by:
stopin interfaceSmartLifecycle
-
start
-
stop
-
isRunning
-
getPhase
public int getPhase()- Specified by:
getPhasein interfacePhased- Specified by:
getPhasein interfaceSmartLifecycle
-
register
public <S,T> Subscription register(SubscriptionRequest<S, ? super T, ? extends SubscriptionRequest.RequestOptions> request, Class<T> bodyType) Description copied from interface:MessageListenerContainerRegister 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.- Specified by:
registerin interfaceMessageListenerContainer- Parameters:
request- must not be null.bodyType- the exact target or a more concrete type of theMessage.getBody().- Returns:
- never null.
-
register
public <S,T> Subscription register(SubscriptionRequest<S, ? super T, ? extends SubscriptionRequest.RequestOptions> request, Class<T> bodyType, ErrorHandler errorHandler) Description copied from interface:MessageListenerContainerRegister 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.- Specified by:
registerin interfaceMessageListenerContainer- 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.
-
lookup
Description copied from interface:MessageListenerContainerLookup the givenSubscriptionRequestwithin the container and return the associatedSubscriptionif present.- Specified by:
lookupin interfaceMessageListenerContainer- Parameters:
request- must not be null.- Returns:
Optional.empty()if not set.
-
register
-
remove
Description copied from interface:MessageListenerContainerUnregister a givenSubscriptionfrom the container. This prevents theSubscriptionto be restarted in a potentialstop/startscenario.
Anactivesubcriptioniscancelledprior to removal.- Specified by:
removein interfaceMessageListenerContainer- Parameters:
subscription- must not be null.
-