diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterParser.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterParser.java index 3497e7457c..4e24db3271 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterParser.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterParser.java @@ -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); diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterParser.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterParser.java index 1d2bc76d14..e059c22539 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterParser.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterParser.java @@ -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(); } -} \ No newline at end of file + +} diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/config/RedisParserUtils.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/config/RedisParserUtils.java deleted file mode 100644 index da398a8bc5..0000000000 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/config/RedisParserUtils.java +++ /dev/null @@ -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"); - } -} diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/inbound/RedisStoreMessageSource.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/inbound/RedisStoreMessageSource.java index c1beb4644e..d829837ce9 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/inbound/RedisStoreMessageSource.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/inbound/RedisStoreMessageSource.java @@ -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 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 redisTemplate = new RedisTemplate(); + 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() { diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandler.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandler.java index 51afb94269..41e7aa49cc 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandler.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandler.java @@ -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 redisTemplate; + private volatile RedisTemplate 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 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 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 redisTemplate = new RedisTemplate(); - 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 template = new RedisTemplate(); + 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) store, message); diff --git a/spring-integration-redis/src/main/resources/org/springframework/integration/redis/config/spring-integration-redis-2.2.xsd b/spring-integration-redis/src/main/resources/org/springframework/integration/redis/config/spring-integration-redis-2.2.xsd index 804a3012ad..c5e813a52e 100644 --- a/spring-integration-redis/src/main/resources/org/springframework/integration/redis/config/spring-integration-redis-2.2.xsd +++ b/spring-integration-redis/src/main/resources/org/springframework/integration/redis/config/spring-integration-redis-2.2.xsd @@ -335,6 +335,11 @@ RedisTemplate to be used with this adapter + + + + + @@ -520,54 +525,6 @@ - - - - - Reference to an instance of org.springframework.data.redis.serializer.RedisSerializer - - - - - - - - - - - - Reference to an instance of org.springframework.data.redis.serializer.RedisSerializer - - - - - - - - - - - - Reference to an instance of org.springframework.data.redis.serializer.RedisSerializer - - - - - - - - - - - - Reference to an instance of org.springframework.data.redis.serializer.RedisSerializer - - - - - - - diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterIntegrationTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterIntegrationTests.java index 8bdfa03c29..3386e6bbdd 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterIntegrationTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterIntegrationTests.java @@ -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> message = (Message>) 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> message = (Message>) redisChannel.receive(1000); assertNotNull(message); assertEquals(13, message.getPayload().size()); @@ -216,7 +214,6 @@ public class RedisCollectionInboundChannelAdapterIntegrationTests extends RedisA message = (Message>) otherRedisChannel.receive(1000); assertNotNull(message); assertEquals(2, message.getPayload().rangeByScore(18, 18).size()); - //message.getPayload().remo zsetAdapterWithSingleScoreAndSynchronization.stop(); Thread.sleep(1000); diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/inbound-store-adapter-parser.xml b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterParserTests-context.xml similarity index 68% rename from spring-integration-redis/src/test/java/org/springframework/integration/redis/config/inbound-store-adapter-parser.xml rename to spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterParserTests-context.xml index cfe4572d5b..58220c21c9 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/inbound-store-adapter-parser.xml +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterParserTests-context.xml @@ -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"> - + collection-type="LIST"> + + + + + + + + - + - + diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterParserTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterParserTests.java index 94bbaa9da4..e3cec353f6 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterParserTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionInboundChannelAdapterParserTests.java @@ -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")); } } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterIntegrationTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterIntegrationTests.java index 6c3fa9a920..6c86a4ae99 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterIntegrationTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterIntegrationTests.java @@ -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 redisList = - new DefaultRedisList("pepboys", - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + new DefaultRedisList("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 redisList = - new DefaultRedisList("pepboys", - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + new DefaultRedisList("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 redisZset = - new DefaultRedisZSet("presidents", - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + new DefaultRedisZSet("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 redisMap = new DefaultRedisMap("pepboys", - (RedisOperations) this.initTemplate(jcf, new RedisTemplate>())); + 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> 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> redisMap = new DefaultRedisMap>("pepboys", - (RedisOperations) this.initTemplate(jcf, new RedisTemplate>>())); + this.initTemplate(jcf, new RedisTemplate>>())); assertEquals(0, redisMap.size()); @@ -185,20 +178,21 @@ public class RedisCollectionOutboundChannelAdapterIntegrationTests extends Redis pepboys.put("2", "Moe"); pepboys.put("3", "Jack"); - Message> 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>> redisTemplate = new RedisTemplate>>(); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); RedisMap> redisMap = new DefaultRedisMap>("pepboys", - (RedisOperations) this.initTemplate(jcf, new RedisTemplate>>())); + 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> message = MessageBuilder.withPayload(pepboys).build(); redisChannel.send(message); } - @SuppressWarnings("unchecked") @Test @RedisAvailable public void testMapToMapAsSingleEntryWithKeyAsHeader(){ JedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + RedisTemplate>> redisTemplate = new RedisTemplate>>(); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); RedisMap> redisMap = new DefaultRedisMap>("pepboys", - (RedisOperations) this.initTemplate(jcf, new RedisTemplate>>())); + 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> 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 redisMap = + new DefaultRedisMap("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 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 redisList = - new DefaultRedisSet("pepboys", - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); - assertEquals(0, redisList.size()); + RedisSet redisSet = + new DefaultRedisSet("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> 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 redisList = - new DefaultRedisSet("pepboys", - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); - assertEquals(0, redisList.size()); + RedisTemplate redisTemplate = new RedisTemplate(); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + RedisSet redisSet = + new DefaultRedisSet("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> 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 redisList = - new DefaultRedisSet("pepboys", - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); - assertEquals(0, redisList.size()); + RedisSet redisSet = + new DefaultRedisSet("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 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) this.initTemplate(jcf, new RedisTemplate())); + 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 RedisTemplate initTemplate(RedisConnectionFactory rcf, RedisTemplate redisTemplate){ redisTemplate.setConnectionFactory(rcf); redisTemplate.afterPropertiesSet(); return redisTemplate; diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter-parser-fail.xml b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterParserTests-context.xml similarity index 73% rename from spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter-parser-fail.xml rename to spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterParserTests-context.xml index 41bfd769a4..72600084e7 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter-parser-fail.xml +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterParserTests-context.xml @@ -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"> - + + @@ -28,10 +29,20 @@ + + + + + + diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterParserTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterParserTests.java index 90ce9966e7..fa8234bb8c 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterParserTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisCollectionOutboundChannelAdapterParserTests.java @@ -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")); } } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisInboundChannelAdapterParserTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisInboundChannelAdapterParserTests.java index 13592fa843..82d916eb8f 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisInboundChannelAdapterParserTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/RedisInboundChannelAdapterParserTests.java @@ -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 redisTemplate = new RedisTemplate(); - 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 redisTemplate = new RedisTemplate(); - 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 redisTemplate = new RedisTemplate(); - 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 redisTemplate = new RedisTemplate(); - 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 { } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/inbound-store-adapter-parser-fail.xml b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/inbound-store-adapter-parser-fail.xml deleted file mode 100644 index 3b451e9802..0000000000 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/inbound-store-adapter-parser-fail.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/list-inbound-adapter.xml b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/list-inbound-adapter.xml index 0d345eddf5..644444be30 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/list-inbound-adapter.xml +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/list-inbound-adapter.xml @@ -73,6 +73,12 @@ + + + + + + diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter-parser.xml b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter-parser.xml deleted file mode 100644 index 713d528b94..0000000000 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter-parser.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter.xml b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter.xml index e3aacb9fe9..958827d2da 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter.xml +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter.xml @@ -37,6 +37,9 @@ collection-type="MAP" extract-payload-elements="false"/> + + diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandlerTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandlerTests.java index f1beda8eb9..b59bef5347 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandlerTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandlerTests.java @@ -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 redisList = - new DefaultRedisList(key, - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + new DefaultRedisList(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 list = new ArrayList(); @@ -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 redisList = - new DefaultRedisList(key, - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + new DefaultRedisList(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 redisList = - new DefaultRedisList(key, - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + new DefaultRedisList(key, this.initTemplate(jcf, new RedisTemplate())); assertEquals(0, redisList.size()); RedisCollectionPopulatingMessageHandler handler = - new RedisCollectionPopulatingMessageHandler(jcf, null); + new RedisCollectionPopulatingMessageHandler(jcf); handler.afterPropertiesSet(); List list = new ArrayList(); @@ -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> redisList = - new DefaultRedisList>(key, - (RedisOperations>) this.initTemplate(jcf, new RedisTemplate>())); + new DefaultRedisList>(key, this.initTemplate(jcf, new RedisTemplate>())); assertEquals(0, redisList.size()); + RedisTemplate> template = this.initTemplate(jcf, new RedisTemplate>()); 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 redisZset = - new DefaultRedisZSet(key, - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + new DefaultRedisZSet(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 redisZset = - new DefaultRedisZSet(key, - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + new DefaultRedisZSet(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 redisZset = - new DefaultRedisZSet(key, - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + new DefaultRedisZSet(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> redisZset = - new DefaultRedisZSet>(key, - (RedisOperations>) this.initTemplate(jcf, new RedisTemplate>())); + new DefaultRedisZSet>(key, this.initTemplate(jcf, new RedisTemplate>())); assertEquals(0, redisZset.size()); + RedisTemplate> template = this.initTemplate(jcf, new RedisTemplate>()); 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 redisZset = - new DefaultRedisZSet(key, - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + new DefaultRedisZSet(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 redisZset = - new DefaultRedisZSet(key, - (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + new DefaultRedisZSet(key, this.initTemplate(jcf, new RedisTemplate())); assertEquals(0, redisZset.size()); + RedisTemplate template = this.initTemplate(jcf, new RedisTemplate()); 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> redisZset = - new DefaultRedisZSet>(key, - (RedisOperations>) this.initTemplate(jcf, new RedisTemplate>())); + new DefaultRedisZSet>(key, this.initTemplate(jcf, new RedisTemplate>())); assertEquals(0, redisZset.size()); + RedisTemplate> template = this.initTemplate(jcf, new RedisTemplate>()); 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 RedisTemplate initTemplate(RedisConnectionFactory rcf, RedisTemplate redisTemplate) { redisTemplate.setConnectionFactory(rcf); redisTemplate.afterPropertiesSet(); return redisTemplate; diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java index 3900b833dc..bcdd913659 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java @@ -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 redisTemplate = new RedisTemplate(); + StringRedisTemplate redisTemplate = new StringRedisTemplate(); redisTemplate.setConnectionFactory(jcf); redisTemplate.afterPropertiesSet(); - BoundListOperations ops = redisTemplate.boundListOps("presidents"); + BoundListOperations ops = redisTemplate.boundListOps("presidents"); ops.rightPush("John Adams"); @@ -79,11 +80,11 @@ public class RedisAvailableTests { protected void prepareZset(JedisConnectionFactory jcf){ - RedisTemplate redisTemplate = new RedisTemplate(); + StringRedisTemplate redisTemplate = new StringRedisTemplate(); redisTemplate.setConnectionFactory(jcf); redisTemplate.afterPropertiesSet(); - BoundZSetOperations ops = redisTemplate.boundZSetOps("presidents"); + BoundZSetOperations ops = redisTemplate.boundZSetOps("presidents"); ops.add("John Adams", 18); diff --git a/src/reference/docbook/redis.xml b/src/reference/docbook/redis.xml index 8d75c6b3ab..6092075dbf 100644 --- a/src/reference/docbook/redis.xml +++ b/src/reference/docbook/redis.xml @@ -247,22 +247,6 @@ rt.setConnectionFactory(redisConnectionFactory);]]> collection-type - enumeration of the Collection types supported by this adapter. Supported Collections are: LIST, SET, ZSET, PROPERTIES, MAP - - key-serializer - reference to an instance of org.springframework.data.redis.serializer.RedisSerializer - to be used while serializing keys - - - value-serializer - reference to an instance of org.springframework.data.redis.serializer.RedisSerializer - to be used while serializing values - - - hash-key-serializer - reference to an instance of org.springframework.data.redis.serializer.RedisSerializer - to be used while serializing hash keys - - - hash-value-serializer - reference to an instance of org.springframework.data.redis.serializer.RedisSerializer - to be used while serializing hash values - connection-factory - reference to an instance of org.springframework.data.redis.connection.RedisConnectionFactory @@ -278,6 +262,28 @@ rt.setConnectionFactory(redisConnectionFactory);]]> You cannot set both redis-template and connection-factory. + + By default, the adapter uses a StringRedisTemplate; this uses + StringRedisSerializers for keys, values, hash keys and hash values. If your + Redis store contains objects that are serialized with other techniques, you must supply a + RedisTemplate configured with appropriate serializers. + For example, if the store is written to using a RedisStore Outbound Adapter that has its + extract-payload-elements set to false, you must provide a + RedisTemplate configured thus: + + + + + + + + +]]> + + This uses String serializers for keys and hash keys and the default JDK Serialization serializers for + values and hash values. + + The example above is relatively simple and static since it has a literal value for the key. Sometimes, you may need to change the value of the key at runtime based on some condition. @@ -367,22 +373,6 @@ rt.setConnectionFactory(redisConnectionFactory);]]> collection-type - enumeration of the Collection types supported by this adapter. Supported Collections are: LIST, SET, ZSET, PROPERTIES, MAP - - key-serializer - reference to an instance of the org.springframework.data.redis.serializer.RedisSerializer - to be used while serializing keys - - - value-serializer - reference to an instance of the org.springframework.data.redis.serializer.RedisSerializer - to be used while serializing values - - - hash-key-serializer - reference to an instance of the org.springframework.data.redis.serializer.RedisSerializer - to be used while serializing hash keys - - - hash-value-serializer - reference to an instance of the org.springframework.data.redis.serializer.RedisSerializer - to be used while serializing hash values - map-key-expression - SpEL expression that returns the name of the key for entry being stored. Only applies if the collection-type is MAP or PROPERTIES and @@ -394,12 +384,25 @@ rt.setConnectionFactory(redisConnectionFactory);]]> redis-template - - reference to an instance of org.springframework.data.redis.core.RedisTemplate - (NOTE: you can not have both redis-template and connection-factory set) + reference to an instance of org.springframework.data.redis.core.RedisTemplate and other attributes that are common across all other inbound adapters (e.g., 'channel'). + + You cannot set both redis-template and connection-factory. + + + By default, the adapter uses a StringRedisTemplate; this uses + StringRedisSerializers for keys, values, hash keys and hash values. However, if + extract-payload-elements is set to false, a RedisTemplate using + StringRedisSerializers for keys and hash keys, and + JdkSerializationRedisSerializers 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 +RedisTemplate rather than relying upon these defaults. + The example above is relatively simple and static since it has a literal values for the key and other attributes. Sometimes you may need to change the values dynamically at runtime based on some condition.