public class RedisStoreWritingMessageHandler extends AbstractMessageHandler
MessageHandler
which writes Message data into a Redis store
identified by a key String
.
It supports the collection types identified by RedisCollectionFactoryBean.CollectionType
.
It supports batch updates or single item entry.
"Batch updates" means that the payload of the Message may be a Map or Collection.
With such a payload, individual items from it are added to the corresponding Redis store.
See handleMessageInternal(Message)
for more details.
You can instead choose to persist such a payload as a single item if the
extractPayloadElements
property is set to false (default is true).Constructor and Description |
---|
RedisStoreWritingMessageHandler(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory)
Constructs an instance using the provided
RedisConnectionFactory . |
RedisStoreWritingMessageHandler(org.springframework.data.redis.core.RedisTemplate<java.lang.String,?> redisTemplate)
Constructs an instance using the provided
RedisTemplate . |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getComponentType()
Subclasses may implement this method to provide component type information.
|
protected void |
handleMessageInternal(Message<?> message)
Will extract the payload from the Message and store it in the collection identified by the
key (which may be determined by an expression).
|
protected void |
onInit()
Subclasses may implement this for initialization logic.
|
void |
setCollectionType(org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType collectionType)
Sets the collection type for this handler as per
RedisCollectionFactoryBean.CollectionType . |
void |
setExtractPayloadElements(boolean extractPayloadElements)
Sets the flag signifying that if the payload is a "multivalue" (i.e., Collection or Map),
it should be saved using addAll/putAll semantics.
|
void |
setKey(java.lang.String key)
Specifies the key for the Redis store.
|
void |
setKeyExpression(org.springframework.expression.Expression keyExpression)
Specifies a SpEL Expression to be used to determine the key for the Redis store.
|
void |
setMapKeyExpression(org.springframework.expression.Expression mapKeyExpression)
Sets the expression used as the key for Map and Properties entries.
|
getOrder, handleMessage, setOrder, setShouldTrack
afterPropertiesSet, getApplicationContextId, getBeanFactory, getComponentName, getConversionService, getIntegrationProperties, getIntegrationProperty, getTaskScheduler, setApplicationContext, setBeanFactory, setBeanName, setComponentName, setConversionService, setTaskScheduler, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getComponentName
public RedisStoreWritingMessageHandler(org.springframework.data.redis.core.RedisTemplate<java.lang.String,?> redisTemplate)
RedisTemplate
.
The RedisTemplate must be fully initialized.redisTemplate
- public RedisStoreWritingMessageHandler(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory)
RedisConnectionFactory
.
It will use either a StringRedisTemplate
if extractPayloadElements
is
true (default) or a RedisTemplate
with StringRedisSerializer
s for
keys and hash keys and JdkSerializationRedisSerializer
s for values and
hash values, when it is false.connectionFactory
- setExtractPayloadElements(boolean)
public void setKey(java.lang.String key)
setKeyExpression(Expression)
instead of this method (they are mutually exclusive).
If neither setter is called, the default expression will be 'headers.RedisHeaders.KEY
'.key
- setKeyExpression(Expression)
public void setKeyExpression(org.springframework.expression.Expression keyExpression)
setKey(String)
method instead of this one (they are mutually exclusive).
If neither setter is called, the default expression will be 'headers.RedisHeaders.KEY
'.keyExpression
- setKey(String)
public void setCollectionType(org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType collectionType)
RedisCollectionFactoryBean.CollectionType
.collectionType
- public void setExtractPayloadElements(boolean extractPayloadElements)
extractPayloadElements
- public void setMapKeyExpression(org.springframework.expression.Expression mapKeyExpression)
RedisHeaders.MAP_KEY
'mapKeyExpression
- public java.lang.String getComponentType()
IntegrationObjectSupport
getComponentType
in interface NamedComponent
getComponentType
in class AbstractMessageHandler
protected void onInit() throws java.lang.Exception
IntegrationObjectSupport
onInit
in class IntegrationObjectSupport
java.lang.Exception
protected void handleMessageInternal(Message<?> message) throws java.lang.Exception
collectionType
property. The default CollectionType is LIST.
The rules for storing the payload are:
LIST/SET
If the payload is of type Collection and extractPayloadElements
is 'true' (default),
the payload will be added using the addAll() method. If extractPayloadElements
is set to 'false', then regardless of the payload type, the payload will be added using add().
ZSET
In addition to the rules described for LIST/SET, ZSET allows 'score' information
to be provided. The score can be provided using the RedisHeaders.ZSET_SCORE
message header
when the payload is not a Map, or by sending a Map as the payload where each Map 'key' is a
value to be saved and each corresponding Map 'value' is the score assigned to it.
If extractPayloadElements
is set to 'false' the map will be stored as a single entry.
If the 'score' can not be determined, the default value (1) will be used.
MAP/PROPERTIES
You can also add items to a Map or Properties based store.
If the payload itself is of type Map or Properties, it can be stored either as a batch or single
item following the same rules as described above for other collection types.
If the payload itself needs to be stored as a value of the map/property then the map key
must be specified via the mapKeyExpression (default RedisHeaders.MAP_KEY
Message header).
handleMessageInternal
in class AbstractMessageHandler
java.lang.Exception