Package io.micronaut.jms.listener
Class JMSListener
java.lang.Object
io.micronaut.jms.listener.JMSListener
Sets up and manages
MessageListeners 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 JMSListenerSuccessHandlers
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
JMSListenerErrorHandlers 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
ConstructorsConstructorDescriptionJMSListener(javax.jms.Session session, javax.jms.MessageListener delegate, JMSDestinationType destinationType, String destination, ExecutorService executor, Optional<String> messageSelector) Creates aJMSListenerinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddErrorHandlers(JMSListenerErrorHandler... handlers) voidaddErrorHandlers(Collection<? extends JMSListenerErrorHandler> handlers) voidaddSuccessHandlers(JMSListenerSuccessHandler... handlers) voidaddSuccessHandlers(Collection<? extends JMSListenerSuccessHandler> handlers) voidstart()Configures the listener to begin listening for messages and processing them.voidstop()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 aJMSListenerinstance. This instance will not begin listening for messages untilstart()is called. The provided session's parentConnectionmust be started (Connection.start()) for the message listener to receive messages.- Parameters:
session- - theSessionfor the messages to be consumed ondelegate- - the listener logic to be invoked. All concurrency, success, and error handling is provided. ThisMessageListenershould extract the necessary data from theMessageand perform application specific logic.destinationType- - theJMSDestinationTypeof the target destinationdestination- - the name of the target destinationexecutor- - theExecutorServiceto 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.
-