DATAREDIS-374 - Adapt JedisConnectionFactory for changes in Jedis 2.7.
We now support both Jedis 2.6.2 (current release) as well as the upcoming 2.7 version. Therefore we call the in 2.7 renamed `getTimeout` / `setTimeout` Methods on `JedisShardInfo` via reflection to read/set `soTimeout`. Since there’s no change in r.c.j.JedisFactory that would allow to individually configure connection and socket timeout, we use the socketTimeout of `JedisShardInfo`. This will cause `r.c.j.JedisFactory` to use the socket timeout for the connection as well, which is the same behavior as in 2.6.2. Tested against Jedis 2.7 branch with Redis 3.0.0.RC4. Original Pull Request: #127
This commit is contained in:
committed by
Christoph Strobl
parent
8bf624b1bb
commit
c47292d173
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
* Copyright 2011-2015 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.
|
||||
@@ -13,9 +13,9 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.redis.connection.jedis;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
@@ -37,6 +37,7 @@ import org.springframework.data.redis.connection.RedisSentinelConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisSentinelConnection;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
@@ -60,6 +61,27 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
|
||||
private static final ExceptionTranslationStrategy EXCEPTION_TRANSLATION = new PassThroughExceptionTranslationStrategy(
|
||||
JedisConverters.exceptionConverter());
|
||||
|
||||
private static final Method SET_TIMEOUT_METHOD;
|
||||
private static final Method GET_TIMEOUT_METHOD;
|
||||
|
||||
static {
|
||||
|
||||
// We need to configure Jedis socket timeout via reflection since the method-name was changed between releases.
|
||||
Method setTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "setTimeout", int.class);
|
||||
if (setTimeoutMethodCandidate == null) {
|
||||
// Jedis V 2.7.x changed the setTimeout method to setSoTimeout
|
||||
setTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "setSoTimeout", int.class);
|
||||
}
|
||||
SET_TIMEOUT_METHOD = setTimeoutMethodCandidate;
|
||||
|
||||
Method getTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "getTimeout");
|
||||
if (getTimeoutMethodCandidate == null) {
|
||||
getTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "getSoTimeout");
|
||||
}
|
||||
|
||||
GET_TIMEOUT_METHOD = getTimeoutMethodCandidate;
|
||||
}
|
||||
|
||||
private JedisShardInfo shardInfo;
|
||||
private String hostName = "localhost";
|
||||
private int port = Protocol.DEFAULT_PORT;
|
||||
@@ -165,7 +187,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
|
||||
}
|
||||
|
||||
if (timeout > 0) {
|
||||
shardInfo.setTimeout(timeout);
|
||||
setTimeoutOn(shardInfo, timeout);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,8 +213,8 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
|
||||
*/
|
||||
protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config) {
|
||||
return new JedisSentinelPool(config.getMaster().getName(), convertToJedisSentinelSet(config.getSentinels()),
|
||||
getPoolConfig() != null ? getPoolConfig() : new JedisPoolConfig(), getShardInfo().getTimeout(), getShardInfo()
|
||||
.getPassword());
|
||||
getPoolConfig() != null ? getPoolConfig() : new JedisPoolConfig(), getTimeoutFrom(getShardInfo()),
|
||||
getShardInfo().getPassword());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,8 +224,8 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
|
||||
* @since 1.4
|
||||
*/
|
||||
protected Pool<Jedis> createRedisPool() {
|
||||
return new JedisPool(getPoolConfig(), getShardInfo().getHost(), getShardInfo().getPort(), getShardInfo()
|
||||
.getTimeout(), getShardInfo().getPassword());
|
||||
return new JedisPool(getPoolConfig(), getShardInfo().getHost(), getShardInfo().getPort(),
|
||||
getTimeoutFrom(getShardInfo()), getShardInfo().getPassword());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -424,7 +446,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
|
||||
if (!isRedisSentinelAware()) {
|
||||
throw new InvalidDataAccessResourceUsageException("No Sentinels configured");
|
||||
}
|
||||
|
||||
|
||||
return new JedisSentinelConnection(getActiveSentinel());
|
||||
}
|
||||
|
||||
@@ -456,4 +478,12 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
|
||||
return convertedNodes;
|
||||
}
|
||||
|
||||
private void setTimeoutOn(JedisShardInfo shardInfo, int timeout) {
|
||||
ReflectionUtils.invokeMethod(SET_TIMEOUT_METHOD, shardInfo, timeout);
|
||||
}
|
||||
|
||||
private int getTimeoutFrom(JedisShardInfo shardInfo) {
|
||||
return (Integer) ReflectionUtils.invokeMethod(GET_TIMEOUT_METHOD, shardInfo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user