public abstract class AbstractAdaptableMessageListener extends Object implements MessageListener, ChannelAwareMessageListener
MessageListener
adapter providing the necessary infrastructure
to extract the payload of a Message
.MessageListener
,
ChannelAwareMessageListener
Modifier and Type | Class and Description |
---|---|
static class |
AbstractAdaptableMessageListener.ReplyExpressionRoot
Root object for reply expression evaluation.
|
static class |
AbstractAdaptableMessageListener.ResultHolder
Result holder.
|
Modifier and Type | Field and Description |
---|---|
protected org.apache.commons.logging.Log |
logger
Logger available to subclasses.
|
Constructor and Description |
---|
AbstractAdaptableMessageListener() |
Modifier and Type | Method and Description |
---|---|
protected Message |
buildMessage(com.rabbitmq.client.Channel channel,
Object result)
Build a Rabbit message to be sent as response based on the given result object.
|
protected Object |
extractMessage(Message message)
Extract the message body from the given Rabbit message.
|
String |
getEncoding()
The encoding to use when inter-converting between byte arrays and Strings in
message properties.
|
protected MessageConverter |
getMessageConverter()
Return the converter that will convert incoming Rabbit messages to listener method arguments, and objects
returned from listener methods back to Rabbit messages.
|
protected String |
getReceivedExchange(Message request) |
protected Address |
getReplyToAddress(Message request,
Object source,
Object result)
Determine a reply-to Address for the given message.
|
protected void |
handleListenerException(Throwable ex)
Handle the given exception that arose during listener execution.
|
protected void |
handleResult(Object resultArg,
Message request,
com.rabbitmq.client.Channel channel)
Handle the given result object returned from the listener method, sending a
response message back.
|
protected void |
handleResult(Object resultArg,
Message request,
com.rabbitmq.client.Channel channel,
Object source)
Handle the given result object returned from the listener method, sending a
response message back.
|
void |
onMessage(Message message)
Rabbit
MessageListener entry point. |
protected void |
postProcessChannel(com.rabbitmq.client.Channel channel,
Message response)
Post-process the given message before sending the response.
|
protected void |
postProcessResponse(Message request,
Message response)
Post-process the given response message before it will be sent.
|
protected void |
sendResponse(com.rabbitmq.client.Channel channel,
Address replyTo,
Message messageIn)
Send the given response message to the given destination.
|
void |
setBeanResolver(org.springframework.expression.BeanResolver beanResolver)
Set a bean resolver for runtime SpEL expressions.
|
void |
setEncoding(String encoding)
The encoding to use when inter-converting between byte arrays and Strings in
message properties.
|
void |
setMandatoryPublish(boolean mandatoryPublish) |
void |
setMessageConverter(MessageConverter messageConverter)
Set the converter that will convert incoming Rabbit messages to listener method arguments, and objects returned
from listener methods back to Rabbit messages.
|
void |
setReplyPostProcessor(MessagePostProcessor replyPostProcessor)
Set a post processor to process the reply immediately before
Channel#basicPublish() . |
void |
setResponseAddress(String defaultReplyTo)
Set the default replyTo address to use when sending response messages.
|
void |
setResponseExchange(String responseExchange)
Set the exchange to use when sending response messages.
|
void |
setResponseRoutingKey(String responseRoutingKey)
Set the routing key to use when sending response messages.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
onMessage
protected final org.apache.commons.logging.Log logger
public void setResponseRoutingKey(String responseRoutingKey)
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.
responseRoutingKey
- The routing key.public void setEncoding(String encoding)
encoding
- the encoding to set.public String getEncoding()
public void setResponseExchange(String responseExchange)
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.
responseExchange
- The exchange.public void setResponseAddress(String defaultReplyTo)
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.
Can be a string starting with "SpEL:" in which case the expression is evaluated at runtime; see the reference manual for more information.
defaultReplyTo
- The exchange.public void setMandatoryPublish(boolean mandatoryPublish)
public void setMessageConverter(MessageConverter messageConverter)
The default converter is a SimpleMessageConverter
, which is able to handle "text" content-types.
messageConverter
- The message converter.public void setReplyPostProcessor(MessagePostProcessor replyPostProcessor)
Channel#basicPublish()
.
Often used to compress the data.replyPostProcessor
- the reply post processor.public void setBeanResolver(org.springframework.expression.BeanResolver beanResolver)
beanResolver
- the resolver.protected MessageConverter getMessageConverter()
public void onMessage(Message message)
MessageListener
entry point.
Delegates the message to the target listener method, with appropriate conversion of the message argument.
Note: Does not support sending response messages based on result objects returned from listener methods.
Use the ChannelAwareMessageListener
entry point (typically through a Spring message listener container)
for handling result objects as well.
onMessage
in interface MessageListener
message
- the incoming Rabbit messageChannelAwareMessageListener.onMessage(Message, com.rabbitmq.client.Channel)
protected void handleListenerException(Throwable ex)
Can be used by inheritors from overridden onMessage(Message)
or ChannelAwareMessageListener.onMessage(Message, com.rabbitmq.client.Channel)
ex
- the exception to handleprotected Object extractMessage(Message message)
message
- the Rabbit Message
protected void handleResult(Object resultArg, Message request, com.rabbitmq.client.Channel channel) throws Exception
resultArg
- the result object to handle (never null
)request
- the original request messagechannel
- the Rabbit channel to operate on (may be null
)Exception
- if thrown by Rabbit API methodsbuildMessage(com.rabbitmq.client.Channel, java.lang.Object)
,
postProcessResponse(org.springframework.amqp.core.Message, org.springframework.amqp.core.Message)
,
getReplyToAddress(Message, Object, Object)
,
sendResponse(com.rabbitmq.client.Channel, org.springframework.amqp.core.Address, org.springframework.amqp.core.Message)
protected void handleResult(Object resultArg, Message request, com.rabbitmq.client.Channel channel, Object source) throws Exception
resultArg
- the result object to handle (never null
)request
- the original request messagechannel
- the Rabbit channel to operate on (may be null
)source
- the source data for the method invocation - e.g.
o.s.messaging.Message<?>
; may be nullException
- if thrown by Rabbit API methodsbuildMessage(com.rabbitmq.client.Channel, java.lang.Object)
,
postProcessResponse(org.springframework.amqp.core.Message, org.springframework.amqp.core.Message)
,
getReplyToAddress(Message, Object, Object)
,
sendResponse(com.rabbitmq.client.Channel, org.springframework.amqp.core.Address, org.springframework.amqp.core.Message)
protected Message buildMessage(com.rabbitmq.client.Channel channel, Object result) throws Exception
channel
- the Rabbit Channel to operate onresult
- the content of the message, as returned from the listener methodMessage
(never null
)Exception
- if thrown by Rabbit API methodssetMessageConverter(org.springframework.amqp.support.converter.MessageConverter)
protected void postProcessResponse(Message request, Message response) throws Exception
The default implementation sets the response's correlation id to the request message's correlation id, if any; otherwise to the request message id.
request
- the original incoming Rabbit messageresponse
- the outgoing Rabbit message about to be sentException
- if thrown by Rabbit API methodsprotected Address getReplyToAddress(Message request, Object source, Object result) throws Exception
The default implementation first checks the Rabbit Reply-To Address of the supplied request; if that is not
null
it is returned; if it is null
, then the configured default response Exchange and
routing key are used to construct a reply-to Address. If the responseExchange property is also null
,
then an AmqpException
is thrown.
request
- the original incoming Rabbit message.source
- the source data (e.g. o.s.messaging.Message<?>
).result
- the result.null
)Exception
- if thrown by Rabbit API methodsAmqpException
- if no Address
can be determinedsetResponseAddress(String)
,
setResponseRoutingKey(String)
,
Message.getMessageProperties()
,
MessageProperties.getReplyTo()
protected void sendResponse(com.rabbitmq.client.Channel channel, Address replyTo, Message messageIn) throws Exception
channel
- the Rabbit channel to operate onreplyTo
- the Rabbit ReplyTo string to use when sending. Currently interpreted to be the routing key.messageIn
- the Rabbit message to sendException
- if thrown by Rabbit API methodspostProcessResponse(Message, Message)
protected void postProcessChannel(com.rabbitmq.client.Channel channel, Message response) throws Exception
The default implementation is empty.
channel
- The channel.response
- the outgoing Rabbit message about to be sentException
- if thrown by Rabbit API methods