Merge pull request #606 from olegz/INT-2729

INT-2729 Redis Outbound Adapter
This commit is contained in:
Mark Fisher
2012-08-31 17:49:49 -04:00
7 changed files with 501 additions and 332 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.integration.redis.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -24,7 +26,6 @@ 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
*
@@ -61,6 +62,11 @@ public class RedisCollectionsOutboundChannelAdapterParser extends AbstractOutbou
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "collection-type");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload-elements");
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");
if (expressionDef != null){
builder.addConstructorArgValue(expressionDef);
}

View File

@@ -21,14 +21,14 @@ 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.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
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;
@@ -87,6 +87,14 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand
private volatile boolean extractPayloadElements = true;
private volatile RedisSerializer<?> keySerializer;
private volatile RedisSerializer<?> valueSerializer;
private volatile RedisSerializer<?> hashKeySerializer;
private volatile RedisSerializer<?> hashValueSerializer;
/**
* Will construct this instance using fully created and initialized instance of
* provided {@link RedisTemplate}
@@ -119,9 +127,8 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand
/**
* Will construct this instance using the provided {@link RedisConnectionFactory}.
* It will create an instance of {@link RedisTemplate}, initializing it with a
* {@link StringRedisSerializer} for the keySerializer and a {@link JdkSerializationRedisSerializer}
* for each of valueSerializer, hasKeySerializer, and hashValueSerializer.
* It will create an instance of {@link RedisTemplate} with default serializers unless those
* are overridden via this instance's corresponding setters.
*
* The default expression 'headers.{@link RedisHeaders#KEY}'
* will be used.
@@ -133,9 +140,8 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand
/**
* Will construct this instance using the provided {@link RedisConnectionFactory} and {@link #keyExpression}
* It will create an instance of {@link RedisTemplate} initializing it with a
* {@link StringRedisSerializer} for the keySerializer and a {@link JdkSerializationRedisSerializer}
* for each of valueSerializer, hasKeySerializer, and hashValueSerializer.
* It will create an instance of {@link RedisTemplate} with default serializers unless those
* are overridden via this instance's corresponding setters.
*
* If {@link #keyExpression} is null, the default expression 'headers.{@link RedisHeaders#KEY}'
* will be used.
@@ -148,10 +154,6 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
this.redisTemplate = redisTemplate;
if (keyExpression != null) {
@@ -159,6 +161,26 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand
}
}
public void setKeySerializer(RedisSerializer<?> keySerializer) {
Assert.notNull(keySerializer, "'keySerializer' must not be null");
this.keySerializer = keySerializer;
}
public void setValueSerializer(RedisSerializer<?> valueSerializer) {
Assert.notNull(valueSerializer, "'valueSerializer' must not be null");
this.valueSerializer = valueSerializer;
}
public void setHashKeySerializer(RedisSerializer<?> hashKeySerializer) {
Assert.notNull(hashKeySerializer, "'hashKeySerializer' must not be null");
this.hashKeySerializer = hashKeySerializer;
}
public void setHashValueSerializer(RedisSerializer<?> hashValueSerializer) {
Assert.notNull(hashValueSerializer, "'hashValueSerializer' must not be null");
this.hashValueSerializer = hashValueSerializer;
}
/**
* Sets the collection type for this handler as per {@link CollectionType}
*
@@ -212,6 +234,19 @@ 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.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();
}
/**
@@ -244,7 +279,7 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand
protected void handleMessageInternal(Message<?> message) throws Exception {
String key = this.keyExpression.getValue(this.evaluationContext, message, String.class);
Assert.hasText(key, "Can not determine a 'key' for a Redis store. The key can be provided via the " +
Assert.hasText(key, "Cannot determine a 'key' for a Redis store. The key can be provided via the " +
"'key' or 'key-expression' attributes.");
RedisStore store = this.createStoreView(key);
@@ -392,7 +427,7 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand
private Object assertMapEntry(Message<?> message, boolean property) {
Object mapKey = this.mapKeyExpression.getValue(this.evaluationContext, message);
Assert.notNull(mapKey, "Can not determine a map key for the entry. The key is determined by evaluating " +
Assert.notNull(mapKey, "Cannot determine a map key for the entry. The key is determined by evaluating " +
"the 'mapKeyExpression' property.");
Object payload = message.getPayload();
if (property) {

View File

@@ -451,6 +451,54 @@
</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

@@ -0,0 +1,339 @@
/*
* 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 static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
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.support.collections.DefaultRedisList;
import org.springframework.data.redis.support.collections.DefaultRedisMap;
import org.springframework.data.redis.support.collections.DefaultRedisSet;
import org.springframework.data.redis.support.collections.DefaultRedisZSet;
import org.springframework.data.redis.support.collections.RedisList;
import org.springframework.data.redis.support.collections.RedisMap;
import org.springframework.data.redis.support.collections.RedisProperties;
import org.springframework.data.redis.support.collections.RedisSet;
import org.springframework.data.redis.support.collections.RedisZSet;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.redis.outbound.RedisCollectionPopulatingMessageHandler;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.redis.support.RedisHeaders;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
/**
* @author Oleg Zhurakousky
* @since 2.2
*/
public class RedisCollectionsOutboundChannelAdapterIntegrationTests 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>()));
assertEquals(0, redisList.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("listWithKeyAsHeader", MessageChannel.class);
List<String> pepboys = new ArrayList<String>();
pepboys.add("Manny");
pepboys.add("Moe");
pepboys.add("Jack");
Message<List<String>> message = MessageBuilder.withPayload(pepboys).setHeader("redis_key", "pepboys").build();
redisChannel.send(message);
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>()));
assertEquals(0, redisList.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("listWithKeyProvided", MessageChannel.class);
List<String> pepboys = new ArrayList<String>();
pepboys.add("Manny");
pepboys.add("Moe");
pepboys.add("Jack");
Message<List<String>> message = MessageBuilder.withPayload(pepboys).build();
redisChannel.send(message);
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>()));
assertEquals(0, redisZset.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("mapToZset", MessageChannel.class);
Map<String, Integer> presidents = new HashMap<String, Integer>();
presidents.put("John Adams", 18);
presidents.put("Barack Obama", 21);
presidents.put("Thomas Jefferson", 19);
presidents.put("John Quincy Adams", 19);
presidents.put("Zachary Taylor", 19);
Message<Map<String, Integer>> message = MessageBuilder.withPayload(presidents).build();
redisChannel.send(message);
assertEquals(5, redisZset.size());
assertEquals(1, redisZset.rangeByScore(18, 18).size());
assertEquals(4, redisZset.rangeByScore(18, 19).size());
RedisCollectionPopulatingMessageHandler handler = context.getBean("mapToZset.handler",
RedisCollectionPopulatingMessageHandler.class);
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>>()));
assertEquals(0, redisMap.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("mapToMapA", MessageChannel.class);
Map<String, String> pepboys = new HashMap<String, String>();
pepboys.put("1", "Manny");
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"));
assertEquals("Moe", redisMap.get("2"));
assertEquals("Jack", redisMap.get("3"));
RedisCollectionPopulatingMessageHandler handler = context.getBean("mapToMapA.handler",
RedisCollectionPopulatingMessageHandler.class);
assertEquals("pepboys", TestUtils.getPropertyValue(handler, "keyExpression", LiteralExpression.class).getExpressionString());
assertEquals("'foo'", TestUtils.getPropertyValue(handler, "mapKeyExpression", SpelExpression.class).getExpressionString());
}
@SuppressWarnings("unchecked")
@Test(expected=MessageHandlingException.class)// map key is not proivided
@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>>>()));
assertEquals(0, redisMap.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("mapToMapB", MessageChannel.class);
Map<String, String> pepboys = new HashMap<String, String>();
pepboys.put("1", "Manny");
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
@RedisAvailable
public void testMapToMapNoKey(){
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>>>()));
assertEquals(0, redisMap.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("mapToMapB", MessageChannel.class);
Map<String, String> pepboys = new HashMap<String, String>();
pepboys.put("1", "Manny");
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();
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>>>()));
assertEquals(0, redisMap.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("mapToMapB", MessageChannel.class);
Map<String, String> pepboys = new HashMap<String, String>();
pepboys.put("1", "Manny");
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);
Map<String, String> pepboyz = redisMap.get("foo");
assertEquals("Manny", pepboyz.get("1"));
assertEquals("Moe", pepboyz.get("2"));
assertEquals("Jack", pepboyz.get("3"));
}
@SuppressWarnings("unchecked")
@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());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("set", MessageChannel.class);
Set<String> pepboys = new HashSet<String>();
pepboys.add("Manny");
pepboys.add("Moe");
pepboys.add("Jack");
Message<Set<String>> message = MessageBuilder.withPayload(pepboys).setHeader("redis_key", "pepboys").build();
redisChannel.send(message);
assertEquals(3, redisList.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());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("setNotParsed", MessageChannel.class);
Set<String> pepboys = new HashSet<String>();
pepboys.add("Manny");
pepboys.add("Moe");
pepboys.add("Jack");
Message<Set<String>> message = MessageBuilder.withPayload(pepboys).setHeader("redis_key", "pepboys").build();
redisChannel.send(message);
assertEquals(1, redisList.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());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("pojoIntoSet", MessageChannel.class);
String pepboy = "Manny";
Message<String> message = MessageBuilder.withPayload(pepboy).setHeader("redis_key", "pepboys").build();
redisChannel.send(message);
assertEquals(1, redisList.size());
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testProperty(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisProperties redisProperties =
new RedisProperties("pepboys",
(RedisOperations<String, ?>) this.initTemplate(jcf, new RedisTemplate<String, Properties>()));
assertEquals(0, redisProperties.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("property", MessageChannel.class);
Properties pepboys = new Properties();
pepboys.put("1", "Manny");
pepboys.put("2", "Moe");
pepboys.put("3", "Jack");
Message<Properties> message = MessageBuilder.withPayload(pepboys).build();
redisChannel.send(message);
assertEquals("Manny", redisProperties.get("1"));
assertEquals("Moe", redisProperties.get("2"));
assertEquals("Jack", redisProperties.get("3"));
}
private RedisTemplate<?,?> initTemplate(RedisConnectionFactory rcf, RedisTemplate<?, ?> redisTemplate){
redisTemplate.setConnectionFactory(rcf);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}

View File

@@ -14,329 +14,31 @@
* limitations under the License.
*/
package org.springframework.integration.redis.config;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
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.serializer.JdkSerializationRedisSerializer;
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;
import org.springframework.data.redis.support.collections.DefaultRedisZSet;
import org.springframework.data.redis.support.collections.RedisList;
import org.springframework.data.redis.support.collections.RedisMap;
import org.springframework.data.redis.support.collections.RedisProperties;
import org.springframework.data.redis.support.collections.RedisSet;
import org.springframework.data.redis.support.collections.RedisZSet;
import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.redis.outbound.RedisCollectionPopulatingMessageHandler;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.redis.support.RedisHeaders;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
/**
*
* @author Oleg Zhurakousky
* @since 2.2
*/
public class RedisCollectionsOutboundChannelAdapterParserTests extends RedisAvailableTests {
public class RedisCollectionsOutboundChannelAdapterParserTests {
@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>()));
assertEquals(0, redisList.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("listWithKeyAsHeader", MessageChannel.class);
List<String> pepboys = new ArrayList<String>();
pepboys.add("Manny");
pepboys.add("Moe");
pepboys.add("Jack");
Message<List<String>> message = MessageBuilder.withPayload(pepboys).setHeader("redis_key", "pepboys").build();
redisChannel.send(message);
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>()));
assertEquals(0, redisList.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("listWithKeyProvided", MessageChannel.class);
List<String> pepboys = new ArrayList<String>();
pepboys.add("Manny");
pepboys.add("Moe");
pepboys.add("Jack");
Message<List<String>> message = MessageBuilder.withPayload(pepboys).build();
redisChannel.send(message);
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>()));
assertEquals(0, redisZset.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("mapToZset", MessageChannel.class);
Map<String, Integer> presidents = new HashMap<String, Integer>();
presidents.put("John Adams", 18);
presidents.put("Barack Obama", 21);
presidents.put("Thomas Jefferson", 19);
presidents.put("John Quincy Adams", 19);
presidents.put("Zachary Taylor", 19);
Message<Map<String, Integer>> message = MessageBuilder.withPayload(presidents).build();
redisChannel.send(message);
assertEquals(5, redisZset.size());
assertEquals(1, redisZset.rangeByScore(18, 18).size());
assertEquals(4, redisZset.rangeByScore(18, 19).size());
RedisCollectionPopulatingMessageHandler handler = context.getBean("mapToZset.handler",
RedisCollectionPopulatingMessageHandler.class);
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>>()));
assertEquals(0, redisMap.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("mapToMapA", MessageChannel.class);
Map<String, String> pepboys = new HashMap<String, String>();
pepboys.put("1", "Manny");
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"));
assertEquals("Moe", redisMap.get("2"));
assertEquals("Jack", redisMap.get("3"));
RedisCollectionPopulatingMessageHandler handler = context.getBean("mapToMapA.handler",
RedisCollectionPopulatingMessageHandler.class);
assertEquals("pepboys", TestUtils.getPropertyValue(handler, "keyExpression", LiteralExpression.class).getExpressionString());
assertEquals("'foo'", TestUtils.getPropertyValue(handler, "mapKeyExpression", SpelExpression.class).getExpressionString());
}
@SuppressWarnings("unchecked")
@Test(expected=MessageHandlingException.class)// map key is not proivided
@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>>>()));
assertEquals(0, redisMap.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("mapToMapB", MessageChannel.class);
Map<String, String> pepboys = new HashMap<String, String>();
pepboys.put("1", "Manny");
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
@RedisAvailable
public void testMapToMapNoKey(){
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>>>()));
assertEquals(0, redisMap.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("mapToMapB", MessageChannel.class);
Map<String, String> pepboys = new HashMap<String, String>();
pepboys.put("1", "Manny");
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();
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>>>()));
assertEquals(0, redisMap.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("mapToMapB", MessageChannel.class);
Map<String, String> pepboys = new HashMap<String, String>();
pepboys.put("1", "Manny");
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);
Map<String, String> pepboyz = redisMap.get("foo");
assertEquals("Manny", pepboyz.get("1"));
assertEquals("Moe", pepboyz.get("2"));
assertEquals("Jack", pepboyz.get("3"));
}
@SuppressWarnings("unchecked")
@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());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("set", MessageChannel.class);
Set<String> pepboys = new HashSet<String>();
pepboys.add("Manny");
pepboys.add("Moe");
pepboys.add("Jack");
Message<Set<String>> message = MessageBuilder.withPayload(pepboys).setHeader("redis_key", "pepboys").build();
redisChannel.send(message);
assertEquals(3, redisList.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());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("setNotParsed", MessageChannel.class);
Set<String> pepboys = new HashSet<String>();
pepboys.add("Manny");
pepboys.add("Moe");
pepboys.add("Jack");
Message<Set<String>> message = MessageBuilder.withPayload(pepboys).setHeader("redis_key", "pepboys").build();
redisChannel.send(message);
assertEquals(1, redisList.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());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("pojoIntoSet", MessageChannel.class);
String pepboy = "Manny";
Message<String> message = MessageBuilder.withPayload(pepboy).setHeader("redis_key", "pepboys").build();
redisChannel.send(message);
assertEquals(1, redisList.size());
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
public void testProperty(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisProperties redisProperties =
new RedisProperties("pepboys",
(RedisOperations<String, ?>) this.initTemplate(jcf, new RedisTemplate<String, Properties>()));
assertEquals(0, redisProperties.size());
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("store-outbound-adapter.xml", this.getClass());
MessageChannel redisChannel = context.getBean("property", MessageChannel.class);
Properties pepboys = new Properties();
pepboys.put("1", "Manny");
pepboys.put("2", "Moe");
pepboys.put("3", "Jack");
Message<Properties> message = MessageBuilder.withPayload(pepboys).build();
redisChannel.send(message);
assertEquals("Manny", redisProperties.get("1"));
assertEquals("Moe", redisProperties.get("2"));
assertEquals("Jack", redisProperties.get("3"));
}
private RedisTemplate<?,?> initTemplate(RedisConnectionFactory rcf, RedisTemplate<?, ?> redisTemplate){
redisTemplate.setConnectionFactory(rcf);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer());
return redisTemplate;
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(TestUtils.getPropertyValue(handler, "keySerializer"), context.getBean("keySerializer"));
assertEquals(TestUtils.getPropertyValue(handler, "valueSerializer"), context.getBean("valueSerializer"));
assertEquals(TestUtils.getPropertyValue(handler, "hashKeySerializer"), context.getBean("hashKeySerializer"));
assertEquals(TestUtils.getPropertyValue(handler, "hashValueSerializer"), context.getBean("hashValueSerializer"));
assertEquals("pepboys", ((LiteralExpression)TestUtils.getPropertyValue(handler, "keyExpression")).getExpressionString());
assertEquals("PROPERTIES", ((CollectionType)TestUtils.getPropertyValue(handler, "collectionType")).toString());
}
}

View File

@@ -0,0 +1,32 @@
<?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.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="port" value="7379"/>
</bean>
</beans>

View File

@@ -12,13 +12,12 @@ import java.util.Map;
import java.util.Set;
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.ZSetOperations.TypedTuple;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.support.collections.DefaultRedisList;
import org.springframework.data.redis.support.collections.DefaultRedisZSet;
import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType;
@@ -48,6 +47,7 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
handler.afterPropertiesSet();
List<String> list = new ArrayList<String>();
list.add("Manny");
@@ -76,6 +76,7 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, null);
handler.afterPropertiesSet();
List<String> list = new ArrayList<String>();
@@ -105,6 +106,7 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, null);
handler.afterPropertiesSet();
List<String> list = new ArrayList<String>();
list.add("Manny");
@@ -128,7 +130,9 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
handler.setExtractPayloadElements(false);
handler.afterPropertiesSet();
List<String> list = new ArrayList<String>();
list.add("Manny");
@@ -160,6 +164,7 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
handler.setCollectionType(CollectionType.ZSET);
handler.afterPropertiesSet();
List<String> list = new ArrayList<String>();
list.add("Manny");
@@ -189,10 +194,10 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
RedisCollectionPopulatingMessageHandler handler =
new RedisCollectionPopulatingMessageHandler(jcf, null);
handler.afterPropertiesSet();
handler.setCollectionType(CollectionType.ZSET);
handler.setExtractPayloadElements(false);
handler.afterPropertiesSet();
List<String> list = new ArrayList<String>();
list.add("Manny");
@@ -225,6 +230,7 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
handler.setCollectionType(CollectionType.ZSET);
handler.afterPropertiesSet();
Map<String, Double> presidents = new HashMap<String, Double>();
presidents.put("John Adams", 18D);
@@ -268,6 +274,7 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key));
handler.setCollectionType(CollectionType.ZSET);
handler.afterPropertiesSet();
Map<President, Double> presidents = new HashMap<President, Double>();
presidents.put(new President("John Adams"), 18D);
@@ -312,6 +319,7 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
handler.setCollectionType(CollectionType.ZSET);
handler.setExtractPayloadElements(false);
handler.afterPropertiesSet();
Map<President, Double> presidents = new HashMap<President, Double>();
presidents.put(new President("John Adams"), 18D);
@@ -396,8 +404,7 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable
private RedisTemplate<?,?> initTemplate(RedisConnectionFactory rcf, RedisTemplate<?, ?> redisTemplate) {
redisTemplate.setConnectionFactory(rcf);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}