|
Spring Data Key-Value | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.data.keyvalue.redis.listener.adapter.MessageListenerAdapter
public class MessageListenerAdapter
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.
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); }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)
.
MessageListenerAdapter
Field Summary | |
---|---|
protected org.apache.commons.logging.Log |
logger
Logger available to subclasses |
static String |
ORIGINAL_DEFAULT_LISTENER_METHOD
Out-of-the-box value for the default listener method: "handleMessage". |
Constructor Summary | |
---|---|
MessageListenerAdapter()
Create a new MessageListenerAdapter with default settings. |
|
MessageListenerAdapter(Object delegate)
Create a new MessageListenerAdapter for the given delegate. |
Method Summary | |
---|---|
protected Object[] |
buildListenerArguments(Object extractedMessage)
Build an array of arguments to be passed into the target listener method. |
protected Object |
extractMessage(Message message)
Extract the message body from the given Redis message. |
protected String |
getDefaultListenerMethod()
Return the name of the default listener method to delegate to. |
Object |
getDelegate()
Returns the target object to delegate message listening to. |
protected String |
getListenerMethodName(Message originalMessage,
Object extractedMessage)
Determine the name of the listener method that is supposed to handle the given message. |
protected void |
handleListenerException(Throwable ex)
Handle the given exception that arose during listener execution. |
protected void |
initDefaultStrategies()
Initialize the default implementations for the adapter's strategies. |
protected Object |
invokeListenerMethod(String methodName,
Object[] arguments)
Invoke the specified listener method. |
void |
onMessage(Message message,
byte[] pattern)
Standard Redis MessageListener entry point. |
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 |
setSerializer(RedisSerializer<?> serializer)
Set the serializer that will convert incoming raw Redis messages to listener method arguments. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String ORIGINAL_DEFAULT_LISTENER_METHOD
protected final org.apache.commons.logging.Log logger
Constructor Detail |
---|
public MessageListenerAdapter()
MessageListenerAdapter
with default settings.
public MessageListenerAdapter(Object delegate)
MessageListenerAdapter
for the given delegate.
delegate
- the delegate objectMethod Detail |
---|
public void setDelegate(Object delegate)
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.
delegate
- delegate objectpublic Object getDelegate()
public void setDefaultListenerMethod(String defaultListenerMethod)
"handleMessage"
.
getListenerMethodName(org.springframework.data.keyvalue.redis.connection.Message, java.lang.Object)
protected String getDefaultListenerMethod()
public void setSerializer(RedisSerializer<?> serializer)
The default converter is a JdkSerializationRedisSerializer
, which is able
to handle Serializable
objects.
public void onMessage(Message message, byte[] pattern)
MessageListener
entry 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.
onMessage
in interface MessageListener
message
- the incoming Redis messagepattern
- pattern matching the channel (if specified) - can be nullhandleListenerException(java.lang.Throwable)
protected void initDefaultStrategies()
setSerializer(RedisSerializer)
,
JdkSerializationRedisSerializer
protected void handleListenerException(Throwable ex)
ex
- the exception to handleprotected Object extractMessage(Message message)
message
- the Redis Message
protected String getListenerMethodName(Message originalMessage, Object extractedMessage)
The default implementation simply returns the configured default listener method, if any.
originalMessage
- the Redis request messageextractedMessage
- the converted Redis request message,
to be passed into the listener method as argument
null
)setDefaultListenerMethod(java.lang.String)
protected Object[] buildListenerArguments(Object extractedMessage)
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.
extractedMessage
- the content of the message
protected Object invokeListenerMethod(String methodName, Object[] arguments)
methodName
- the name of the listener methodarguments
- the message arguments to be passed in
getListenerMethodName(org.springframework.data.keyvalue.redis.connection.Message, java.lang.Object)
,
buildListenerArguments(java.lang.Object)
|
Spring Data Key-Value | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |