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 Redis API.
Make sure to call {@link #afterPropertiesSet()} after setting all the parameters on the adapter.
Note that if the underlying "delegate" is implementing {@link MessageListener}, the adapter will delegate to it and allow an invalid method to be specified. However if it is not, the method becomes mandatory. This lenient behavior allows the adapter to be used uniformly across existing listeners and message POJOs.
Modeled as much as possible after the JMS MessageListenerAdapter in Spring Framework.
By default, the content of incoming Redis 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 Data {@link RedisSerializer}. By default, the {@link JdkSerializationRedisSerializer} will be used. (If you do not want such automatic message conversion takingplace, then be sure to set the {@link #setSerializer Serializer} to null
.)
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.
public interface MessageContentsDelegate { void handleMessage(String text); void handleMessage(byte[] bytes); void handleMessage(Person obj); }
In addition, the channel or pattern to which a message is sent can be passed in to the method as a second argument of type String:
public interface MessageContentsDelegate { void handleMessage(String text, String channel); void handleMessage(byte[] bytes, String pattern); }
For further examples and discussion please do refer to the Spring Data reference documentation which describes this class (and its attendant configuration) in detail.
Important: Due to the nature of messages, the default serializer used by the adapter is {@link StringRedisSerializer}. If the messages are of a different type, change them accordingly through {@link #setSerializer(RedisSerializer)}.
@author Juergen Hoeller
@author Costin Leau
@author Greg Turnquist
@author Thomas Darimont
@author Christoph Strobl
@see org.springframework.jms.listener.adapter.MessageListenerAdapter