Message listener adapter that delegates the handling of messages to target listener methods via reflection, with flexible message type conversion. Allows listener methods to operate on message content types, completely independent from the Rabbit API.
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 {@link Message}. Message type conversion is delegated to a Spring AMQ {@link MessageConverter}. By default, a {@link SimpleMessageConverter} will be used. (If you do not want such automatic message conversion taking place, thenbe sure to set the {@link #setMessageConverter 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 {@link #setResponseRoutingKey(String) specified routingKey}).
Note: The sending of response messages is only available when using the {@link ChannelAwareMessageListener}entry point (typically through a Spring message listener container). Usage as {@link MessageListener} does notsupport 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 {@link Message Messages}. Notice also how the name of the
Message
handling method is different from the {@link #ORIGINAL_DEFAULT_LISTENER_METHOD original} (this will have to be configured in the attandant beandefinition). 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 {@link Message Messages}. Notice how the return type of this method is
String
: This will result in the configured {@link MessageListenerAdapter} sending a {@link 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
@author Mark Pollack
@author Mark Fisher
@author Dave Syer
@author Gary Russell
@author Greg Turnquist
@see #setDelegate
@see #setDefaultListenerMethod
@see #setResponseRoutingKey(String)
@see #setMessageConverter
@see org.springframework.amqp.support.converter.SimpleMessageConverter
@see org.springframework.amqp.rabbit.core.ChannelAwareMessageListener
@see org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer#setMessageListener