INT-2799/2811 Redis Store Adapter Changes

INT-2799 Use StringRedisTemplate by default on both inbound
and outbound store adapters.

Use a RedisTemplate with String key serializers and JDK
value serializers on the outbound store adapter if
'extract-payload-elements' is false.

INT-2811 Remove the ability to configure serializers on the adapters.

Use an external RedisTemplate for customized serialization
strategies.

INT-2811 Polishing (Review)

Polishing and updated Reference Doc.

polishing

using setters instead of ctor args for key or keyExpression

INT-2811 Polishing

Improve message when handler not initialized.

Add test for storing a simple value in a map.
This commit is contained in:
Gary Russell
2012-11-07 15:03:09 -05:00
parent 3d93bb409e
commit f501a6730e
20 changed files with 369 additions and 575 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.util.StringUtils;
* Parser for Redis store inbound adapters
*
* @author Oleg Zhurakousky
* @author Gary Russell
* @since 2.2
*/
public class RedisCollectionInboundChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser {
@@ -64,8 +65,6 @@ public class RedisCollectionInboundChannelAdapterParser extends AbstractPollingI
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "collection-type");
RedisParserUtils.setRedisSerializers(StringUtils.hasText(redisTemplate), element, parserContext, builder);
String beanName = BeanDefinitionReaderUtils.registerWithGeneratedName(
builder.getBeanDefinition(), parserContext.getRegistry());
return new RuntimeBeanReference(beanName);

View File

@@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.redis.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -26,10 +26,14 @@ import org.springframework.integration.config.xml.AbstractOutboundChannelAdapter
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.integration.redis.outbound.RedisCollectionPopulatingMessageHandler;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* Parser for redis:store-outbound-channel-adapter element
* Parser for the <redis:store-outbound-channel-adapter> element.
*
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Mark Fisher
* @since 2.2
*/
public class RedisCollectionOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser {
@@ -42,9 +46,8 @@ public class RedisCollectionOutboundChannelAdapterParser extends AbstractOutboun
String connectionFactory = element.getAttribute("connection-factory");
if (StringUtils.hasText(redisTemplateRef) && StringUtils.hasText(connectionFactory)){
parserContext.getReaderContext().error("Only one of 'redis-template' or 'connection-factory'" +
" is allowed", element);
" is allowed.", element);
}
if (StringUtils.hasText(redisTemplateRef)){
builder.addConstructorArgReference(redisTemplateRef);
}
@@ -55,17 +58,19 @@ public class RedisCollectionOutboundChannelAdapterParser extends AbstractOutboun
builder.addConstructorArgReference(connectionFactory);
}
boolean atLeastOneRequired = false;
RootBeanDefinition expressionDef =
IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression("key", "key-expression",
parserContext, element, atLeastOneRequired);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "collection-type");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload-elements");
RedisParserUtils.setRedisSerializers(StringUtils.hasText(redisTemplateRef), element, parserContext, builder);
if (expressionDef != null){
builder.addConstructorArgValue(expressionDef);
boolean hasKey = element.hasAttribute("key");
boolean hasKeyExpression = element.hasAttribute("key-expression");
if (hasKey && hasKeyExpression) {
parserContext.getReaderContext().error(
"At most one of 'key' or 'key-expression' is allowed.", element);
}
if (hasKey) {
builder.addPropertyValue("key", new TypedStringValue(element.getAttribute("key")));
}
if (hasKeyExpression) {
RootBeanDefinition expressionDef = new RootBeanDefinition(ExpressionFactoryBean.class);
expressionDef.getConstructorArgumentValues().addGenericArgumentValue(element.getAttribute("key-expression"));
builder.addPropertyValue("keyExpression", expressionDef);
}
String mapKeyExpression = element.getAttribute("map-key-expression");
@@ -75,6 +80,9 @@ public class RedisCollectionOutboundChannelAdapterParser extends AbstractOutboun
builder.addPropertyValue("mapKeyExpression", mapKeyExpressionDef);
}
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "collection-type");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload-elements");
return builder.getBeanDefinition();
}
}
}

View File

@@ -1,47 +0,0 @@
/*
* Copyright 2007-2012 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.redis.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
/**
*
* @author Oleg Zhurakousky
* @since 2.2
*/
public class RedisParserUtils {
public static void setRedisSerializers(boolean redisTemplateSet, Element element, ParserContext parserContext, BeanDefinitionBuilder builder){
if (redisTemplateSet){
if (element.hasAttribute("key-serializer") ||
element.hasAttribute("value-serializer") ||
element.hasAttribute("hash-key-serializer") ||
element.hasAttribute("hash-value-serializer")){
parserContext.getReaderContext().error("Serializers can not be provided if this adapter is initialized " +
"with an external RedisTemplate. You may set serializers directly on the RedisTemplate", element);
}
}
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "key-serializer");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "value-serializer");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "hash-key-serializer");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "hash-value-serializer");
}
}

View File

@@ -20,8 +20,7 @@ import java.util.Collection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean;
import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType;
@@ -43,6 +42,7 @@ import org.springframework.util.Assert;
* {@link CollectionType}
*
* @author Oleg Zhurakousky
* @author Gary Russell
* @since 2.2
*/
public class RedisStoreMessageSource extends IntegrationObjectSupport
@@ -58,18 +58,8 @@ public class RedisStoreMessageSource extends IntegrationObjectSupport
private final RedisTemplate<String, ?> redisTemplate;
private volatile RedisSerializer<?> keySerializer;
private volatile RedisSerializer<?> valueSerializer;
private volatile RedisSerializer<?> hashKeySerializer;
private volatile RedisSerializer<?> hashValueSerializer;
private volatile boolean usingDefaultTemplate = true;
/**
* Creates this instance with provided {@link RedisTemplate} and SpEL expression
* Creates an instance with the provided {@link RedisTemplate} and SpEL expression
* which should resolve to a 'key' name of the collection to be used.
* It assumes that {@link RedisTemplate} is fully initialized and ready to be used.
* The 'keyExpression' will be evaluated on every call to the {@link #receive()} method.
@@ -84,16 +74,14 @@ public class RedisStoreMessageSource extends IntegrationObjectSupport
Assert.notNull(redisTemplate, "'redisTemplate' must not be null");
this.redisTemplate = redisTemplate;
this.usingDefaultTemplate = false;
this.keyExpression = keyExpression;
}
/**
* Creates this instance with provided {@link RedisConnectionFactory} and SpEL expression
* Creates an instance with the provided {@link RedisConnectionFactory} and SpEL expression
* which should resolve to a 'key' name of the collection to be used.
* It will create and initialize an instance of the {@link RedisTemplate} using
* {@link StringRedisSerializer} as key serializer and {@link JdkSerializationRedisSerializer} for
* value, hashKey and hashValue serializer.
* It will create and initialize an instance of {@link StringRedisTemplate} that uses
* {@link StringRedisSerializer} for all serialization.
*
* The 'keyExpression' will be evaluated on every call to the {@link #receive()} method.
*
@@ -106,37 +94,13 @@ public class RedisStoreMessageSource extends IntegrationObjectSupport
Assert.notNull(keyExpression, "'keyExpression' must not be null");
Assert.notNull(connectionFactory, "'connectionFactory' must not be null");
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(connectionFactory);
this.redisTemplate = redisTemplate;
this.keyExpression = keyExpression;
}
public void setKeySerializer(RedisSerializer<?> keySerializer) {
Assert.state(this.usingDefaultTemplate, "'keySerializer' can not be set if a RedisTemplate was explicitly provided");
Assert.notNull(keySerializer, "'keySerializer' must not be null");
this.keySerializer = keySerializer;
}
public void setValueSerializer(RedisSerializer<?> valueSerializer) {
Assert.state(this.usingDefaultTemplate, "'valueSerializer' can not be set if a RedisTemplate was explicitly provided");
Assert.notNull(valueSerializer, "'valueSerializer' must not be null");
this.valueSerializer = valueSerializer;
}
public void setHashKeySerializer(RedisSerializer<?> hashKeySerializer) {
Assert.state(this.usingDefaultTemplate, "'hashKeySerializer' can not be set if a RedisTemplate was explicitly provided");
Assert.notNull(hashKeySerializer, "'hashKeySerializer' must not be null");
this.hashKeySerializer = hashKeySerializer;
}
public void setHashValueSerializer(RedisSerializer<?> hashValueSerializer) {
Assert.state(this.usingDefaultTemplate, "'hashValueSerializer' can not be set if a RedisTemplate was explicitly provided");
Assert.notNull(hashValueSerializer, "'hashValueSerializer' must not be null");
this.hashValueSerializer = hashValueSerializer;
}
public void setCollectionType(CollectionType collectionType) {
this.collectionType = collectionType;
}
@@ -184,22 +148,6 @@ public class RedisStoreMessageSource extends IntegrationObjectSupport
else {
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext();
}
if (this.usingDefaultTemplate){
if (this.keySerializer != null){
redisTemplate.setKeySerializer(this.keySerializer);
}
if (this.valueSerializer != null){
redisTemplate.setValueSerializer(this.valueSerializer);
}
if (this.hashKeySerializer != null){
redisTemplate.setHashKeySerializer(this.hashKeySerializer);
}
if (this.hashValueSerializer != null){
redisTemplate.setHashValueSerializer(this.hashValueSerializer);
}
this.redisTemplate.afterPropertiesSet();
}
}
public RedisStore getResource() {

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.redis.outbound;
import java.util.Collection;
@@ -22,14 +23,15 @@ import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean;
import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType;
import org.springframework.data.redis.support.collections.RedisList;
@@ -39,6 +41,7 @@ import org.springframework.data.redis.support.collections.RedisSet;
import org.springframework.data.redis.support.collections.RedisStore;
import org.springframework.data.redis.support.collections.RedisZSet;
import org.springframework.expression.Expression;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.integration.Message;
@@ -66,6 +69,7 @@ import org.springframework.util.NumberUtils;
*
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Mark Fisher
* @since 2.2
*/
public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHandler {
@@ -85,111 +89,72 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand
private volatile boolean mapKeyExpressionExplicitlySet;
private final RedisTemplate<String, ?> redisTemplate;
private volatile RedisTemplate<String, ?> redisTemplate = new StringRedisTemplate();
private volatile boolean redisTemplateExplicitlySet;
private volatile CollectionType collectionType = CollectionType.LIST;
private volatile boolean extractPayloadElements = true;
private volatile RedisSerializer<?> keySerializer;
private volatile RedisConnectionFactory connectionFactory;
private volatile RedisSerializer<?> valueSerializer;
private volatile boolean initialized;
private volatile RedisSerializer<?> hashKeySerializer;
private volatile RedisSerializer<?> hashValueSerializer;
private volatile boolean usingDefaultTemplate = true;
/**
* Will construct this instance using fully created and initialized instance of
* provided {@link RedisTemplate}
* Constructs an instance using the provided {@link RedisTemplate}.
* The RedisTemplate must be fully initialized.
*
* The default expression 'headers.{@link RedisHeaders#KEY}'
* will be used.
* @param redisTemplate
*/
public RedisCollectionPopulatingMessageHandler(RedisTemplate<String, ?> redisTemplate) {
this(redisTemplate, null);
}
/**
* Will construct this instance using
* provided {@link RedisTemplate} and {@link #keyExpression}. The RedisTemplate must
* be fully initialized.
* If {@link #keyExpression} is null, the default expression 'headers.{@link RedisHeaders#KEY}'
* will be used.
*
* @param redisTemplate
* @param keyExpression
*/
public RedisCollectionPopulatingMessageHandler(RedisTemplate<String, ?> redisTemplate, Expression keyExpression) {
Assert.notNull(redisTemplate, "'redisTemplate' must not be null");
this.redisTemplate = redisTemplate;
this.usingDefaultTemplate = false;
if (keyExpression != null) {
this.keyExpression = keyExpression;
}
this.redisTemplateExplicitlySet = true;
}
/**
* Will construct this instance using the provided {@link RedisConnectionFactory}.
* It will create an instance of {@link RedisTemplate} with default serializers unless those
* are overridden via this instance's corresponding setters.
* Constructs an instance using the provided {@link RedisConnectionFactory}.
* It will use either a {@link StringRedisTemplate} if {@link #extractPayloadElements} is
* true (default) or a {@link RedisTemplate} with {@link StringRedisSerializer}s for
* keys and hash keys and {@link JdkSerializationRedisSerializer}s for values and
* hash values, when it is false.
*
* The default expression 'headers.{@link RedisHeaders#KEY}'
* will be used.
* @see #setExtractPayloadElements(boolean)
* @param connectionFactory
*/
public RedisCollectionPopulatingMessageHandler(RedisConnectionFactory connectionFactory) {
this(connectionFactory, null);
Assert.notNull(connectionFactory, "'connectionFactory' must not be null");
this.connectionFactory = connectionFactory;
}
/**
* Specifies the key for the Redis store. If an expression is needed, then call
* {@link #setKeyExpression(Expression)} instead of this method (they are mutually exclusive).
* If neither setter is called, the default expression will be 'headers.{@link RedisHeaders#KEY}'.
*
* @see #setKeyExpression(Expression)
* @param key
*/
public void setKey(String key) {
Assert.hasText(key, "key must not be empty");
this.setKeyExpression(new LiteralExpression(key));
}
/**
* Will construct this instance using the provided {@link RedisConnectionFactory} and {@link #keyExpression}
* It will create an instance of {@link RedisTemplate} with default serializers unless those
* are overridden via this instance's corresponding setters.
* Specifies a SpEL Expression to be used to determine the key for the Redis store.
* If an expression is not needed, then a literal value may be passed to the
* {@link #setKey(String)} method instead of this one (they are mutually exclusive).
* If neither setter is called, the default expression will be 'headers.{@link RedisHeaders#KEY}'.
*
* If {@link #keyExpression} is null, the default expression 'headers.{@link RedisHeaders#KEY}'
* will be used.
*
* @param connectionFactory
* @see #setKey(String)
* @param keyExpression
*/
public RedisCollectionPopulatingMessageHandler(RedisConnectionFactory connectionFactory, Expression keyExpression) {
Assert.notNull(connectionFactory, "'connectionFactory' must not be null");
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(connectionFactory);
this.redisTemplate = redisTemplate;
if (keyExpression != null) {
this.keyExpression = keyExpression;
}
}
public void setKeySerializer(RedisSerializer<?> keySerializer) {
Assert.state(this.usingDefaultTemplate, "'keySerializer' can not be set if a RedisTemplate was explicitly provided");
Assert.notNull(keySerializer, "'keySerializer' must not be null");
this.keySerializer = keySerializer;
}
public void setValueSerializer(RedisSerializer<?> valueSerializer) {
Assert.state(this.usingDefaultTemplate, "'valueSerializer' can not be set if a RedisTemplate was explicitly provided");
Assert.notNull(valueSerializer, "'valueSerializer' must not be null");
this.valueSerializer = valueSerializer;
}
public void setHashKeySerializer(RedisSerializer<?> hashKeySerializer) {
Assert.state(this.usingDefaultTemplate, "'hashKeySerializer' can not be set if a RedisTemplate was explicitly provided");
Assert.notNull(hashKeySerializer, "'hashKeySerializer' must not be null");
this.hashKeySerializer = hashKeySerializer;
}
public void setHashValueSerializer(RedisSerializer<?> hashValueSerializer) {
Assert.state(this.usingDefaultTemplate, "'hashValueSerializer' can not be set if a RedisTemplate was explicitly provided");
Assert.notNull(hashValueSerializer, "'hashValueSerializer' must not be null");
this.hashValueSerializer = hashValueSerializer;
public void setKeyExpression(Expression keyExpression) {
Assert.notNull(keyExpression, "keyExpression must not be null");
this.keyExpression = keyExpression;
}
/**
@@ -209,8 +174,6 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand
* the value of this attribute is meaningless as the payload will always be
* stored as a single entry.
*
* @see #setExtractPayloadElements(boolean)
*
* @param extractPayloadElements
*/
public void setExtractPayloadElements(boolean extractPayloadElements) {
@@ -245,22 +208,18 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand
Assert.state(!this.mapKeyExpressionExplicitlySet ||
(this.collectionType == CollectionType.MAP || this.collectionType == CollectionType.PROPERTIES),
"'mapKeyExpression' can only be set for CollectionType.MAP or CollectionType.PROPERTIES");
if (this.usingDefaultTemplate){
if (this.keySerializer != null){
redisTemplate.setKeySerializer(this.keySerializer);
}
if (this.valueSerializer != null){
redisTemplate.setValueSerializer(this.valueSerializer);
}
if (this.hashKeySerializer != null){
redisTemplate.setHashKeySerializer(this.hashKeySerializer);
}
if (this.hashValueSerializer != null){
redisTemplate.setHashValueSerializer(this.hashValueSerializer);
if (!this.redisTemplateExplicitlySet) {
if (!this.extractPayloadElements) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
StringRedisSerializer serializer = new StringRedisSerializer();
template.setKeySerializer(serializer);
template.setHashKeySerializer(serializer);
this.redisTemplate = template;
}
this.redisTemplate.setConnectionFactory(this.connectionFactory);
this.redisTemplate.afterPropertiesSet();
}
this.initialized = true;
}
/**
@@ -292,12 +251,12 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
String key = this.keyExpression.getValue(this.evaluationContext, message, String.class);
Assert.hasText(key, "Cannot determine a 'key' for a Redis store. The key can be provided via the " +
"'key' or 'key-expression' attributes.");
Assert.hasText(key, "Failed to determine a key for the Redis store using expression: "
+ this.keyExpression.getExpressionString());
RedisStore store = this.createStoreView(key);
Assert.state(this.initialized, "handler not initialized - afterPropertiesSet() must be called before the first use");
try {
if (collectionType == CollectionType.ZSET) {
this.handleZset((RedisZSet<Object>) store, message);

View File

@@ -335,6 +335,11 @@
<xsd:documentation>
RedisTemplate to be used with this adapter
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.data.redis.core.RedisTemplate"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
@@ -520,54 +525,6 @@
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="key-serializer" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<xsd:documentation>
Reference to an instance of org.springframework.data.redis.serializer.RedisSerializer
</xsd:documentation>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.data.redis.serializer.RedisSerializer"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="value-serializer" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<xsd:documentation>
Reference to an instance of org.springframework.data.redis.serializer.RedisSerializer
</xsd:documentation>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.data.redis.serializer.RedisSerializer"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="hash-key-serializer" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<xsd:documentation>
Reference to an instance of org.springframework.data.redis.serializer.RedisSerializer
</xsd:documentation>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.data.redis.serializer.RedisSerializer"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="hash-value-serializer" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<xsd:documentation>
Reference to an instance of org.springframework.data.redis.serializer.RedisSerializer
</xsd:documentation>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.data.redis.serializer.RedisSerializer"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="collection-type">
<xsd:annotation>
<xsd:documentation>

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.redis.config;
import static org.junit.Assert.assertEquals;
@@ -20,7 +21,6 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
@@ -49,7 +49,6 @@ public class RedisCollectionInboundChannelAdapterIntegrationTests extends RedisA
spca.start();
QueueChannel redisChannel = context.getBean("redisChannel", QueueChannel.class);
Message<RedisList<Object>> message = (Message<RedisList<Object>>) redisChannel.receive(1000);
assertNotNull(message);
assertEquals(13, message.getPayload().size());
@@ -72,7 +71,6 @@ public class RedisCollectionInboundChannelAdapterIntegrationTests extends RedisA
spca.start();
QueueChannel redisChannel = context.getBean("redisChannel", QueueChannel.class);
Message<RedisList<Object>> message = (Message<RedisList<Object>>) redisChannel.receive(1000);
assertNotNull(message);
assertEquals(13, message.getPayload().size());
@@ -216,7 +214,6 @@ public class RedisCollectionInboundChannelAdapterIntegrationTests extends RedisA
message = (Message<RedisZSet<Object>>) otherRedisChannel.receive(1000);
assertNotNull(message);
assertEquals(2, message.getPayload().rangeByScore(18, 18).size());
//message.getPayload().remo
zsetAdapterWithSingleScoreAndSynchronization.stop();
Thread.sleep(1000);

View File

@@ -7,28 +7,37 @@
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/redis http://www.springframework.org/schema/integration/redis/spring-integration-redis-2.2.xsd">
<int-redis:store-inbound-channel-adapter id="adapterWithConnectionFactory"
<int-redis:store-inbound-channel-adapter id="withStringTemplate"
connection-factory="redisConnectionFactory"
key-expression="'presidents'"
channel="redisChannel"
auto-startup="false"
collection-type="LIST"
key-serializer="keySerializer"
value-serializer="valueSerializer"
hash-key-serializer="hashKeySerializer"
hash-value-serializer="hashValueSerializer">
collection-type="LIST">
<int:poller fixed-rate="2000" max-messages-per-poll="10"/>
</int-redis:store-inbound-channel-adapter>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"/>
<property name="keySerializer" ref="keySerializer"/>
<property name="valueSerializer" ref="valueSerializer"/>
<property name="hashKeySerializer" ref="hashKeySerializer"/>
<property name="hashValueSerializer" ref="hashValueSerializer"/>
</bean>
<int-redis:store-inbound-channel-adapter id="withExternalTemplate"
key-expression="'presidents'"
channel="redisChannel"
auto-startup="false"
redis-template="redisTemplate"
collection-type="LIST">
<int:poller fixed-rate="2000" max-messages-per-poll="10"/>
</int-redis:store-inbound-channel-adapter>
<int:channel id="redisChannel"/>
<bean id="keySerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="valueSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="hashKeySerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="hashKeySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="hashValueSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="redisConnectionFactory" class="org.mockito.Mockito" factory-method="mock">

View File

@@ -16,36 +16,52 @@
package org.springframework.integration.redis.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.integration.redis.inbound.RedisStoreMessageSource;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Oleg Zhurakousky
* @author Gary Russell
* @since 2.2
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class RedisCollectionInboundChannelAdapterParserTests {
@Autowired
private ApplicationContext context;
@Autowired
private RedisTemplate<?,?> redisTemplate;
@Test
public void validateFullConfiguration(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("inbound-store-adapter-parser.xml", this.getClass());
RedisStoreMessageSource source =
TestUtils.getPropertyValue(context.getBean("adapterWithConnectionFactory"), "source", RedisStoreMessageSource.class);
assertEquals(context.getBean("keySerializer"), TestUtils.getPropertyValue(source, "keySerializer"));
assertEquals(context.getBean("valueSerializer"), TestUtils.getPropertyValue(source, "valueSerializer"));
assertEquals(context.getBean("hashKeySerializer"), TestUtils.getPropertyValue(source, "hashKeySerializer"));
assertEquals(context.getBean("hashValueSerializer"), TestUtils.getPropertyValue(source, "hashValueSerializer"));
assertEquals("'presidents'", ((SpelExpression)TestUtils.getPropertyValue(source, "keyExpression")).getExpressionString());
assertEquals("LIST", ((CollectionType)TestUtils.getPropertyValue(source, "collectionType")).toString());
public void validateWithStringTemplate(){
RedisStoreMessageSource withStringTemplate =
TestUtils.getPropertyValue(context.getBean("withStringTemplate"), "source", RedisStoreMessageSource.class);
assertEquals("'presidents'", ((SpelExpression)TestUtils.getPropertyValue(withStringTemplate, "keyExpression")).getExpressionString());
assertEquals("LIST", ((CollectionType)TestUtils.getPropertyValue(withStringTemplate, "collectionType")).toString());
assertTrue(TestUtils.getPropertyValue(withStringTemplate, "redisTemplate") instanceof StringRedisTemplate);
}
@Test(expected=BeanDefinitionParsingException.class)
public void validateFailureIfTemplateAndSerializers(){
new ClassPathXmlApplicationContext("inbound-store-adapter-parser-fail.xml", this.getClass());
@Test
public void validateWithExternalTemplate(){
RedisStoreMessageSource withExternalTemplate =
TestUtils.getPropertyValue(context.getBean("withExternalTemplate"), "source", RedisStoreMessageSource.class);
assertEquals("'presidents'", ((SpelExpression)TestUtils.getPropertyValue(withExternalTemplate, "keyExpression")).getExpressionString());
assertEquals("LIST", ((CollectionType)TestUtils.getPropertyValue(withExternalTemplate, "collectionType")).toString());
assertSame(redisTemplate, TestUtils.getPropertyValue(withExternalTemplate, "redisTemplate"));
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.redis.config;
import static org.junit.Assert.assertEquals;
@@ -26,12 +27,12 @@ import java.util.Properties;
import java.util.Set;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.support.collections.DefaultRedisList;
import org.springframework.data.redis.support.collections.DefaultRedisMap;
import org.springframework.data.redis.support.collections.DefaultRedisSet;
@@ -55,19 +56,18 @@ import org.springframework.integration.test.util.TestUtils;
/**
* @author Oleg Zhurakousky
* @author Mark Fisher
* @since 2.2
*/
public class RedisCollectionOutboundChannelAdapterIntegrationTests extends RedisAvailableTests {
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testListWithKeyAsHeader(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisList<String> redisList =
new DefaultRedisList<String>("pepboys",
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
new DefaultRedisList<String>("pepboys", this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisList.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
@@ -82,14 +82,12 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
assertEquals(3, redisList.size());
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testListWithProvidedKey(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisList<String> redisList =
new DefaultRedisList<String>("pepboys",
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
new DefaultRedisList<String>("pepboys", this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisList.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
@@ -104,14 +102,12 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
assertEquals(3, redisList.size());
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testMapToZsetWithProvidedKey(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisZSet<String> redisZset =
new DefaultRedisZSet<String>("presidents",
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
new DefaultRedisZSet<String>("presidents", this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisZset.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
@@ -136,14 +132,13 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
assertEquals("'presidents'", TestUtils.getPropertyValue(handler, "keyExpression", SpelExpression.class).getExpressionString());
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testMapToMapWithProvidedKey(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMap<String, String> redisMap =
new DefaultRedisMap<String, String>("pepboys",
(RedisOperations<String, ?>) this.initTemplate(jcf, new RedisTemplate<String, Map<String, String>>()));
this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisMap.size());
@@ -154,7 +149,6 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
pepboys.put("2", "Moe");
pepboys.put("3", "Jack");
Message<Map<String, String>> message = MessageBuilder.withPayload(pepboys).build();
redisChannel.send(message);
assertEquals("Manny", redisMap.get("1"));
@@ -167,14 +161,13 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
assertEquals("'foo'", TestUtils.getPropertyValue(handler, "mapKeyExpression", SpelExpression.class).getExpressionString());
}
@SuppressWarnings("unchecked")
@Test(expected=MessageHandlingException.class)// map key is not proivided
@Test(expected=MessageHandlingException.class) // map key is not provided
@RedisAvailable
public void testMapToMapAsSingleEntryWithKeyAsHeaderFail(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMap<String, Map<String, String>> redisMap =
new DefaultRedisMap<String, Map<String, String>>("pepboys",
(RedisOperations<String, ?>) this.initTemplate(jcf, new RedisTemplate<String, Map<String, Map<String, String>>>()));
this.initTemplate(jcf, new RedisTemplate<String, Map<String, Map<String, String>>>()));
assertEquals(0, redisMap.size());
@@ -185,20 +178,21 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
pepboys.put("2", "Moe");
pepboys.put("3", "Jack");
Message<Map<String, String>> message = MessageBuilder.withPayload(pepboys).
setHeader(RedisHeaders.KEY, "pepboys").build();
redisChannel.send(message);
}
@SuppressWarnings("unchecked")
@Test(expected=MessageHandlingException.class)//key is not provided
@Test(expected=MessageHandlingException.class) // key is not provided
@RedisAvailable
public void testMapToMapNoKey(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisTemplate<String, Map<String, Map<String, String>>> redisTemplate = new RedisTemplate<String, Map<String, Map<String, String>>>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
RedisMap<String, Map<String, String>> redisMap =
new DefaultRedisMap<String, Map<String, String>>("pepboys",
(RedisOperations<String, ?>) this.initTemplate(jcf, new RedisTemplate<String, Map<String, Map<String, String>>>()));
this.initTemplate(jcf, redisTemplate));
assertEquals(0, redisMap.size());
@@ -209,19 +203,20 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
pepboys.put("2", "Moe");
pepboys.put("3", "Jack");
Message<Map<String, String>> message = MessageBuilder.withPayload(pepboys).build();
redisChannel.send(message);
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testMapToMapAsSingleEntryWithKeyAsHeader(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisTemplate<String, Map<String, Map<String, String>>> redisTemplate = new RedisTemplate<String, Map<String, Map<String, String>>>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
RedisMap<String, Map<String, String>> redisMap =
new DefaultRedisMap<String, Map<String, String>>("pepboys",
(RedisOperations<String, ?>) this.initTemplate(jcf, new RedisTemplate<String, Map<String, Map<String, String>>>()));
this.initTemplate(jcf, redisTemplate));
assertEquals(0, redisMap.size());
@@ -232,7 +227,6 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
pepboys.put("2", "Moe");
pepboys.put("3", "Jack");
Message<Map<String, String>> message = MessageBuilder.withPayload(pepboys).
setHeader(RedisHeaders.KEY, "pepboys").setHeader(RedisHeaders.MAP_KEY, "foo").build();
redisChannel.send(message);
@@ -243,15 +237,35 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
assertEquals("Jack", pepboyz.get("3"));
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testStoreSimpleStringInMap(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
StringRedisTemplate redisTemplate = new StringRedisTemplate();
RedisMap<String, String> redisMap =
new DefaultRedisMap<String, String>("bar",
this.initTemplate(jcf, redisTemplate));
assertEquals(0, redisMap.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("simpleMap", MessageChannel.class);
Message<String> message = MessageBuilder.withPayload("hello, world!").
setHeader(RedisHeaders.KEY, "bar").setHeader(RedisHeaders.MAP_KEY, "foo").build();
redisChannel.send(message);
String hello = redisMap.get("foo");
assertEquals("hello, world!", hello);
}
@Test
@RedisAvailable
public void testSetWithKeyAsHeader(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisSet<String> redisList =
new DefaultRedisSet<String>("pepboys",
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
assertEquals(0, redisList.size());
RedisSet<String> redisSet =
new DefaultRedisSet<String>("pepboys", this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisSet.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("set", MessageChannel.class);
@@ -262,18 +276,19 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
Message<Set<String>> message = MessageBuilder.withPayload(pepboys).setHeader("redis_key", "pepboys").build();
redisChannel.send(message);
assertEquals(3, redisList.size());
assertEquals(3, redisSet.size());
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testSetWithKeyAsHeaderNotParsed(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisSet<String> redisList =
new DefaultRedisSet<String>("pepboys",
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
assertEquals(0, redisList.size());
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
RedisSet<String> redisSet =
new DefaultRedisSet<String>("pepboys", this.initTemplate(jcf, redisTemplate));
assertEquals(0, redisSet.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("setNotParsed", MessageChannel.class);
@@ -284,18 +299,16 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
Message<Set<String>> message = MessageBuilder.withPayload(pepboys).setHeader("redis_key", "pepboys").build();
redisChannel.send(message);
assertEquals(1, redisList.size());
assertEquals(1, redisSet.size());
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testPojoIntoSet(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisSet<String> redisList =
new DefaultRedisSet<String>("pepboys",
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
assertEquals(0, redisList.size());
RedisSet<String> redisSet =
new DefaultRedisSet<String>("pepboys", this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisSet.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("pojoIntoSet", MessageChannel.class);
@@ -303,17 +316,15 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
Message<String> message = MessageBuilder.withPayload(pepboy).setHeader("redis_key", "pepboys").build();
redisChannel.send(message);
assertEquals(1, redisList.size());
assertEquals(1, redisSet.size());
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testProperty(){
public void testProperties(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisProperties redisProperties =
new RedisProperties("pepboys",
(RedisOperations<String, ?>) this.initTemplate(jcf, new RedisTemplate<String, Properties>()));
new RedisProperties("pepboys", this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisProperties.size());
@@ -331,7 +342,7 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis
assertEquals("Jack", redisProperties.get("3"));
}
private RedisTemplate<?,?> initTemplate(RedisConnectionFactory rcf, RedisTemplate<?, ?> redisTemplate){
private <K,V> RedisTemplate<K,V> initTemplate(RedisConnectionFactory rcf, RedisTemplate<K,V> redisTemplate){
redisTemplate.setConnectionFactory(rcf);
redisTemplate.afterPropertiesSet();
return redisTemplate;

View File

@@ -11,14 +11,15 @@
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<int-redis:store-outbound-channel-adapter id="storeAdapter"
redis-template="redisTemplate"
<int-redis:store-outbound-channel-adapter id="withStringTemplate"
collection-type="PROPERTIES"
key="pepboys"
key-serializer="keySerializer"
value-serializer="valueSerializer"
hash-key-serializer="hashKeySerializer"
hash-value-serializer="hashValueSerializer"
auto-startup="false"/>
<int-redis:store-outbound-channel-adapter id="withStringObjectTemplate"
collection-type="PROPERTIES"
key="pepboys"
extract-payload-elements="false"
auto-startup="false"/>
<bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
@@ -28,10 +29,20 @@
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"/>
<property name="keySerializer" ref="keySerializer"/>
<property name="valueSerializer" ref="valueSerializer"/>
<property name="hashKeySerializer" ref="hashKeySerializer"/>
<property name="hashValueSerializer" ref="hashValueSerializer"/>
</bean>
<bean id="redisConnectionFactory" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.data.redis.connection.RedisConnectionFactory"/>
</bean>
<int-redis:store-outbound-channel-adapter id="withExternalTemplate"
collection-type="PROPERTIES"
key="pepboys"
redis-template="redisTemplate"
auto-startup="false"/>
</beans>

View File

@@ -13,38 +13,71 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.redis.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.redis.outbound.RedisCollectionPopulatingMessageHandler;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
* @author Oleg Zhurakousky
* @author Gary Russell
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class RedisCollectionOutboundChannelAdapterParserTests {
@Autowired
private ApplicationContext context;
@Autowired
private RedisTemplate<?,?> redisTemplate;
@Test
public void validateFullConfiguration(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter-parser.xml", this.getClass());
RedisCollectionPopulatingMessageHandler handler =
TestUtils.getPropertyValue(context.getBean("storeAdapter.adapter"), "handler", RedisCollectionPopulatingMessageHandler.class);
assertEquals(context.getBean("keySerializer"), TestUtils.getPropertyValue(handler, "keySerializer"));
assertEquals(context.getBean("valueSerializer"), TestUtils.getPropertyValue(handler, "valueSerializer"));
assertEquals(context.getBean("hashKeySerializer"), TestUtils.getPropertyValue(handler, "hashKeySerializer"));
assertEquals(context.getBean("hashValueSerializer"), TestUtils.getPropertyValue(handler, "hashValueSerializer"));
assertEquals("pepboys", ((LiteralExpression)TestUtils.getPropertyValue(handler, "keyExpression")).getExpressionString());
assertEquals("PROPERTIES", ((CollectionType)TestUtils.getPropertyValue(handler, "collectionType")).toString());
public void validateWithStringTemplate(){
RedisCollectionPopulatingMessageHandler withStringTemplate =
TestUtils.getPropertyValue(context.getBean("withStringTemplate.adapter"), "handler", RedisCollectionPopulatingMessageHandler.class);
assertEquals("pepboys", ((LiteralExpression)TestUtils.getPropertyValue(withStringTemplate, "keyExpression")).getExpressionString());
assertEquals("PROPERTIES", ((CollectionType)TestUtils.getPropertyValue(withStringTemplate, "collectionType")).toString());
assertTrue(TestUtils.getPropertyValue(withStringTemplate, "redisTemplate") instanceof StringRedisTemplate);
}
@Test(expected=BeanDefinitionParsingException.class)
public void validateFailureIfTemplateAndSerializers(){
new ClassPathXmlApplicationContext("store-outbound-adapter-parser-fail.xml", this.getClass());
@Test
public void validateWithStringObjectTemplate(){
RedisCollectionPopulatingMessageHandler withStringObjectTemplate =
TestUtils.getPropertyValue(context.getBean("withStringObjectTemplate.adapter"), "handler", RedisCollectionPopulatingMessageHandler.class);
assertEquals("pepboys", ((LiteralExpression)TestUtils.getPropertyValue(withStringObjectTemplate, "keyExpression")).getExpressionString());
assertEquals("PROPERTIES", ((CollectionType)TestUtils.getPropertyValue(withStringObjectTemplate, "collectionType")).toString());
assertFalse(TestUtils.getPropertyValue(withStringObjectTemplate, "redisTemplate") instanceof StringRedisTemplate);
assertTrue(TestUtils.getPropertyValue(withStringObjectTemplate, "redisTemplate.keySerializer") instanceof StringRedisSerializer);
assertTrue(TestUtils.getPropertyValue(withStringObjectTemplate, "redisTemplate.hashKeySerializer") instanceof StringRedisSerializer);
assertTrue(TestUtils.getPropertyValue(withStringObjectTemplate, "redisTemplate.valueSerializer") instanceof JdkSerializationRedisSerializer);
assertTrue(TestUtils.getPropertyValue(withStringObjectTemplate, "redisTemplate.hashValueSerializer") instanceof JdkSerializationRedisSerializer);
}
@Test
public void validateWithExternalTemplate(){
RedisCollectionPopulatingMessageHandler withExternalTemplate =
TestUtils.getPropertyValue(context.getBean("withExternalTemplate.adapter"), "handler", RedisCollectionPopulatingMessageHandler.class);
assertEquals("pepboys", ((LiteralExpression)TestUtils.getPropertyValue(withExternalTemplate, "keyExpression")).getExpressionString());
assertEquals("PROPERTIES", ((CollectionType)TestUtils.getPropertyValue(withExternalTemplate, "collectionType")).toString());
assertSame(redisTemplate, TestUtils.getPropertyValue(withExternalTemplate, "redisTemplate"));
}
}

View File

@@ -26,13 +26,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.redis.inbound.RedisInboundChannelAdapter;
import org.springframework.integration.redis.inbound.RedisStoreMessageSource;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.support.converter.SimpleMessageConverter;
@@ -92,46 +88,6 @@ public class RedisInboundChannelAdapterParserTests extends RedisAvailableTests{
assertSame(autoChannel, TestUtils.getPropertyValue(autoChannelAdapter, "outputChannel"));
}
@Test(expected=IllegalStateException.class)
public void cantSetKeySerializer() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(new JedisConnectionFactory());
RedisStoreMessageSource source = new RedisStoreMessageSource(redisTemplate,
new LiteralExpression("foo"));
source.setKeySerializer(new StringRedisSerializer());
source.afterPropertiesSet();
}
@Test(expected=IllegalStateException.class)
public void cantSetValueSerializer() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(new JedisConnectionFactory());
RedisStoreMessageSource source = new RedisStoreMessageSource(redisTemplate,
new LiteralExpression("foo"));
source.setValueSerializer(new StringRedisSerializer());
source.afterPropertiesSet();
}
@Test(expected=IllegalStateException.class)
public void cantSetHashKeySerializer() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(new JedisConnectionFactory());
RedisStoreMessageSource source = new RedisStoreMessageSource(redisTemplate,
new LiteralExpression("foo"));
source.setHashKeySerializer(new StringRedisSerializer());
source.afterPropertiesSet();
}
@Test(expected=IllegalStateException.class)
public void cantSetHashValueSerializer() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(new JedisConnectionFactory());
RedisStoreMessageSource source = new RedisStoreMessageSource(redisTemplate,
new LiteralExpression("foo"));
source.setHashValueSerializer(new StringRedisSerializer());
source.afterPropertiesSet();
}
@SuppressWarnings("unused")
private static class TestMessageConverter extends SimpleMessageConverter {
}

View File

@@ -1,33 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-redis="http://www.springframework.org/schema/integration/redis"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/redis http://www.springframework.org/schema/integration/redis/spring-integration-redis-2.2.xsd">
<int-redis:store-inbound-channel-adapter id="adapterWithTemplate"
redis-template="redisTemplate"
key-expression="'presidents'"
channel="redisChannel"
auto-startup="false"
collection-type="LIST"
key-serializer="serializer"
value-serializer="serializer"
hash-key-serializer="serializer"
hash-value-serializer="serializer">
<int:poller fixed-rate="2000" max-messages-per-poll="10"/>
</int-redis:store-inbound-channel-adapter>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"/>
<int:channel id="redisChannel"/>
<bean id="serializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="redisConnectionFactory" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.data.redis.connection.RedisConnectionFactory"/>
</bean>
</beans>

View File

@@ -73,6 +73,12 @@
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
</bean>
<int:channel id="redisChannel">

View File

@@ -1,32 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-redis="http://www.springframework.org/schema/integration/redis"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/redis http://www.springframework.org/schema/integration/redis/spring-integration-redis.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<int-redis:store-outbound-channel-adapter id="storeAdapter"
collection-type="PROPERTIES"
key="pepboys"
key-serializer="keySerializer"
value-serializer="valueSerializer"
hash-key-serializer="hashKeySerializer"
hash-value-serializer="hashValueSerializer"
auto-startup="false"/>
<bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="valueSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="hashKeySerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="hashValueSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="redisConnectionFactory" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.data.redis.connection.RedisConnectionFactory"/>
</bean>
</beans>

View File

@@ -37,6 +37,9 @@
collection-type="MAP"
extract-payload-elements="false"/>
<int-redis:store-outbound-channel-adapter id="simpleMap"
collection-type="MAP"/>
<int-redis:store-outbound-channel-adapter id="set"
collection-type="SET"/>

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.redis.outbound;
import static org.junit.Assert.assertEquals;
@@ -30,8 +31,8 @@ import org.junit.Test;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.data.redis.support.collections.DefaultRedisList;
import org.springframework.data.redis.support.collections.DefaultRedisZSet;
@@ -50,24 +51,23 @@ import org.springframework.integration.support.MessageBuilder;
/**
* @author Oleg Zhurakousky
* @author Gunnar Hillert
*
* @author Mark Fisher
*/
public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailableTests{
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testListWithListPayloadParsedAndProvidedKey() {
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisList<String> redisList =
new DefaultRedisList<String>(key,
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
new DefaultRedisList<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisList.size());
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(jcf);
handler.setKey(key);
handler.afterPropertiesSet();
List<String> list = new ArrayList<String>();
@@ -83,20 +83,18 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
assertEquals("Jack", redisList.get(2));
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testListWithListPayloadParsedAndProvidedKeyAsHeader() {
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisList<String> redisList =
new DefaultRedisList<String>(key,
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
new DefaultRedisList<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisList.size());
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, null);
new RedisCollectionPopulatingMessageHandler(jcf);
handler.afterPropertiesSet();
@@ -113,20 +111,18 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
assertEquals("Jack", redisList.get(2));
}
@SuppressWarnings("unchecked")
@RedisAvailable
@Test(expected=MessageHandlingException.class)
public void testListWithListPayloadParsedAndNoKey() {
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisList<String> redisList =
new DefaultRedisList<String>(key,
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
new DefaultRedisList<String>(key, this.initTemplate(jcf, new RedisTemplate<String, String>()));
assertEquals(0, redisList.size());
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, null);
new RedisCollectionPopulatingMessageHandler(jcf);
handler.afterPropertiesSet();
List<String> list = new ArrayList<String>();
@@ -137,21 +133,20 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
handler.handleMessage(message);
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testListWithListPayloadAsSingleEntry() {
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisList<List<String>> redisList =
new DefaultRedisList<List<String>>(key,
(RedisOperations<String, List<String>>) this.initTemplate(jcf, new RedisTemplate<String, List<String>>()));
new DefaultRedisList<List<String>>(key, this.initTemplate(jcf, new RedisTemplate<String, List<String>>()));
assertEquals(0, redisList.size());
RedisTemplate<String, List<String>> template = this.initTemplate(jcf, new RedisTemplate<String, List<String>>());
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(template);
handler.setKey(key);
handler.setExtractPayloadElements(false);
handler.afterPropertiesSet();
@@ -169,21 +164,19 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
assertEquals("Jack", resultList.get(2));
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testZsetWithListPayloadParsedAndProvidedKeyDefault() {
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisZSet<String> redisZset =
new DefaultRedisZSet<String>(key,
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
new DefaultRedisZSet<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisZset.size());
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(jcf);
handler.setKey(key);
handler.setCollectionType(CollectionType.ZSET);
handler.afterPropertiesSet();
@@ -209,21 +202,19 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
}
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testZsetWithListPayloadParsedAndProvidedKeyScoreIncrement() {
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisZSet<String> redisZset =
new DefaultRedisZSet<String>(key,
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
new DefaultRedisZSet<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisZset.size());
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(jcf);
handler.setKey(key);
handler.setCollectionType(CollectionType.ZSET);
handler.afterPropertiesSet();
@@ -252,21 +243,19 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
}
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testZsetWithListPayloadParsedAndProvidedKeyScoreIncrementAsStringHeader() {// see INT-2775
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisZSet<String> redisZset =
new DefaultRedisZSet<String>(key,
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
new DefaultRedisZSet<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisZset.size());
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(jcf);
handler.setKey(key);
handler.setCollectionType(CollectionType.ZSET);
handler.afterPropertiesSet();
@@ -295,20 +284,19 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
}
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testZsetWithListPayloadAsSingleEntryAndHeaderKeyHeaderScore() {
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisZSet<List<String>> redisZset =
new DefaultRedisZSet<List<String>>(key,
(RedisOperations<String, List<String>>) this.initTemplate(jcf, new RedisTemplate<String, List<String>>()));
new DefaultRedisZSet<List<String>>(key, this.initTemplate(jcf, new RedisTemplate<String, List<String>>()));
assertEquals(0, redisZset.size());
RedisTemplate<String, List<String>> template = this.initTemplate(jcf, new RedisTemplate<String, List<String>>());
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, null);
new RedisCollectionPopulatingMessageHandler(template);
handler.setCollectionType(CollectionType.ZSET);
handler.setExtractPayloadElements(false);
@@ -329,21 +317,19 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
}
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testZsetWithMapPayloadParsedHeaderKey() {
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "presidents";
RedisZSet<String> redisZset =
new DefaultRedisZSet<String>(key,
(RedisOperations<String, String>) this.initTemplate(jcf, new RedisTemplate<String, String>()));
new DefaultRedisZSet<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisZset.size());
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(jcf);
handler.setKey(key);
handler.setCollectionType(CollectionType.ZSET);
handler.afterPropertiesSet();
@@ -373,21 +359,20 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
assertEquals(6, entries.size());
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testZsetWithMapPayloadPojoParsedHeaderKey() {
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "presidents";
RedisZSet<President> redisZset =
new DefaultRedisZSet<President>(key,
(RedisOperations<String, President>) this.initTemplate(jcf, new RedisTemplate<String, President>()));
new DefaultRedisZSet<President>(key, this.initTemplate(jcf, new RedisTemplate<String, President>()));
assertEquals(0, redisZset.size());
RedisTemplate<String, President> template = this.initTemplate(jcf, new RedisTemplate<String, President>());
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(template);
handler.setKey(key);
handler.setCollectionType(CollectionType.ZSET);
handler.afterPropertiesSet();
@@ -417,21 +402,20 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
assertEquals(6, entries.size());
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testZsetWithMapPayloadPojoAsSingleEntryHeaderKey() {
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "presidents";
RedisZSet<Map<President, Double>> redisZset =
new DefaultRedisZSet<Map<President, Double>>(key,
(RedisOperations<String, Map<President, Double>>) this.initTemplate(jcf, new RedisTemplate<String, Map<President, Double>>()));
new DefaultRedisZSet<Map<President, Double>>(key, this.initTemplate(jcf, new RedisTemplate<String, Map<President, Double>>()));
assertEquals(0, redisZset.size());
RedisTemplate<String, Map<President, Double>> template = this.initTemplate(jcf, new RedisTemplate<String, Map<President, Double>>());
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(template);
handler.setKey(key);
handler.setCollectionType(CollectionType.ZSET);
handler.setExtractPayloadElements(false);
handler.afterPropertiesSet();
@@ -454,7 +438,8 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(jcf);
handler.setKey(key);
handler.setMapKeyExpression(new LiteralExpression(key));
handler.afterPropertiesSet();
}
@@ -465,7 +450,8 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(jcf);
handler.setKey(key);
handler.setCollectionType(CollectionType.SET);
handler.setMapKeyExpression(new LiteralExpression(key));
handler.afterPropertiesSet();
@@ -477,7 +463,8 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(jcf);
handler.setKey(key);
handler.setCollectionType(CollectionType.ZSET);
handler.setMapKeyExpression(new LiteralExpression(key));
handler.afterPropertiesSet();
@@ -489,7 +476,8 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(jcf);
handler.setKey(key);
handler.setCollectionType(CollectionType.MAP);
handler.setMapKeyExpression(new LiteralExpression(key));
try {
@@ -506,7 +494,8 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
String key = "foo";
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
new RedisCollectionPopulatingMessageHandler(jcf);
handler.setKey(key);
handler.setCollectionType(CollectionType.PROPERTIES);
handler.setMapKeyExpression(new LiteralExpression(key));
try {
@@ -517,7 +506,7 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
}
}
private RedisTemplate<?,?> initTemplate(RedisConnectionFactory rcf, RedisTemplate<?, ?> redisTemplate) {
private <K,V> RedisTemplate<K,V> initTemplate(RedisConnectionFactory rcf, RedisTemplate<K,V> redisTemplate) {
redisTemplate.setConnectionFactory(rcf);
redisTemplate.afterPropertiesSet();
return redisTemplate;

View File

@@ -18,7 +18,6 @@ package org.springframework.integration.redis.rules;
import java.util.UUID;
import org.junit.Rule;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
@@ -26,9 +25,11 @@ import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
/**
* @author Oleg Zhurakousky
* @author Gary Russell
*
*/
public class RedisAvailableTests {
@@ -55,10 +56,10 @@ public class RedisAvailableTests {
protected void prepareList(JedisConnectionFactory jcf){
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(jcf);
redisTemplate.afterPropertiesSet();
BoundListOperations<Object, Object> ops = redisTemplate.boundListOps("presidents");
BoundListOperations<String, String> ops = redisTemplate.boundListOps("presidents");
ops.rightPush("John Adams");
@@ -79,11 +80,11 @@ public class RedisAvailableTests {
protected void prepareZset(JedisConnectionFactory jcf){
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(jcf);
redisTemplate.afterPropertiesSet();
BoundZSetOperations<Object, Object> ops = redisTemplate.boundZSetOps("presidents");
BoundZSetOperations<String, String> ops = redisTemplate.boundZSetOps("presidents");
ops.add("John Adams", 18);

View File

@@ -247,22 +247,6 @@ rt.setConnectionFactory(redisConnectionFactory);]]></programlisting>
<listitem>
<para><code>collection-type</code> - enumeration of the Collection types supported by this adapter. Supported Collections are: LIST, SET, ZSET, PROPERTIES, MAP </para>
</listitem>
<listitem>
<para><code>key-serializer</code> - reference to an instance of <code>org.springframework.data.redis.serializer.RedisSerializer</code>
to be used while serializing keys </para>
</listitem>
<listitem>
<para><code>value-serializer</code> - reference to an instance of <code>org.springframework.data.redis.serializer.RedisSerializer</code>
to be used while serializing values </para>
</listitem>
<listitem>
<para><code>hash-key-serializer</code> - reference to an instance of <code>org.springframework.data.redis.serializer.RedisSerializer</code>
to be used while serializing hash keys </para>
</listitem>
<listitem>
<para><code>hash-value-serializer</code> - reference to an instance of <code>org.springframework.data.redis.serializer.RedisSerializer</code>
to be used while serializing hash values </para>
</listitem>
<listitem>
<para><code>connection-factory</code> -
reference to an instance of <classname>org.springframework.data.redis.connection.RedisConnectionFactory</classname> </para>
@@ -278,6 +262,28 @@ rt.setConnectionFactory(redisConnectionFactory);]]></programlisting>
<note>
You cannot set both <code>redis-template</code> and <code>connection-factory</code>.
</note>
<important>
By default, the adapter uses a <classname>StringRedisTemplate</classname>; this uses
<classname>StringRedisSerializer</classname>s for keys, values, hash keys and hash values. If your
Redis store contains objects that are serialized with other techniques, you must supply a
<classname>RedisTemplate</classname> configured with appropriate serializers.
For example, if the store is written to using a RedisStore Outbound Adapter that has its
<code>extract-payload-elements</code> set to false, you must provide a
<classname>RedisTemplate</classname> configured thus:
<programlisting><![CDATA[<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
</bean>]]></programlisting>
<para>
This uses String serializers for keys and hash keys and the default JDK Serialization serializers for
values and hash values.
</para>
</important>
<para>
The example above is relatively simple and static since it has a literal value for the <code>key</code>.
Sometimes, you may need to change the value of the key at runtime based on some condition.
@@ -367,22 +373,6 @@ rt.setConnectionFactory(redisConnectionFactory);]]></programlisting>
<listitem>
<para><code>collection-type</code> - enumeration of the Collection types supported by this adapter. Supported Collections are: LIST, SET, ZSET, PROPERTIES, MAP </para>
</listitem>
<listitem>
<para><code>key-serializer</code> - reference to an instance of the <code>org.springframework.data.redis.serializer.RedisSerializer</code>
to be used while serializing keys </para>
</listitem>
<listitem>
<para><code>value-serializer</code> - reference to an instance of the <code>org.springframework.data.redis.serializer.RedisSerializer</code>
to be used while serializing values </para>
</listitem>
<listitem>
<para><code>hash-key-serializer</code> - reference to an instance of the <code>org.springframework.data.redis.serializer.RedisSerializer</code>
to be used while serializing hash keys </para>
</listitem>
<listitem>
<para><code>hash-value-serializer</code> - reference to an instance of the <code>org.springframework.data.redis.serializer.RedisSerializer</code>
to be used while serializing hash values </para>
</listitem>
<listitem>
<para><code>map-key-expression</code> - SpEL expression that returns the name of the key for entry being
stored. Only applies if the <code>collection-type</code> is MAP or PROPERTIES and
@@ -394,12 +384,25 @@ rt.setConnectionFactory(redisConnectionFactory);]]></programlisting>
</listitem>
<listitem>
<para><code>redis-template</code> -
reference to an instance of <classname>org.springframework.data.redis.core.RedisTemplate</classname>
(NOTE: you can not have both redis-template and connection-factory set)</para>
reference to an instance of <classname>org.springframework.data.redis.core.RedisTemplate</classname></para>
</listitem>
</itemizedlist>
and other attributes that are common across all other inbound adapters (e.g., 'channel').
<note>
You cannot set both <code>redis-template</code> and <code>connection-factory</code>.
</note>
</para>
<important>
By default, the adapter uses a <classname>StringRedisTemplate</classname>; this uses
<classname>StringRedisSerializer</classname>s for keys, values, hash keys and hash values. However, if
<code>extract-payload-elements</code> is set to false, a <classname>RedisTemplate</classname> using
<classname>StringRedisSerializer</classname>s for keys and hash keys, and
<classname>JdkSerializationRedisSerializer</classname>s for values and hash values will be used. With the JDK serializer, it is important
to understand that java serialization is used for all values, regardless of whether the value
is actually a collection or not. If you need more control over
the serialization of values, you may want to consider providing your own
<classname>RedisTemplate</classname> rather than relying upon these defaults.
</important>
<para>
The example above is relatively simple and static since it has a literal values for the <code>key</code> and other attributes.
Sometimes you may need to change the values dynamically at runtime based on some condition.