Class MessageListenerAdapter
- All Implemented Interfaces:
MessageListener
,ChannelAwareMessageListener
By default, the content of incoming Rabbit messages gets extracted before being passed into the target listener
method, to let the target method operate on message content types such as String or byte array instead of the raw
Message
. Message type conversion is delegated to a Spring AMQ MessageConverter
. By default, a
SimpleMessageConverter
will be used.
(If you do not want such automatic message conversion taking place, then
be sure to set the MessageConverter
to null
.)
If a target listener method returns a non-null object (typically of a message content type such as
String
or byte array), it will get wrapped in a Rabbit Message
and sent to the exchange of
the incoming message with the routingKey that comes from the Rabbit ReplyTo property or via
specified routingKey
).
Note: The sending of response messages is only available when using the ChannelAwareMessageListener
entry point (typically through a Spring message listener container). Usage as MessageListener
does not
support the generation of response messages.
Find below some examples of method signatures compliant with this adapter class. This first example handles all
Message
types and gets passed the contents of each Message
type as an argument. No
Message
will be sent back as all of these methods return void
.
public interface MessageContentsDelegate { void handleMessage(String text); void handleMessage(Map map); void handleMessage(byte[] bytes); void handleMessage(Serializable obj); }This next example handle a
Message
type and gets passed the actual (raw) Message
as an
argument. Again, no Message
will be sent back as all of these methods return void
.
public interface RawMessageDelegate { void handleMessage(Message message); }This next example illustrates a
Message
delegate that just consumes the String
contents of
Messages
. Notice also how the name of the Message
handling method is different from the
original
(this will have to be configured in the attandant bean
definition). Again, no Message
will be sent back as the method returns void
.
public interface TextMessageContentDelegate { void onMessage(String text); }This final example illustrates a
Message
delegate that just consumes the String
contents of
Messages
. Notice how the return type of this method is String
: This will result in the
configured MessageListenerAdapter
sending a Message
in response.
public interface ResponsiveTextMessageContentDelegate { String handleMessage(String text); }For further examples and discussion please do refer to the Spring reference documentation which describes this class (and its attendant XML configuration) in detail.
- Author:
- Juergen Hoeller, Mark Pollack, Mark Fisher, Dave Syer, Gary Russell, Greg Turnquist, Cai Kun
- See Also:
-
setDelegate(java.lang.Object)
setDefaultListenerMethod(java.lang.String)
AbstractAdaptableMessageListener.setResponseRoutingKey(String)
AbstractAdaptableMessageListener.setMessageConverter(org.springframework.amqp.support.converter.MessageConverter)
SimpleMessageConverter
ChannelAwareMessageListener
AbstractMessageListenerContainer.setMessageListener(MessageListener)
-
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener
AbstractAdaptableMessageListener.ReplyExpressionRoot
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Out-of-the-box value for the default listener method: "handleMessage".Fields inherited from class org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener
logger
-
Constructor Summary
ConstructorDescriptionCreate a newMessageListenerAdapter
with default settings.MessageListenerAdapter
(Object delegate) Create a newMessageListenerAdapter
for the given delegate.MessageListenerAdapter
(Object delegate, String defaultListenerMethod) Create a newMessageListenerAdapter
for the given delegate while also declaring its POJO method.MessageListenerAdapter
(Object delegate, MessageConverter messageConverter) Create a newMessageListenerAdapter
for the given delegate. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addQueueOrTagToMethodName
(String queueOrTag, String methodName) Add the mapping of a queue name or consumer tag to a method name.protected Object[]
buildListenerArguments
(Object extractedMessage, com.rabbitmq.client.Channel channel, Message message) Build an array of arguments to be passed into the target listener method.protected String
protected Object
protected String
getListenerMethodName
(Message originalMessage, Object extractedMessage) Determine the name of the listener method that will handle the given message.protected Object
invokeListenerMethod
(String methodName, Object[] arguments, Message originalMessage) Invoke the specified listener method.void
SpringChannelAwareMessageListener
entry point.removeQueueOrTagToMethodName
(String queueOrTag) Remove the mapping of a queue name or consumer tag to a method name.void
setDefaultListenerMethod
(String defaultListenerMethod) Specify the name of the default listener method to delegate to, for the case where no specific listener method has been determined.void
setDelegate
(Object delegate) Set a target object to delegate message listening to.void
setQueueOrTagToMethodName
(Map<String, String> queueOrTagToMethodName) Set the mapping of queue name or consumer tag to method name.Methods inherited from class org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener
asyncFailure, buildMessage, containerAckMode, convert, doHandleResult, doPublish, extractMessage, getEncoding, getMessageConverter, getReceivedExchange, getReplyContentType, getReplyToAddress, handleListenerException, handleResult, handleResult, isConverterWinsContentType, postProcessChannel, postProcessResponse, sendResponse, setBeanResolver, setBeforeSendReplyPostProcessors, setConverterWinsContentType, setDefaultRequeueRejected, setEncoding, setMandatoryPublish, setMessageConverter, setRecoveryCallback, setReplyContentType, setReplyPostProcessor, setResponseAddress, setResponseExchange, setResponseRoutingKey, setRetryTemplate
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, onMessageBatch
Methods inherited from interface org.springframework.amqp.core.MessageListener
isAsyncReplies, onMessageBatch
-
Field Details
-
ORIGINAL_DEFAULT_LISTENER_METHOD
Out-of-the-box value for the default listener method: "handleMessage".- See Also:
-
-
Constructor Details
-
MessageListenerAdapter
public MessageListenerAdapter()Create a newMessageListenerAdapter
with default settings. -
MessageListenerAdapter
Create a newMessageListenerAdapter
for the given delegate.- Parameters:
delegate
- the delegate object
-
MessageListenerAdapter
Create a newMessageListenerAdapter
for the given delegate.- Parameters:
delegate
- the delegate objectmessageConverter
- the message converter to use
-
MessageListenerAdapter
Create a newMessageListenerAdapter
for the given delegate while also declaring its POJO method.- Parameters:
delegate
- the delegate objectdefaultListenerMethod
- name of the POJO method to call upon message receipt
-
-
Method Details
-
setDelegate
Set a target object to delegate message listening to. Specified listener methods have to be present on this target object.If no explicit delegate object has been specified, listener methods are expected to present on this adapter instance, that is, on a custom subclass of this adapter, defining listener methods.
- Parameters:
delegate
- The delegate listener or POJO.
-
getDelegate
- Returns:
- The target object to delegate message listening to.
-
setDefaultListenerMethod
Specify the name of the default listener method to delegate to, for the case where no specific listener method has been determined. Out-of-the-box value is"handleMessage"
.- Parameters:
defaultListenerMethod
- The name of the default listener method.- See Also:
-
getDefaultListenerMethod
- Returns:
- The name of the default listener method to delegate to.
-
setQueueOrTagToMethodName
Set the mapping of queue name or consumer tag to method name. The first lookup is by queue name, if that returns null, we lookup by consumer tag, if that returns null, thedefaultListenerMethod
is used.- Parameters:
queueOrTagToMethodName
- the map.- Since:
- 1.5
-
addQueueOrTagToMethodName
Add the mapping of a queue name or consumer tag to a method name. The first lookup is by queue name, if that returns null, we lookup by consumer tag, if that returns null, thedefaultListenerMethod
is used.- Parameters:
queueOrTag
- The queue name or consumer tag.methodName
- The method name.- Since:
- 1.5
-
removeQueueOrTagToMethodName
Remove the mapping of a queue name or consumer tag to a method name.- Parameters:
queueOrTag
- The queue name or consumer tag.- Returns:
- the method name that was removed, or null.
- Since:
- 1.5
-
onMessage
SpringChannelAwareMessageListener
entry point.Delegates the message to the target listener method, with appropriate conversion of the message argument. If the target method returns a non-null object, wrap in a Rabbit message and send it back.
- Parameters:
message
- the incoming Rabbit messagechannel
- the Rabbit channel to operate on- Throws:
Exception
- if thrown by Rabbit API methods
-
getListenerMethodName
Determine the name of the listener method that will handle the given message.The default implementation first consults the
queueOrTagToMethodName
map looking for a match on the consumer queue or consumer tag; if no match found, it simply returns the configured default listener method, or "handleMessage" if not configured.- Parameters:
originalMessage
- the Rabbit request messageextractedMessage
- the converted Rabbit request message, to be passed into the listener method as argument- Returns:
- the name of the listener method (never
null
) - See Also:
-
buildListenerArguments
protected Object[] buildListenerArguments(Object extractedMessage, com.rabbitmq.client.Channel channel, Message message) Build an array of arguments to be passed into the target listener method. Allows for multiple method arguments to be built from a single message object.The default implementation builds an array with the given message object as sole element. This means that the extracted message will always be passed into a single method argument, even if it is an array, with the target method having a corresponding single argument of the array's type declared.
This can be overridden to treat special message content such as arrays differently, for example passing in each element of the message array as distinct method argument.
- Parameters:
extractedMessage
- the content of the messagechannel
- the Rabbit channel to operate onmessage
- the incoming Rabbit message- Returns:
- the array of arguments to be passed into the listener method (each element of the array corresponding to a distinct method argument)
-
invokeListenerMethod
protected Object invokeListenerMethod(String methodName, Object[] arguments, Message originalMessage) Invoke the specified listener method.- Parameters:
methodName
- the name of the listener methodarguments
- the message arguments to be passed inoriginalMessage
- the original message- Returns:
- the result returned from the listener method
- See Also:
-