+ improved configuration of jredis and jedis instances

+ removed unused resources
This commit is contained in:
Costin Leau
2010-12-11 21:07:48 +02:00
parent 3cf0f002a9
commit 68e490661c
4 changed files with 104 additions and 78 deletions

View File

@@ -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 <a href="http://github.com/xetorthio/jedis">Jedis</a> 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 <code>JedisConnectionFactory</code> instance.
*/
public JedisConnectionFactory() {
this(getDefaultHostName());
}
/**
* Constructs a new <code>JedisConnectionFactory</code> instance.
*
* @param hostName
*/
public JedisConnectionFactory(String hostName) {
Assert.hasText(hostName);
shardInfo = new JedisShardInfo(hostName);
}
/**
* Constructs a new <code>JedisConnectionFactory</code> instance.
*
* @param hostName
* @param port
*/
public JedisConnectionFactory(String hostName, int port) {
shardInfo = new JedisShardInfo(hostName, port);
}
/**
* Constructs a new <code>JedisConnectionFactory</code> 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.
*

View File

@@ -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 <code>JredisConnectionFactory</code> 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 <code>JredisConnectionFactory</code> 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 <code>JredisConnectionFactory</code> 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.
*

View File

@@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<description>Example configuration to get you started.</description>
<bean id="service" class="org.springframework.data.ExampleService" />
</beans>

View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:/META-INF/spring/app-context.xml"/>
</beans>