INT-3085: Use RedisProperties for RMDStore

JIRA: https://jira.springsource.org/browse/INT-3085
This commit is contained in:
Artem Bilan
2013-11-19 18:44:43 +02:00
committed by Gary Russell
parent afb369c0eb
commit 225c0b4c9f
3 changed files with 66 additions and 31 deletions

View File

@@ -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<String, String> redisTemplate = new StringRedisTemplate(connectionFactory);
BoundHashOperations<String, String, String> 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<String, ?> operations) {
this(operations, KEY);
}
/**
* Initializes the {@link RedisProperties} by provided {@link RedisConnectionFactory} and key.
*/
public RedisMetadataStore(RedisOperations<String, ?> operations, String key) {
Assert.notNull(operations, "'operations' must not be null.");
Assert.hasText(key, "'key' must not be empty.");
BoundHashOperations<String, String, String> 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<String, String> 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<String, String> 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);
}
}

View File

@@ -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<String, String> ops = redisTemplate.boundValueOps("RedisMetadataStoreTests-Spring");
BoundHashOperations<String,Object,Object> 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);

View File

@@ -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;