From 225c0b4c9f4e9962d2cc9943b1afcd5468818702 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 19 Nov 2013 18:44:43 +0200 Subject: [PATCH] INT-3085: Use `RedisProperties` for `RMDStore` JIRA: https://jira.springsource.org/browse/INT-3085 --- .../redis/metadata/RedisMetadataStore.java | 65 ++++++++++++++----- .../metadata/RedisMetadataStoreTests.java | 26 ++++---- ...hReceivingMessageSourceWithRedisTests.java | 6 +- 3 files changed, 66 insertions(+), 31 deletions(-) diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/metadata/RedisMetadataStore.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/metadata/RedisMetadataStore.java index e8aa0609c7..9e45936094 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/metadata/RedisMetadataStore.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/metadata/RedisMetadataStore.java @@ -14,9 +14,10 @@ package org.springframework.integration.redis.metadata; import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.core.BoundValueOperations; -import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.BoundHashOperations; +import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.support.collections.RedisProperties; import org.springframework.integration.metadata.MetadataStore; import org.springframework.util.Assert; @@ -25,21 +26,58 @@ import org.springframework.util.Assert; * to achieve meta-data persistence across application restarts. * * @author Gunnar Hillert + * @author Artem Bilan * @since 3.0 */ public class RedisMetadataStore implements MetadataStore { - private final StringRedisTemplate redisTemplate; + public static final String KEY = "MetaData"; + + private final RedisProperties properties; /** - * Initializes the {@link RedisTemplate}. - * A {@link StringRedisTemplate} is used with default properties. - * - * @param connectionFactory Must not be null + * Specifies the {@link RedisProperties} backend for this {@link MetadataStore}. + */ + public RedisMetadataStore(RedisProperties properties) { + Assert.notNull(properties, "'properties' must not be null."); + this.properties = properties; + } + + /** + * Initializes the {@link RedisProperties} by provided {@link RedisConnectionFactory} + * and default hash key - {@link #KEY}. */ public RedisMetadataStore(RedisConnectionFactory connectionFactory) { + this(connectionFactory, KEY); + } + + /** + * Initializes the {@link RedisProperties} by provided {@link RedisConnectionFactory} and key. + */ + public RedisMetadataStore(RedisConnectionFactory connectionFactory, String key) { Assert.notNull(connectionFactory, "'connectionFactory' must not be null."); - this.redisTemplate = new StringRedisTemplate(connectionFactory); + Assert.hasText(key, "'key' must not be empty."); + RedisOperations redisTemplate = new StringRedisTemplate(connectionFactory); + BoundHashOperations hashOperations = redisTemplate.boundHashOps(key); + this.properties = new RedisProperties(hashOperations); + } + + /** + * Initializes the {@link RedisProperties} by provided {@link RedisConnectionFactory} + * and default hash key - {@link #KEY}. + */ + public RedisMetadataStore(RedisOperations operations) { + this(operations, KEY); + } + + /** + * Initializes the {@link RedisProperties} by provided {@link RedisConnectionFactory} and key. + */ + public RedisMetadataStore(RedisOperations operations, String key) { + Assert.notNull(operations, "'operations' must not be null."); + Assert.hasText(key, "'key' must not be empty."); + BoundHashOperations hashOperations = operations.boundHashOps(key); + this.properties = new RedisProperties(hashOperations); } /** @@ -51,8 +89,7 @@ public class RedisMetadataStore implements MetadataStore { public void put(String key, String value) { Assert.notNull(key, "'key' must not be null."); Assert.notNull(value, "'value' must not be null."); - BoundValueOperations ops = this.redisTemplate.boundValueOps(key); - ops.set(value); + this.properties.put(key, value); } /** @@ -62,16 +99,14 @@ public class RedisMetadataStore implements MetadataStore { */ public String get(String key) { Assert.notNull(key, "'key' must not be null."); - BoundValueOperations ops = this.redisTemplate.boundValueOps(key); - return ops.get(); + return (String) this.properties.get(key); } @Override + public String remove(String key) { Assert.notNull(key, "'key' must not be null."); - String value = this.get(key); - this.redisTemplate.delete(key); - return value; + return (String) this.properties.remove(key); } } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java index c9a48610c8..f08856f77e 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java @@ -22,7 +22,7 @@ import static org.junit.Assert.fail; import org.junit.Test; import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.core.BoundValueOperations; +import org.springframework.data.redis.core.BoundHashOperations; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.integration.redis.rules.RedisAvailable; import org.springframework.integration.redis.rules.RedisAvailableTests; @@ -37,7 +37,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @Test @RedisAvailable - public void testGetNonExistingKeyValue(){ + public void testGetNonExistingKeyValue() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); String retrievedValue = metadataStore.get("does-not-exist"); @@ -46,20 +46,20 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @Test @RedisAvailable - public void testPersistKeyValue(){ + public void testPersistKeyValue() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); - RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); + RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "foo"); metadataStore.put("RedisMetadataStoreTests-Spring", "Integration"); StringRedisTemplate redisTemplate = new StringRedisTemplate(jcf); - BoundValueOperations ops = redisTemplate.boundValueOps("RedisMetadataStoreTests-Spring"); + BoundHashOperations ops = redisTemplate.boundHashOps("foo"); - assertEquals("Integration", ops.get()); + assertEquals("Integration", ops.get("RedisMetadataStoreTests-Spring")); } @Test @RedisAvailable - public void testGetValueFromMetadataStore(){ + public void testGetValueFromMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); @@ -71,7 +71,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @Test @RedisAvailable - public void testPersistEmptyStringToMetadataStore(){ + public void testPersistEmptyStringToMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); @@ -83,7 +83,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @Test @RedisAvailable - public void testPersistNullStringToMetadataStore(){ + public void testPersistNullStringToMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); @@ -102,7 +102,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @Test @RedisAvailable - public void testPersistWithEmptyKeyToMetadataStore(){ + public void testPersistWithEmptyKeyToMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); metadataStore.put("", "PersistWithEmptyKey"); @@ -113,7 +113,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @Test @RedisAvailable - public void testPersistWithNullKeyToMetadataStore(){ + public void testPersistWithNullKeyToMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); @@ -130,7 +130,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @Test @RedisAvailable - public void testGetValueWithNullKeyFromMetadataStore(){ + public void testGetValueWithNullKeyFromMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); @@ -147,7 +147,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @Test @RedisAvailable - public void testRemoveFromMetadataStore(){ + public void testRemoveFromMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests.java b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests.java index c29762180f..acb9d6e7b6 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests.java +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests.java @@ -38,10 +38,10 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.integration.core.PollableChannel; import org.springframework.integration.endpoint.SourcePollingChannelAdapter; +import org.springframework.integration.metadata.MetadataStore; +import org.springframework.integration.redis.metadata.RedisMetadataStore; import org.springframework.integration.redis.rules.RedisAvailable; import org.springframework.integration.redis.rules.RedisAvailableTests; -import org.springframework.integration.redis.metadata.RedisMetadataStore; -import org.springframework.integration.metadata.MetadataStore; import org.springframework.integration.test.util.TestUtils; import org.springframework.social.twitter.api.SearchMetadata; import org.springframework.social.twitter.api.SearchOperations; @@ -60,7 +60,7 @@ public class SearchReceivingMessageSourceWithRedisTests extends RedisAvailableTe private SourcePollingChannelAdapter twitterSearchAdapter; - private AbstractTwitterMessageSource twitterMessageSource; + private AbstractTwitterMessageSource twitterMessageSource; private MetadataStore metadataStore;