Spring Integration

org.springframework.integration.redis.outbound
Class RedisCollectionPopulatingMessageHandler

java.lang.Object
  extended by org.springframework.integration.context.IntegrationObjectSupport
      extended by org.springframework.integration.handler.AbstractMessageHandler
          extended by org.springframework.integration.redis.outbound.RedisCollectionPopulatingMessageHandler
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean, org.springframework.core.Ordered, NamedComponent, Orderable, MessageHandler, TrackableComponent

public class RedisCollectionPopulatingMessageHandler
extends AbstractMessageHandler

Implementation of MessageHandler which writes Message data into a Redis store identified by a key String. It supports the collection types identified by RedisCollectionFactoryBean.CollectionType. It also supports batch updates and 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 AbstractMessageHandler.handleMessage(Message) for more details. You can also choose to persist such a payload as a single item if the extractPayloadElements property is set to false (default is true).

Since:
2.2
Author:
Oleg Zhurakousky, Gary Russell

Field Summary
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
RedisCollectionPopulatingMessageHandler(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory)
          Will construct this instance using the provided RedisConnectionFactory.
RedisCollectionPopulatingMessageHandler(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory, org.springframework.expression.Expression keyExpression)
          Will construct this instance using the provided RedisConnectionFactory and keyExpression It will create an instance of RedisTemplate with default serializers unless those are overridden via this instance's corresponding setters.
RedisCollectionPopulatingMessageHandler(org.springframework.data.redis.core.RedisTemplate<java.lang.String,?> redisTemplate)
          Will construct this instance using fully created and initialized instance of provided RedisTemplate The default expression 'headers.RedisHeaders.KEY' will be used.
RedisCollectionPopulatingMessageHandler(org.springframework.data.redis.core.RedisTemplate<java.lang.String,?> redisTemplate, org.springframework.expression.Expression keyExpression)
          Will construct this instance using provided RedisTemplate and keyExpression.
 
Method Summary
 java.lang.String getComponentType()
          Subclasses may implement this method to provide component type information.
protected  void handleMessageInternal(Message<?> message)
          Will extract payload from the Message storing it in the collection identified by the collectionType.
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 setHashKeySerializer(org.springframework.data.redis.serializer.RedisSerializer<?> hashKeySerializer)
           
 void setHashValueSerializer(org.springframework.data.redis.serializer.RedisSerializer<?> hashValueSerializer)
           
 void setKeySerializer(org.springframework.data.redis.serializer.RedisSerializer<?> keySerializer)
           
 void setMapKeyExpression(org.springframework.expression.Expression mapKeyExpression)
          Sets the expression used as the key for Map and Properties entries.
 void setValueSerializer(org.springframework.data.redis.serializer.RedisSerializer<?> valueSerializer)
           
 
Methods inherited from class org.springframework.integration.handler.AbstractMessageHandler
getOrder, handleMessage, setOrder, setShouldTrack
 
Methods inherited from class org.springframework.integration.context.IntegrationObjectSupport
afterPropertiesSet, getBeanFactory, getComponentName, getConversionService, getTaskScheduler, setBeanFactory, setBeanName, setComponentName, setConversionService, setTaskScheduler, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.springframework.integration.context.NamedComponent
getComponentName
 

Constructor Detail

RedisCollectionPopulatingMessageHandler

public RedisCollectionPopulatingMessageHandler(org.springframework.data.redis.core.RedisTemplate<java.lang.String,?> redisTemplate)
Will construct this instance using fully created and initialized instance of provided RedisTemplate The default expression 'headers.RedisHeaders.KEY' will be used.

Parameters:
redisTemplate -

RedisCollectionPopulatingMessageHandler

public RedisCollectionPopulatingMessageHandler(org.springframework.data.redis.core.RedisTemplate<java.lang.String,?> redisTemplate,
                                               org.springframework.expression.Expression keyExpression)
Will construct this instance using provided RedisTemplate and keyExpression. The RedisTemplate must be fully initialized. If keyExpression is null, the default expression 'headers.RedisHeaders.KEY' will be used.

Parameters:
redisTemplate -
keyExpression -

RedisCollectionPopulatingMessageHandler

public RedisCollectionPopulatingMessageHandler(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory)
Will construct this instance using the provided RedisConnectionFactory. It will create an instance of RedisTemplate with default serializers unless those are overridden via this instance's corresponding setters. The default expression 'headers.RedisHeaders.KEY' will be used.

Parameters:
connectionFactory -

RedisCollectionPopulatingMessageHandler

public RedisCollectionPopulatingMessageHandler(org.springframework.data.redis.connection.RedisConnectionFactory connectionFactory,
                                               org.springframework.expression.Expression keyExpression)
Will construct this instance using the provided RedisConnectionFactory and keyExpression It will create an instance of RedisTemplate with default serializers unless those are overridden via this instance's corresponding setters. If keyExpression is null, the default expression 'headers.RedisHeaders.KEY' will be used.

Parameters:
connectionFactory -
keyExpression -
Method Detail

setKeySerializer

public void setKeySerializer(org.springframework.data.redis.serializer.RedisSerializer<?> keySerializer)

setValueSerializer

public void setValueSerializer(org.springframework.data.redis.serializer.RedisSerializer<?> valueSerializer)

setHashKeySerializer

public void setHashKeySerializer(org.springframework.data.redis.serializer.RedisSerializer<?> hashKeySerializer)

setHashValueSerializer

public void setHashValueSerializer(org.springframework.data.redis.serializer.RedisSerializer<?> hashValueSerializer)

setCollectionType

public void setCollectionType(org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType collectionType)
Sets the collection type for this handler as per RedisCollectionFactoryBean.CollectionType

Parameters:
collectionType -

setExtractPayloadElements

public 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. Default is 'true'. If set to 'false' the payload will be saved as a single entry regardless of its type. If the payload is not an instance of "multivalue" (i.e., Collection or Map) the value of this attribute is meaningless as the payload will always be stored as a single entry.

Parameters:
extractPayloadElements -
See Also:
setExtractPayloadElements(boolean)

setMapKeyExpression

public void setMapKeyExpression(org.springframework.expression.Expression mapKeyExpression)
Sets the expression used as the key for Map and Properties entries. Default is 'headers.RedisHeaders.MAP_KEY'

Parameters:
mapKeyExpression -

getComponentType

public java.lang.String getComponentType()
Description copied from class: IntegrationObjectSupport
Subclasses may implement this method to provide component type information.

Specified by:
getComponentType in interface NamedComponent
Overrides:
getComponentType in class AbstractMessageHandler

onInit

protected void onInit()
               throws java.lang.Exception
Description copied from class: IntegrationObjectSupport
Subclasses may implement this for initialization logic.

Overrides:
onInit in class IntegrationObjectSupport
Throws:
java.lang.Exception

handleMessageInternal

protected void handleMessageInternal(Message<?> message)
                              throws java.lang.Exception
Will extract payload from the Message storing it in the collection identified by the collectionType. The default CollectinType is LIST.

The rules for storing payload are:

LIST/SET If 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 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 a Collection, or by sending a Map as the payload, where the Map 'key' is the value to be saved and the 'value' is the score assigned to this value. 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 store a payload of type Map or Properties following the same rules as above. If 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).

Specified by:
handleMessageInternal in class AbstractMessageHandler
Throws:
java.lang.Exception

Spring Integration