Class MessageListenerAdapter
- All Implemented Interfaces:
InitializingBean,MessageListener
Make sure to call afterPropertiesSet() after setting all the parameters on the adapter.
Note that if the underlying "delegate" is implementing 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
Message. Message type conversion is delegated to a Spring Data RedisSerializer. By default, the
JdkSerializationRedisSerializer will be used. (If you do not want such automatic message conversion taking
place, then be sure to set the 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 StringRedisSerializer. If the messages are of a different type, change them
accordingly through setSerializer(RedisSerializer).- Author:
- Juergen Hoeller, Costin Leau, Greg Turnquist, Thomas Darimont, Christoph Strobl, Mark Paluch
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreate a newMessageListenerAdapterwith default settings.MessageListenerAdapter(Object delegate) Create a newMessageListenerAdapterfor the given delegate.MessageListenerAdapter(Object delegate, String defaultListenerMethod) Create a newMessageListenerAdapterfor the given delegate. -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected ObjectextractMessage(Message message) Extract the message body from the given Redis message.protected StringReturn the name of the default listener method to delegate to.Returns the target object to delegate message listening to.protected StringgetListenerMethodName(Message originalMessage, Object extractedMessage) Determine the name of the listener method that is supposed to handle the given message.protected voidHandle the given exception that arose during listener execution.protected voidInitialize the default implementations for the adapter's strategies.protected voidinvokeListenerMethod(String methodName, Object[] arguments) Invoke the specified listener method.voidStandard RedisMessageListenerentry point.voidsetDefaultListenerMethod(String defaultListenerMethod) Specify the name of the default listener method to delegate to, for the case where no specific listener method has been determined.voidsetDelegate(Object delegate) Set a target object to delegate message listening to.voidsetSerializer(RedisSerializer<?> serializer) Set the serializer that will convert incoming raw Redis messages to listener method arguments.voidsetStringSerializer(RedisSerializer<String> serializer) Sets the serializer used for converting the channel/pattern to a String.
-
Field Details
-
ORIGINAL_DEFAULT_LISTENER_METHOD
Out-of-the-box value for the default listener method: "handleMessage".- See Also:
-
logger
Logger available to subclasses
-
-
Constructor Details
-
MessageListenerAdapter
public MessageListenerAdapter()Create a newMessageListenerAdapterwith default settings. -
MessageListenerAdapter
Create a newMessageListenerAdapterfor the given delegate.- Parameters:
delegate- the delegate object
-
MessageListenerAdapter
Create a newMessageListenerAdapterfor the given delegate.- Parameters:
delegate- the delegate objectdefaultListenerMethod- method to call when a message comes- See Also:
-
-
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- delegate object
-
getDelegate
Returns the target object to delegate message listening to.- Returns:
- message listening delegation
-
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". -
getDefaultListenerMethod
Return the name of the default listener method to delegate to. -
setSerializer
Set the serializer that will convert incoming raw Redis messages to listener method arguments.The default converter is a
StringRedisSerializer.- Parameters:
serializer-
-
setStringSerializer
Sets the serializer used for converting the channel/pattern to a String.The default converter is a
StringRedisSerializer.- Parameters:
serializer-
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceInitializingBean
-
onMessage
Standard RedisMessageListenerentry point.Delegates the message to the target listener method, with appropriate conversion of the message argument. In case of an exception, the
handleListenerException(Throwable)method will be invoked.- Specified by:
onMessagein interfaceMessageListener- Parameters:
message- the incoming Redis messagepattern- pattern matching the channel (if specified) - can be null.- See Also:
-
initDefaultStrategies
protected void initDefaultStrategies()Initialize the default implementations for the adapter's strategies. -
handleListenerException
Handle the given exception that arose during listener execution. The default implementation logs the exception at error level.- Parameters:
ex- the exception to handle
-
extractMessage
Extract the message body from the given Redis message.- Parameters:
message- the RedisMessage- Returns:
- the content of the message, to be passed into the listener method as argument
-
getListenerMethodName
Determine the name of the listener method that is supposed to handle the given message.The default implementation simply returns the configured default listener method, if any.
- Parameters:
originalMessage- the Redis request messageextractedMessage- the converted Redis request message, to be passed into the listener method as argument- Returns:
- the name of the listener method (never
null) - See Also:
-
invokeListenerMethod
Invoke the specified listener method.- Parameters:
methodName- the name of the listener methodarguments- the message arguments to be passed in- See Also:
-