Class AbstractAdaptableMessageListener
- java.lang.Object
-
- org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener
-
- All Implemented Interfaces:
MessageListener,ChannelAwareMessageListener
- Direct Known Subclasses:
MessageListenerAdapter,MessagingMessageListenerAdapter
public abstract class AbstractAdaptableMessageListener extends Object implements ChannelAwareMessageListener
An abstractMessageListeneradapter providing the necessary infrastructure to extract the payload of aMessage.- Since:
- 1.4
- Author:
- Stephane Nicoll, Gary Russell, Artem Bilan, Johan Haleby
- See Also:
ChannelAwareMessageListener
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractAdaptableMessageListener.ReplyExpressionRootRoot object for reply expression evaluation.
-
Constructor Summary
Constructors Constructor Description AbstractAdaptableMessageListener()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected MessagebuildMessage(com.rabbitmq.client.Channel channel, Object result, Type genericType)Build a Rabbit message to be sent as response based on the given result object.voidcontainerAckMode(AcknowledgeMode mode)Called by the container to inform the listener of its acknowledgement mode.protected Messageconvert(Object result, Type genericType, MessageConverter converter)Convert to a message, with reply content type based on settings.protected voiddoHandleResult(InvocationResult resultArg, Message request, com.rabbitmq.client.Channel channel, Object source)protected voiddoPublish(com.rabbitmq.client.Channel channel, Address replyTo, Message message)protected ObjectextractMessage(Message message)Extract the message body from the given Rabbit message.StringgetEncoding()The encoding to use when inter-converting between byte arrays and Strings in message properties.protected MessageConvertergetMessageConverter()Return the converter that will convert incoming Rabbit messages to listener method arguments, and objects returned from listener methods back to Rabbit messages.protected StringgetReceivedExchange(Message request)protected StringgetReplyContentType()Get the reply content type.protected AddressgetReplyToAddress(Message request, Object source, InvocationResult result)Determine a reply-to Address for the given message.protected voidhandleListenerException(Throwable ex)Handle the given exception that arose during listener execution.protected voidhandleResult(InvocationResult resultArg, Message request, com.rabbitmq.client.Channel channel)Handle the given result object returned from the listener method, sending a response message back.protected voidhandleResult(InvocationResult 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.protected booleanisConverterWinsContentType()Return whether the content type set by a converter prevails or not.protected voidpostProcessChannel(com.rabbitmq.client.Channel channel, Message response)Post-process the given message before sending the response.protected voidpostProcessResponse(Message request, Message response)Post-process the given response message before it will be sent.protected voidsendResponse(com.rabbitmq.client.Channel channel, Address replyTo, Message messageIn)Send the given response message to the given destination.voidsetBeanResolver(BeanResolver beanResolver)Set a bean resolver for runtime SpEL expressions.voidsetBeforeSendReplyPostProcessors(MessagePostProcessor... beforeSendReplyPostProcessors)Set post processors that will be applied before sending replies.voidsetConverterWinsContentType(boolean converterWinsContentType)Set whether the content type set by a converter prevails or not.voidsetDefaultRequeueRejected(boolean defaultRequeueRejected)Set to the value of this listener's container equivalent property.voidsetEncoding(String encoding)The encoding to use when inter-converting between byte arrays and Strings in message properties.voidsetMandatoryPublish(boolean mandatoryPublish)voidsetMessageConverter(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.voidsetRecoveryCallback(org.springframework.retry.RecoveryCallback<?> recoveryCallback)Set aRecoveryCallbackto invoke when retries are exhausted.voidsetReplyContentType(String replyContentType)Set the reply content type.voidsetReplyPostProcessor(ReplyPostProcessor replyPostProcessor)Set aReplyPostProcessorto post process a response message before it is sent.voidsetResponseAddress(String defaultReplyTo)Set the default replyTo address to use when sending response messages.voidsetResponseExchange(String responseExchange)Set the exchange to use when sending response messages.voidsetResponseRoutingKey(String responseRoutingKey)Set the routing key to use when sending response messages.voidsetRetryTemplate(org.springframework.retry.support.RetryTemplate retryTemplate)Set aRetryTemplateto use when sending replies.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener
onMessage, onMessage, onMessageBatch
-
Methods inherited from interface org.springframework.amqp.core.MessageListener
isAsyncReplies, onMessageBatch
-
-
-
-
Field Detail
-
logger
protected final Log logger
Logger available to subclasses.
-
-
Method Detail
-
setResponseRoutingKey
public void setResponseRoutingKey(String responseRoutingKey)
Set the routing key to use when sending response messages. This will be applied in case of a request message that does not carry a "ReplyTo" propertyResponse 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.
- Parameters:
responseRoutingKey- The routing key.
-
setEncoding
public void setEncoding(String encoding)
The encoding to use when inter-converting between byte arrays and Strings in message properties.- Parameters:
encoding- the encoding to set.
-
getEncoding
public String getEncoding()
The encoding to use when inter-converting between byte arrays and Strings in message properties.- Returns:
- encoding the encoding.
-
setResponseExchange
public void setResponseExchange(String responseExchange)
Set the exchange to use when sending response messages. This is only used if the exchange from the received message is null.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.
- Parameters:
responseExchange- The exchange.
-
setResponseAddress
public void setResponseAddress(String defaultReplyTo)
Set the default replyTo address to use when sending response messages. This is only used if the replyTo from the received message is null.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.
It is parsed in
Addressso should be of the form exchange/rk.It can be a string surrounded by "!{...}" in which case the expression is evaluated at runtime; see the reference manual for more information.
- Parameters:
defaultReplyTo- The replyTo address.- Since:
- 1.6
-
setMandatoryPublish
public void setMandatoryPublish(boolean mandatoryPublish)
-
setMessageConverter
public 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.The default converter is a
SimpleMessageConverter, which is able to handle "text" content-types.- Parameters:
messageConverter- The message converter.
-
setBeforeSendReplyPostProcessors
public void setBeforeSendReplyPostProcessors(MessagePostProcessor... beforeSendReplyPostProcessors)
Set post processors that will be applied before sending replies.- Parameters:
beforeSendReplyPostProcessors- the post processors.- Since:
- 2.0.3
-
setRetryTemplate
public void setRetryTemplate(org.springframework.retry.support.RetryTemplate retryTemplate)
Set aRetryTemplateto use when sending replies.- Parameters:
retryTemplate- the template.- Since:
- 2.0.6
- See Also:
setRecoveryCallback(RecoveryCallback)
-
setRecoveryCallback
public void setRecoveryCallback(org.springframework.retry.RecoveryCallback<?> recoveryCallback)
Set aRecoveryCallbackto invoke when retries are exhausted.- Parameters:
recoveryCallback- the recovery callback.- Since:
- 2.0.6
- See Also:
setRetryTemplate(RetryTemplate)
-
setBeanResolver
public void setBeanResolver(BeanResolver beanResolver)
Set a bean resolver for runtime SpEL expressions. Also configures the evaluation context with a standard type converter and map accessor.- Parameters:
beanResolver- the resolver.- Since:
- 1.6
-
setReplyPostProcessor
public void setReplyPostProcessor(ReplyPostProcessor replyPostProcessor)
Set aReplyPostProcessorto post process a response message before it is sent. It is called afterpostProcessResponse(Message, Message)which sets up the correlationId header.- Parameters:
replyPostProcessor- the post processor.- Since:
- 2.2.5
-
getReplyContentType
protected String getReplyContentType()
Get the reply content type.- Returns:
- the content type.
- Since:
- 2.3
-
setReplyContentType
public void setReplyContentType(String replyContentType)
Set the reply content type.- Parameters:
replyContentType- the content type.- Since:
- 2.3
-
isConverterWinsContentType
protected boolean isConverterWinsContentType()
Return whether the content type set by a converter prevails or not.- Returns:
- false to always apply the reply content type.
- Since:
- 2.3
-
setConverterWinsContentType
public void setConverterWinsContentType(boolean converterWinsContentType)
Set whether the content type set by a converter prevails or not.- Parameters:
converterWinsContentType- false to always apply the reply content type.- Since:
- 2.3
-
getMessageConverter
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.- Returns:
- The message converter.
-
setDefaultRequeueRejected
public void setDefaultRequeueRejected(boolean defaultRequeueRejected)
Set to the value of this listener's container equivalent property. Used when rejecting from an async listener.- Parameters:
defaultRequeueRejected- false to not requeue.- Since:
- 2.1.8
-
containerAckMode
public void containerAckMode(AcknowledgeMode mode)
Description copied from interface:MessageListenerCalled by the container to inform the listener of its acknowledgement mode.- Specified by:
containerAckModein interfaceMessageListener- Parameters:
mode- theAcknowledgeMode.
-
handleListenerException
protected void handleListenerException(Throwable ex)
Handle the given exception that arose during listener execution. The default implementation logs the exception at error level.Can be used by inheritors from overridden
ChannelAwareMessageListener.onMessage(Message)orChannelAwareMessageListener.onMessage(Message, com.rabbitmq.client.Channel)- Parameters:
ex- the exception to handle
-
extractMessage
protected Object extractMessage(Message message)
Extract the message body from the given Rabbit message.- Parameters:
message- the RabbitMessage- Returns:
- the content of the message, to be passed into the listener method as argument
-
handleResult
protected void handleResult(InvocationResult resultArg, Message request, com.rabbitmq.client.Channel channel)
Handle the given result object returned from the listener method, sending a response message back.- Parameters:
resultArg- the result object to handle (nevernull)request- the original request messagechannel- the Rabbit channel to operate on (may benull)- See Also:
buildMessage(com.rabbitmq.client.Channel, java.lang.Object, java.lang.reflect.Type),postProcessResponse(org.springframework.amqp.core.Message, org.springframework.amqp.core.Message),getReplyToAddress(Message, Object, InvocationResult),sendResponse(com.rabbitmq.client.Channel, org.springframework.amqp.core.Address, org.springframework.amqp.core.Message)
-
handleResult
protected void handleResult(InvocationResult 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.- Parameters:
resultArg- the result object to handle (nevernull)request- the original request messagechannel- the Rabbit channel to operate on (may benull)source- the source data for the method invocation - e.g.o.s.messaging.Message<?>; may be null- See Also:
buildMessage(com.rabbitmq.client.Channel, java.lang.Object, java.lang.reflect.Type),postProcessResponse(org.springframework.amqp.core.Message, org.springframework.amqp.core.Message),getReplyToAddress(Message, Object, InvocationResult),sendResponse(com.rabbitmq.client.Channel, org.springframework.amqp.core.Address, org.springframework.amqp.core.Message)
-
doHandleResult
protected void doHandleResult(InvocationResult resultArg, Message request, com.rabbitmq.client.Channel channel, Object source)
-
buildMessage
protected Message buildMessage(com.rabbitmq.client.Channel channel, Object result, Type genericType)
Build a Rabbit message to be sent as response based on the given result object.- Parameters:
channel- the Rabbit Channel to operate on.result- the content of the message, as returned from the listener method.genericType- the generic type to populate type headers.- Returns:
- the Rabbit
Message(nevernull). - See Also:
setMessageConverter(org.springframework.amqp.support.converter.MessageConverter)
-
convert
protected Message convert(Object result, Type genericType, MessageConverter converter)
Convert to a message, with reply content type based on settings.- Parameters:
result- the result.genericType- the type.converter- the converter.- Returns:
- the message.
- Since:
- 2.3
-
postProcessResponse
protected void postProcessResponse(Message request, Message response)
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 Rabbit messageresponse- the outgoing Rabbit message about to be sent
-
getReplyToAddress
protected Address getReplyToAddress(Message request, Object source, InvocationResult result)
Determine a reply-to Address for the given message.The default implementation first checks the Rabbit Reply-To Address of the supplied request; if that is not
nullit is returned; if it isnull, then the configured default response Exchange and routing key are used to construct a reply-to Address. If the responseExchange property is alsonull, then anAmqpExceptionis thrown.- Parameters:
request- the original incoming Rabbit message.source- the source data (e.g.o.s.messaging.Message<?>).result- the result.- Returns:
- the reply-to Address (never
null) - Throws:
AmqpException- if noAddresscan be determined- See Also:
setResponseAddress(String),setResponseRoutingKey(String),Message.getMessageProperties(),MessageProperties.getReplyTo()
-
sendResponse
protected void sendResponse(com.rabbitmq.client.Channel channel, Address replyTo, Message messageIn)Send the given response message to the given destination.- Parameters:
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 send- See Also:
postProcessResponse(Message, Message),setReplyPostProcessor(ReplyPostProcessor)
-
doPublish
protected void doPublish(com.rabbitmq.client.Channel channel, Address replyTo, Message message) throws IOException- Throws:
IOException
-
postProcessChannel
protected void postProcessChannel(com.rabbitmq.client.Channel channel, Message response)Post-process the given message before sending the response.The default implementation is empty.
- Parameters:
channel- The channel.response- the outgoing Rabbit message about to be sent
-
-