Package io.micronaut.jms.listener
Class JMSListener
java.lang.Object
io.micronaut.jms.listener.JMSListener
Sets up and manages
MessageListener
s created by the JMSListener
and
AbstractJMSListenerMethodProcessor
processing.
Additional handlers can be added to inject custom success and error handling cases (see TransactionalJMSListenerErrorHandler
,
TransactionalJMSListenerSuccessHandler
, AcknowledgingJMSListenerSuccessHandler
, and LoggingJMSListenerErrorHandler
)
using the addSuccessHandlers(JMSListenerSuccessHandler...)
and addErrorHandlers(JMSListenerErrorHandler...)
methods.
Once a message is successfully processed by the delegate
then all the JMSListenerSuccessHandler
s
are called sequentially. If a handler throws an error then it is caught and once all handlers have completed, those errors
are rethrown
If any error is thrown during message handling (either by the listener itself, or by a success handler), then all the
JMSListenerErrorHandler
s are called sequentially.
Note: To handle the special cases (e.g.the negative acknowledger feature in the AWS SQS base implementation), an error handler could throw an error to expose it to the base implementation.
However, it must be ensured that the error handler executes after all other error handlers.- Since:
- 2.1.1
- Author:
- Elliott Pope
-
Constructor Summary
ConstructorDescriptionJMSListener
(javax.jms.Session session, javax.jms.MessageListener delegate, JMSDestinationType destinationType, String destination, ExecutorService executor, Optional<String> messageSelector) Creates aJMSListener
instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addErrorHandlers
(JMSListenerErrorHandler... handlers) void
addErrorHandlers
(Collection<? extends JMSListenerErrorHandler> handlers) void
addSuccessHandlers
(JMSListenerSuccessHandler... handlers) void
addSuccessHandlers
(Collection<? extends JMSListenerSuccessHandler> handlers) void
start()
Configures the listener to begin listening for messages and processing them.void
stop()
Stops the listener from consuming messages and attempts to clean up any resources used.
-
Constructor Details
-
JMSListener
public JMSListener(javax.jms.Session session, javax.jms.MessageListener delegate, JMSDestinationType destinationType, String destination, ExecutorService executor, Optional<String> messageSelector) Creates aJMSListener
instance. This instance will not begin listening for messages untilstart()
is called. The provided session's parentConnection
must be started (Connection.start()
) for the message listener to receive messages.- Parameters:
session
- - theSession
for the messages to be consumed ondelegate
- - the listener logic to be invoked. All concurrency, success, and error handling is provided. ThisMessageListener
should extract the necessary data from theMessage
and perform application specific logic.destinationType
- - theJMSDestinationType
of the target destinationdestination
- - the name of the target destinationexecutor
- - theExecutorService
to perform the message handling logic on. The message handling, including success and error handling, is performed on threads managed by this executor.messageSelector
- the message selector for the listener
-
-
Method Details
-
addSuccessHandlers
- Parameters:
handlers
- - add the given handlers to the success handlers for this listener. The handlers will be added such that theJMSListenerSuccessHandler.getOrder()
is decreasing along the list.
-
addSuccessHandlers
- Parameters:
handlers
- - add the given handlers to the success handlers for this listener. The handlers will be added such that theJMSListenerSuccessHandler.getOrder()
is decreasing along the list.
-
addErrorHandlers
- Parameters:
handlers
- - add the given handlers to the error handlers for this listener. The handlers will be added such that theOrdered.getOrder()
is decreasing along the list.
-
addErrorHandlers
- Parameters:
handlers
- - add the given handlers to the error handlers for this listener. The handlers will be added such that theOrdered.getOrder()
is decreasing along the list.
-
start
public void start() throws javax.jms.JMSExceptionConfigures the listener to begin listening for messages and processing them.- Throws:
javax.jms.JMSException
- - if any JMS related exception occurs while configuring the listener.
-
stop
public void stop() throws javax.jms.JMSExceptionStops the listener from consuming messages and attempts to clean up any resources used.- Throws:
javax.jms.JMSException
- - if any error occurs while shutting down the listener.
-