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
-
Field Summary
Fields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE
-
Constructor Summary
ConstructorDescriptionDefaultMessageListenerContainer
(MongoTemplate template) Create a newDefaultMessageListenerContainer
.DefaultMessageListenerContainer
(MongoTemplate template, Executor taskExecutor) Create a newDefaultMessageListenerContainer
runningtasks
via the given taskExecutor.DefaultMessageListenerContainer
(MongoTemplate template, Executor taskExecutor, ErrorHandler errorHandler) Create a newDefaultMessageListenerContainer
runningtasks
via the given taskExecutor delegatingerrors
to the givenErrorHandler
. -
Method Summary
Modifier and TypeMethodDescriptionint
getPhase()
boolean
boolean
lookup
(SubscriptionRequest<?, ?, ?> request) Lookup the givenSubscriptionRequest
within the container and return the associatedSubscription
if present.<S,
T> Subscription register
(SubscriptionRequest<S, ? super T, ? extends SubscriptionRequest.RequestOptions> request, Class<T> bodyType) Register a newSubscriptionRequest
in the container.<S,
T> Subscription register
(SubscriptionRequest<S, ? super T, ? extends SubscriptionRequest.RequestOptions> request, Class<T> bodyType, ErrorHandler errorHandler) Register a newSubscriptionRequest
in the container.register
(SubscriptionRequest request, Task task) void
remove
(Subscription subscription) Unregister a givenSubscription
from the container.void
start()
void
stop()
void
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.data.mongodb.core.messaging.MessageListenerContainer
register
-
Constructor Details
-
DefaultMessageListenerContainer
Create a newDefaultMessageListenerContainer
.- Parameters:
template
- must not be null.
-
DefaultMessageListenerContainer
Create a newDefaultMessageListenerContainer
runningtasks
via 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 newDefaultMessageListenerContainer
runningtasks
via the given taskExecutor delegatingerrors
to the givenErrorHandler
.- Parameters:
template
- must not be null. Used by theTaskFactory
.taskExecutor
- must not be null.errorHandler
- the defaultErrorHandler
to be used by tasks inside the container. Can be null.
-
-
Method Details
-
isAutoStartup
public boolean isAutoStartup()- Specified by:
isAutoStartup
in interfaceSmartLifecycle
-
stop
- Specified by:
stop
in interfaceSmartLifecycle
-
start
public void start() -
stop
public void stop() -
isRunning
public boolean isRunning() -
getPhase
public int getPhase()- Specified by:
getPhase
in interfacePhased
- Specified by:
getPhase
in interfaceSmartLifecycle
-
register
public <S,T> Subscription register(SubscriptionRequest<S, ? super T, ? extends SubscriptionRequest.RequestOptions> request, Class<T> bodyType) Description copied from interface:MessageListenerContainer
Register a newSubscriptionRequest
in the container. If theis already running
theSubscription
will be added and run immediately, otherwise it'll be scheduled and started once the container is actuallystarted
.MessageListenerContainer 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()
allsubscriptions
are cancelled prior to shutting down the container itself.
Registering the very sameSubscriptionRequest
more than once simply returns the already existingSubscription
.
Unless aSubscription
isremoved
form the container, theSubscription
is restarted once the container itself is restarted.
Errors duringMessage
retrieval lead tocannelation
of the underlying task.- Specified by:
register
in 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:MessageListenerContainer
Register a newSubscriptionRequest
in the container. If theis already running
theSubscription
will be added and run immediately, otherwise it'll be scheduled and started once the container is actuallystarted
.MessageListenerContainer 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()
allsubscriptions
are cancelled prior to shutting down the container itself.
Registering the very sameSubscriptionRequest
more than once simply returns the already existingSubscription
.
Unless aSubscription
isremoved
form the container, theSubscription
is restarted once the container itself is restarted.
Errors duringMessage
retrieval are delegated to the givenErrorHandler
.- Specified by:
register
in 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 theMessage
from the data source fails for some reason.- Returns:
- never null.
-
lookup
Description copied from interface:MessageListenerContainer
Lookup the givenSubscriptionRequest
within the container and return the associatedSubscription
if present.- Specified by:
lookup
in interfaceMessageListenerContainer
- Parameters:
request
- must not be null.- Returns:
Optional.empty()
if not set.
-
register
-
remove
Description copied from interface:MessageListenerContainer
Unregister a givenSubscription
from the container. This prevents theSubscription
to be restarted in a potentialstop
/start
scenario.
Anactive
subcription
iscancelled
prior to removal.- Specified by:
remove
in interfaceMessageListenerContainer
- Parameters:
subscription
- must not be null.
-