org.springframework.jms.listener.adapter

## Class AbstractAdaptableMessageListener

- java.lang.Object
- - org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener

- All Implemented Interfaces:
    MessageListener, SessionAwareMessageListener<Message>
  Direct Known Subclasses:
    MessageListenerAdapter, MessagingMessageListenerAdapter

      public abstract class AbstractAdaptableMessageListener
      extends Object
      implements MessageListener, SessionAwareMessageListener<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:
    `MessageListener`, `SessionAwareMessageListener`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Log` | `logger` Logger available to subclasses.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractAdaptableMessageListener()`

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected Message` | `buildMessage(Session session, Object result)` Build a JMS message to be sent as response based on the given result object.
    `protected Object` | `extractMessage(Message message)` Extract the message body from the given JMS message.
    `protected DestinationResolver` | `getDestinationResolver()` Return the DestinationResolver for this adapter.
    `protected 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.
    `protected MessagingMessageConverter` | `getMessagingMessageConverter()` Return the `MessagingMessageConverter` for this listener, being able to convert `Message`.
    `protected Destination` | `getResponseDestination(Message request, Message response, Session session)` Determine a response destination for the given message.
    `protected QosSettings` | `getResponseQosSettings()` Return the `QosSettings` to use when sending a response, or `null` if the defaults should be used.
    `protected void` | `handleListenerException(Throwable ex)` Handle the given exception that arose during listener execution.
    `protected void` | `handleResult(Object result, Message request, Session session)` Handle the given result object returned from the listener method, sending a response message back.
    `void` | `onMessage(Message message)` Standard JMS `MessageListener` entry point.
    `abstract void` | `onMessage(Message message, Session session)` Callback for processing a received JMS message.
    `protected void` | `postProcessProducer(MessageProducer producer, Message response)` Post-process the given message producer before using it to send the response.
    `protected void` | `postProcessResponse(Message request, Message response)` Post-process the given response message before it will be sent.
    `protected Object` | `preProcessResponse(Object result)` Pre-process the given result before it is converted to a `Message`.
    `protected Destination` | `resolveDefaultResponseDestination(Session session)` Resolve the default response destination into a JMS `Destination`, using this accessor's `DestinationResolver` in case of a destination name.
    `protected void` | `sendResponse(Session session, Destination destination, Message response)` Send the given response message to the given destination.
    `void` | `setDefaultResponseDestination(Destination destination)` Set the default destination to send response messages to.
    `void` | `setDefaultResponseQueueName(String destinationName)` Set the name of the default response queue to send response messages to.
    `void` | `setDefaultResponseTopicName(String destinationName)` Set the name of the default response topic to send response messages to.
    `void` | `setDestinationResolver(DestinationResolver destinationResolver)` Set the DestinationResolver that should be used to resolve response destination names for this adapter.
    `void` | `setHeaderMapper(JmsHeaderMapper headerMapper)` Set the `JmsHeaderMapper` implementation to use to map the standard JMS headers.
    `void` | `setMessageConverter(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.
    `void` | `setResponseQosSettings(QosSettings responseQosSettings)` Set the `QosSettings` to use when sending a response.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### logger

          protected final Log logger

      Logger available to subclasses.

  - ### Constructor Detail

    - #### AbstractAdaptableMessageListener

          public AbstractAdaptableMessageListener()

  - ### Method Detail

    - #### setDefaultResponseDestination

          public void setDefaultResponseDestination(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(String)`, `setDefaultResponseTopicName(String)`, `getResponseDestination(javax.jms.Message, javax.jms.Message, javax.jms.Session, java.lang.Object)`

    - #### 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:
        `setDestinationResolver(org.springframework.jms.support.destination.DestinationResolver)`, `setDefaultResponseDestination(javax.jms.Destination)`

    - #### 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(org.springframework.jms.support.destination.DestinationResolver)`, `setDefaultResponseDestination(javax.jms.Destination)`

    - #### 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 DynamicDestinationResolver. Specify a JndiDestinationResolver for resolving destination names as JNDI locations.

      See Also:
        `DynamicDestinationResolver`, `JndiDestinationResolver`

    - #### 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

          @Nullable
          protected 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:
        `SimpleJmsHeaderMapper`

    - #### 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

          @Nullable
          protected 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(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 `MessageListener`
      Parameters:
        `message` - the incoming JMS message
      See Also:
        `handleListenerException(java.lang.Throwable)`, `onMessage(javax.jms.Message, javax.jms.Session)`

    - #### onMessage

          public abstract void onMessage(Message message,
                                         @Nullable
                                         Session session)
                                  throws 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<Message>`
      Parameters:
        `message` - the received JMS message (never `null`)
        `session` - the underlying JMS Session (never `null`)
      Throws:
        `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:
        `onMessage(javax.jms.Message)`

    - #### extractMessage

          protected Object extractMessage(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,
                                      Message request,
                                      @Nullable
                                      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(javax.jms.Session, java.lang.Object)`, `postProcessResponse(javax.jms.Message, javax.jms.Message)`, `getResponseDestination(javax.jms.Message, javax.jms.Message, javax.jms.Session, java.lang.Object)`, `sendResponse(javax.jms.Session, javax.jms.Destination, javax.jms.Message)`

    - #### buildMessage

          protected Message buildMessage(Session session,
                                         Object result)
                                  throws 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:
        `JMSException` - if thrown by JMS API methods
      See Also:
        `setMessageConverter(org.springframework.jms.support.converter.MessageConverter)`

    - #### 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(Message request,
                                             Message response)
                                      throws 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:
        `JMSException` - if thrown by JMS API methods
      See Also:
        `Message.setJMSCorrelationID(java.lang.String)`

    - #### getResponseDestination

          protected Destination getResponseDestination(Message request,
                                                       Message response,
                                                       Session session)
                                                throws 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:
        `JMSException` - if thrown by JMS API methods
        `InvalidDestinationException` - if no `Destination` can be determined
      See Also:
        `setDefaultResponseDestination(javax.jms.Destination)`, `Message.getJMSReplyTo()`

    - #### resolveDefaultResponseDestination

          @Nullable
          protected Destination resolveDefaultResponseDestination(Session session)
                                                                     throws 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:
        `JMSException` - if resolution failed
      See Also:
        `setDefaultResponseDestination(javax.jms.Destination)`, `setDefaultResponseQueueName(java.lang.String)`, `setDefaultResponseTopicName(java.lang.String)`, `setDestinationResolver(org.springframework.jms.support.destination.DestinationResolver)`

    - #### sendResponse

          protected void sendResponse(Session session,
                                      Destination destination,
                                      Message response)
                               throws JMSException

      Send the given response message to the given destination.
      Parameters:
        `response` - the JMS message to send
        `destination` - the JMS destination to send to
        `session` - the JMS session to operate on
      Throws:
        `JMSException` - if thrown by JMS API methods
      See Also:
        `postProcessProducer(javax.jms.MessageProducer, javax.jms.Message)`, `Session.createProducer(javax.jms.Destination)`, `MessageProducer.send(javax.jms.Message)`

    - #### postProcessProducer

          protected void postProcessProducer(MessageProducer producer,
                                             Message response)
                                      throws 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:
        `JMSException` - if thrown by JMS API methods