Class JMSListener

java.lang.Object
io.micronaut.jms.listener.JMSListener

public class JMSListener extends Object
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 Details

    • JMSListener

      public JMSListener(jakarta.jms.Session session, jakarta.jms.MessageListener delegate, JMSDestinationType destinationType, String destination, ExecutorService executor, Optional<String> messageSelector)
      Creates a JMSListener instance. This instance will not begin listening for messages until start() is called. The provided session's parent Connection must be started (Connection.start()) for the message listener to receive messages.
      Parameters:
      session - - the Session for the messages to be consumed on
      delegate - - the listener logic to be invoked. All concurrency, success, and error handling is provided. This MessageListener should extract the necessary data from the Message and perform application specific logic.
      destinationType - - the JMSDestinationType of the target destination
      destination - - the name of the target destination
      executor - - the ExecutorService 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

      public void addSuccessHandlers(JMSListenerSuccessHandler... handlers)
      Parameters:
      handlers - - add the given handlers to the success handlers for this listener. The handlers will be added such that the JMSListenerSuccessHandler.getOrder() is decreasing along the list.
    • addSuccessHandlers

      public void addSuccessHandlers(Collection<? extends JMSListenerSuccessHandler> handlers)
      Parameters:
      handlers - - add the given handlers to the success handlers for this listener. The handlers will be added such that the JMSListenerSuccessHandler.getOrder() is decreasing along the list.
    • addErrorHandlers

      public void addErrorHandlers(JMSListenerErrorHandler... handlers)
      Parameters:
      handlers - - add the given handlers to the error handlers for this listener. The handlers will be added such that the Ordered.getOrder() is decreasing along the list.
    • addErrorHandlers

      public void addErrorHandlers(Collection<? extends JMSListenerErrorHandler> handlers)
      Parameters:
      handlers - - add the given handlers to the error handlers for this listener. The handlers will be added such that the Ordered.getOrder() is decreasing along the list.
    • start

      public void start() throws jakarta.jms.JMSException
      Configures the listener to begin listening for messages and processing them.
      Throws:
      jakarta.jms.JMSException - - if any JMS related exception occurs while configuring the listener.
    • stop

      public void stop() throws jakarta.jms.JMSException
      Stops the listener from consuming messages and attempts to clean up any resources used.
      Throws:
      jakarta.jms.JMSException - - if any error occurs while shutting down the listener.