Class AbstractAdaptableMessageListener

java.lang.Object
org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener
All Implemented Interfaces:
jakarta.jms.MessageListener, SessionAwareMessageListener<jakarta.jms.Message>
Direct Known Subclasses:
MessageListenerAdapter, MessagingMessageListenerAdapter

public abstract class AbstractAdaptableMessageListener extends Object implements jakarta.jms.MessageListener, SessionAwareMessageListener<jakarta.jms.Message>
An abstract JMS MessageListener adapter providing the necessary infrastructure to extract the payload of a JMS Message.
Since:
4.1
Author:
Juergen Hoeller, Stephane Nicoll
See Also:
  • Field Details

    • logger

      protected final org.apache.commons.logging.Log logger
      Logger available to subclasses.
  • Constructor Details

    • AbstractAdaptableMessageListener

      public AbstractAdaptableMessageListener()
  • Method Details

    • setDefaultResponseDestination

      public void setDefaultResponseDestination(jakarta.jms.Destination destination)
      Set the default destination to send response messages to. This will be applied in case of a request message that does not carry a "JMSReplyTo" field.

      Response destinations are only relevant for listener methods that return result objects, which will be wrapped in a response message and sent to a response destination.

      Alternatively, specify a "defaultResponseQueueName" or "defaultResponseTopicName", to be dynamically resolved via the DestinationResolver.

      See Also:
    • setDefaultResponseQueueName

      public void setDefaultResponseQueueName(String destinationName)
      Set the name of the default response queue to send response messages to. This will be applied in case of a request message that does not carry a "JMSReplyTo" field.

      Alternatively, specify a JMS Destination object as "defaultResponseDestination".

      See Also:
    • setDefaultResponseTopicName

      public void setDefaultResponseTopicName(String destinationName)
      Set the name of the default response topic to send response messages to. This will be applied in case of a request message that does not carry a "JMSReplyTo" field.

      Alternatively, specify a JMS Destination object as "defaultResponseDestination".

      See Also:
    • setDestinationResolver

      public void setDestinationResolver(DestinationResolver destinationResolver)
      Set the DestinationResolver that should be used to resolve response destination names for this adapter.

      The default resolver is a SimpleDestinationResolver. Specify a JndiDestinationResolver for resolving destination names as JNDI locations.

      See Also:
    • getDestinationResolver

      protected DestinationResolver getDestinationResolver()
      Return the DestinationResolver for this adapter.
    • setMessageConverter

      public void setMessageConverter(@Nullable MessageConverter messageConverter)
      Set the converter that will convert incoming JMS messages to listener method arguments, and objects returned from listener methods back to JMS messages.

      The default converter is a SimpleMessageConverter, which is able to handle BytesMessages, TextMessages and ObjectMessages.

    • getMessageConverter

      protected @Nullable MessageConverter getMessageConverter()
      Return the converter that will convert incoming JMS messages to listener method arguments, and objects returned from listener methods back to JMS messages.
    • setHeaderMapper

      public void setHeaderMapper(JmsHeaderMapper headerMapper)
      Set the JmsHeaderMapper implementation to use to map the standard JMS headers. By default, a SimpleJmsHeaderMapper is used.
      See Also:
    • getMessagingMessageConverter

      protected final MessagingMessageConverter getMessagingMessageConverter()
      Return the MessagingMessageConverter for this listener, being able to convert Message.
    • setResponseQosSettings

      public void setResponseQosSettings(@Nullable QosSettings responseQosSettings)
      Set the QosSettings to use when sending a response. Can be set to null to indicate that the broker's defaults should be used.
      Parameters:
      responseQosSettings - the QoS settings to use when sending a response or null to use the default values.
      Since:
      5.0
    • getResponseQosSettings

      protected @Nullable QosSettings getResponseQosSettings()
      Return the QosSettings to use when sending a response, or null if the defaults should be used.
      Since:
      5.0
    • onMessage

      public void onMessage(jakarta.jms.Message message)
      Standard JMS MessageListener entry point.

      Delegates the message to the target listener method, with appropriate conversion of the message argument. In case of an exception, the handleListenerException(Throwable) method will be invoked.

      Note: Does not support sending response messages based on result objects returned from listener methods. Use the SessionAwareMessageListener entry point (typically through a Spring message listener container) for handling result objects as well.

      Specified by:
      onMessage in interface jakarta.jms.MessageListener
      Parameters:
      message - the incoming JMS message
      See Also:
    • onMessage

      public abstract void onMessage(jakarta.jms.Message message, @Nullable jakarta.jms.Session session) throws jakarta.jms.JMSException
      Description copied from interface: SessionAwareMessageListener
      Callback for processing a received JMS message.

      Implementors are supposed to process the given Message, typically sending reply messages through the given Session.

      Specified by:
      onMessage in interface SessionAwareMessageListener<jakarta.jms.Message>
      Parameters:
      message - the received JMS message (never null)
      session - the underlying JMS Session (never null)
      Throws:
      jakarta.jms.JMSException - if thrown by JMS methods
    • handleListenerException

      protected void handleListenerException(Throwable ex)
      Handle the given exception that arose during listener execution. The default implementation logs the exception at error level.

      This method only applies when used as standard JMS MessageListener. In case of the Spring SessionAwareMessageListener mechanism, exceptions get handled by the caller instead.

      Parameters:
      ex - the exception to handle
      See Also:
    • extractMessage

      protected Object extractMessage(jakarta.jms.Message message)
      Extract the message body from the given JMS message.
      Parameters:
      message - the JMS Message
      Returns:
      the content of the message, to be passed into the listener method as an argument
      Throws:
      MessageConversionException - if the message could not be extracted
    • handleResult

      protected void handleResult(Object result, jakarta.jms.Message request, @Nullable jakarta.jms.Session session)
      Handle the given result object returned from the listener method, sending a response message back.
      Parameters:
      result - the result object to handle (never null)
      request - the original request message
      session - the JMS Session to operate on (may be null)
      Throws:
      ReplyFailureException - if the response message could not be sent
      See Also:
    • buildMessage

      protected jakarta.jms.Message buildMessage(jakarta.jms.Session session, Object result) throws jakarta.jms.JMSException
      Build a JMS message to be sent as response based on the given result object.
      Parameters:
      session - the JMS Session to operate on
      result - the content of the message, as returned from the listener method
      Returns:
      the JMS Message (never null)
      Throws:
      jakarta.jms.JMSException - if thrown by JMS API methods
      See Also:
    • preProcessResponse

      protected Object preProcessResponse(Object result)
      Pre-process the given result before it is converted to a Message.
      Parameters:
      result - the result of the invocation
      Returns:
      the payload response to handle, either the result argument or any other object (for instance wrapping the result).
      Since:
      4.3
    • postProcessResponse

      protected void postProcessResponse(jakarta.jms.Message request, jakarta.jms.Message response) throws jakarta.jms.JMSException
      Post-process the given response message before it will be sent.

      The default implementation sets the response's correlation id to the request message's correlation id, if any; otherwise to the request message id.

      Parameters:
      request - the original incoming JMS message
      response - the outgoing JMS message about to be sent
      Throws:
      jakarta.jms.JMSException - if thrown by JMS API methods
      See Also:
      • Message.setJMSCorrelationID(String)
    • getResponseDestination

      protected jakarta.jms.Destination getResponseDestination(jakarta.jms.Message request, jakarta.jms.Message response, jakarta.jms.Session session) throws jakarta.jms.JMSException
      Determine a response destination for the given message.

      The default implementation first checks the JMS Reply-To Destination of the supplied request; if that is not null it is returned; if it is null, then the configured default response destination is returned; if this too is null, then an InvalidDestinationException is thrown.

      Parameters:
      request - the original incoming JMS message
      response - the outgoing JMS message about to be sent
      session - the JMS Session to operate on
      Returns:
      the response destination (never null)
      Throws:
      jakarta.jms.JMSException - if thrown by JMS API methods
      jakarta.jms.InvalidDestinationException - if no Destination can be determined
      See Also:
    • resolveDefaultResponseDestination

      protected @Nullable jakarta.jms.Destination resolveDefaultResponseDestination(jakarta.jms.Session session) throws jakarta.jms.JMSException
      Resolve the default response destination into a JMS Destination, using this accessor's DestinationResolver in case of a destination name.
      Returns:
      the located Destination
      Throws:
      jakarta.jms.JMSException - if resolution failed
      See Also:
    • sendResponse

      protected void sendResponse(jakarta.jms.Session session, jakarta.jms.Destination destination, jakarta.jms.Message response) throws jakarta.jms.JMSException
      Send the given response message to the given destination.
      Parameters:
      session - the JMS session to operate on
      destination - the JMS destination to send to
      response - the JMS message to send
      Throws:
      jakarta.jms.JMSException - if thrown by JMS API methods
      See Also:
    • postProcessProducer

      protected void postProcessProducer(jakarta.jms.MessageProducer producer, jakarta.jms.Message response) throws jakarta.jms.JMSException
      Post-process the given message producer before using it to send the response.

      The default implementation is empty.

      Parameters:
      producer - the JMS message producer that will be used to send the message
      response - the outgoing JMS message about to be sent
      Throws:
      jakarta.jms.JMSException - if thrown by JMS API methods