From 68e490661cf612421ac1ecbe301a77d146871111 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Sat, 11 Dec 2010 21:07:48 +0200 Subject: [PATCH] + improved configuration of jredis and jedis instances + removed unused resources --- .../jedis/JedisConnectionFactory.java | 79 ++++++++++------- .../jredis/JredisConnectionFactory.java | 85 ++++++++++++------- .../resources/META-INF/spring/app-context.xml | 10 --- .../ExampleConfigurationTests-context.xml | 8 -- 4 files changed, 104 insertions(+), 78 deletions(-) delete mode 100644 spring-data-redis/src/main/resources/META-INF/spring/app-context.xml delete mode 100644 spring-data-redis/src/test/resources/org/springframework/datastore/ExampleConfigurationTests-context.xml diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnectionFactory.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnectionFactory.java index 186229230..06b5c4df7 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnectionFactory.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnectionFactory.java @@ -25,12 +25,12 @@ import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.data.keyvalue.redis.connection.RedisConnection; import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory; -import org.springframework.util.Assert; import org.springframework.util.StringUtils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisShardInfo; +import redis.clients.jedis.Protocol; /** * Connection factory using creating Jedis based connections. @@ -42,8 +42,10 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean, private final static Log log = LogFactory.getLog(JedisConnectionFactory.class); private JedisShardInfo shardInfo; + private String hostName = "localhost"; + private int port = Protocol.DEFAULT_PORT; + private int timeout = Protocol.DEFAULT_TIMEOUT; private String password; - private int timeout; private boolean usePool = true; @@ -53,31 +55,11 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean, * Constructs a new JedisConnectionFactory instance. */ public JedisConnectionFactory() { - this(getDefaultHostName()); - } - - /** - * Constructs a new JedisConnectionFactory instance. - * - * @param hostName - */ - public JedisConnectionFactory(String hostName) { - Assert.hasText(hostName); - shardInfo = new JedisShardInfo(hostName); - } - - /** - * Constructs a new JedisConnectionFactory instance. - * - * @param hostName - * @param port - */ - public JedisConnectionFactory(String hostName, int port) { - shardInfo = new JedisShardInfo(hostName, port); } /** * Constructs a new JedisConnectionFactory instance. + * Will override the other connection parameters passed to the factory. * * @param shardInfo */ @@ -103,12 +85,16 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean, } public void afterPropertiesSet() { - if (StringUtils.hasLength(password)) { - shardInfo.setPassword(password); - } + if (shardInfo == null) { + shardInfo = new JedisShardInfo(hostName, port); - if (timeout > 0) { - shardInfo.setTimeout(timeout); + if (StringUtils.hasLength(password)) { + shardInfo.setPassword(password); + } + + if (timeout > 0) { + shardInfo.setTimeout(timeout); + } } if (usePool) { @@ -137,8 +123,22 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean, return JedisUtils.convertJedisAccessException(ex); } - private static String getDefaultHostName() { - return "localhost"; + /** + * Returns the Redis hostName. + * + * @return Returns the hostName + */ + public String getHostName() { + return hostName; + } + + /** + * Sets the Redis hostName. + * + * @param hostName The hostName to set. + */ + public void setHostName(String host) { + this.hostName = host; } /** @@ -159,6 +159,25 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean, this.password = password; } + /** + * Returns the port used to connect to the Redis instance. + * + * @return Redis port. + */ + public int getPort() { + return port; + + } + + /** + * Sets the port used to connect to the Redis instance. + * + * @param port Redis port + */ + public void setPort(int port) { + this.port = port; + } + /** * Returns the shardInfo. * diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnectionFactory.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnectionFactory.java index 9820fa567..79e3ec205 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnectionFactory.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnectionFactory.java @@ -38,7 +38,9 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean private ConnectionSpec connectionSpec; - private String password; + private String hostName = "localhost"; + private int port = DEFAULT_REDIS_PORT; + private String password = null; private int timeout; private boolean usePool = true; @@ -63,31 +65,9 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean /** * Constructs a new JredisConnectionFactory instance. + * Will override the other connection parameters passed to the factory. * - * @param hostName - */ - public JredisConnectionFactory(String hostName) { - this(hostName, DEFAULT_REDIS_PORT); - } - - - /** - * Constructs a new JredisConnectionFactory instance. - * - * @param hostName - * @param port - */ - public JredisConnectionFactory(String hostName, int port) { - Assert.hasText(hostName); - ConnectionSpec newSpec = DefaultConnectionSpec.newSpec(hostName, port, DEFAULT_REDIS_DB, DEFAULT_REDIS_PASSWORD); - newSpec.setConnectionFlag(Connection.Flag.RELIABLE, false); - this.connectionSpec = newSpec; - } - - /** - * Constructs a new JredisConnectionFactory instance. - * - * @param connectionSpec + * @param connectionSpec already configured connection. */ public JredisConnectionFactory(ConnectionSpec connectionSpec) { this.connectionSpec = connectionSpec; @@ -95,12 +75,19 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean @Override public void afterPropertiesSet() { - if (StringUtils.hasLength(password)) { - connectionSpec.setCredentials(password); - } + if (connectionSpec == null) { + Assert.hasText(hostName); + connectionSpec = DefaultConnectionSpec.newSpec(hostName, DEFAULT_REDIS_PORT, DEFAULT_REDIS_DB, + DEFAULT_REDIS_PASSWORD); + connectionSpec.setConnectionFlag(Connection.Flag.RELIABLE, false); - if (timeout > 0) { - connectionSpec.setSocketProperty(Property.SO_TIMEOUT, timeout); + if (StringUtils.hasLength(password)) { + connectionSpec.setCredentials(password); + } + + if (timeout > 0) { + connectionSpec.setSocketProperty(Property.SO_TIMEOUT, timeout); + } } if (usePool) { @@ -130,6 +117,44 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean return null; } + + /** + * Returns the Redis host name of this factory. + * + * @return Returns the hostName + */ + public String getHostName() { + return hostName; + } + + /** + * Sets the Redis host name for this factory. + * + * @param hostName The hostName to set. + */ + public void setHostName(String hostName) { + this.hostName = hostName; + } + + + /** + * Returns the Redis port. + * + * @return Returns the port + */ + public int getPort() { + return port; + } + + /** + * Sets the Redis port. + * + * @param port The port to set. + */ + public void setPort(int port) { + this.port = port; + } + /** * Returns the password used for authenticating with the Redis server. * diff --git a/spring-data-redis/src/main/resources/META-INF/spring/app-context.xml b/spring-data-redis/src/main/resources/META-INF/spring/app-context.xml deleted file mode 100644 index fefa52446..000000000 --- a/spring-data-redis/src/main/resources/META-INF/spring/app-context.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - Example configuration to get you started. - - - - diff --git a/spring-data-redis/src/test/resources/org/springframework/datastore/ExampleConfigurationTests-context.xml b/spring-data-redis/src/test/resources/org/springframework/datastore/ExampleConfigurationTests-context.xml deleted file mode 100644 index 4717a9b6b..000000000 --- a/spring-data-redis/src/test/resources/org/springframework/datastore/ExampleConfigurationTests-context.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - -