Skip to content

Commit c47292d

Browse files
Thomas Darimontchristophstrobl
authored andcommitted
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: spring-projects#127
1 parent 8bf624b commit c47292d

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnectionFactory.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,9 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.data.redis.connection.jedis;
1817

18+
import java.lang.reflect.Method;
1919
import java.util.Collection;
2020
import java.util.Collections;
2121
import java.util.LinkedHashSet;
@@ -37,6 +37,7 @@
3737
import org.springframework.data.redis.connection.RedisSentinelConnection;
3838
import org.springframework.util.Assert;
3939
import org.springframework.util.CollectionUtils;
40+
import org.springframework.util.ReflectionUtils;
4041
import org.springframework.util.StringUtils;
4142

4243
import redis.clients.jedis.Jedis;
@@ -60,6 +61,27 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
6061
private static final ExceptionTranslationStrategy EXCEPTION_TRANSLATION = new PassThroughExceptionTranslationStrategy(
6162
JedisConverters.exceptionConverter());
6263

64+
private static final Method SET_TIMEOUT_METHOD;
65+
private static final Method GET_TIMEOUT_METHOD;
66+
67+
static {
68+
69+
// We need to configure Jedis socket timeout via reflection since the method-name was changed between releases.
70+
Method setTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "setTimeout", int.class);
71+
if (setTimeoutMethodCandidate == null) {
72+
// Jedis V 2.7.x changed the setTimeout method to setSoTimeout
73+
setTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "setSoTimeout", int.class);
74+
}
75+
SET_TIMEOUT_METHOD = setTimeoutMethodCandidate;
76+
77+
Method getTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "getTimeout");
78+
if (getTimeoutMethodCandidate == null) {
79+
getTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "getSoTimeout");
80+
}
81+
82+
GET_TIMEOUT_METHOD = getTimeoutMethodCandidate;
83+
}
84+
6385
private JedisShardInfo shardInfo;
6486
private String hostName = "localhost";
6587
private int port = Protocol.DEFAULT_PORT;
@@ -165,7 +187,7 @@ public void afterPropertiesSet() {
165187
}
166188

167189
if (timeout > 0) {
168-
shardInfo.setTimeout(timeout);
190+
setTimeoutOn(shardInfo, timeout);
169191
}
170192
}
171193

@@ -191,8 +213,8 @@ private Pool<Jedis> createPool() {
191213
*/
192214
protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config) {
193215
return new JedisSentinelPool(config.getMaster().getName(), convertToJedisSentinelSet(config.getSentinels()),
194-
getPoolConfig() != null ? getPoolConfig() : new JedisPoolConfig(), getShardInfo().getTimeout(), getShardInfo()
195-
.getPassword());
216+
getPoolConfig() != null ? getPoolConfig() : new JedisPoolConfig(), getTimeoutFrom(getShardInfo()),
217+
getShardInfo().getPassword());
196218
}
197219

198220
/**
@@ -202,8 +224,8 @@ protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config)
202224
* @since 1.4
203225
*/
204226
protected Pool<Jedis> createRedisPool() {
205-
return new JedisPool(getPoolConfig(), getShardInfo().getHost(), getShardInfo().getPort(), getShardInfo()
206-
.getTimeout(), getShardInfo().getPassword());
227+
return new JedisPool(getPoolConfig(), getShardInfo().getHost(), getShardInfo().getPort(),
228+
getTimeoutFrom(getShardInfo()), getShardInfo().getPassword());
207229
}
208230

209231
/*
@@ -424,7 +446,7 @@ public RedisSentinelConnection getSentinelConnection() {
424446
if (!isRedisSentinelAware()) {
425447
throw new InvalidDataAccessResourceUsageException("No Sentinels configured");
426448
}
427-
449+
428450
return new JedisSentinelConnection(getActiveSentinel());
429451
}
430452

@@ -456,4 +478,12 @@ private Set<String> convertToJedisSentinelSet(Collection<RedisNode> nodes) {
456478
return convertedNodes;
457479
}
458480

481+
private void setTimeoutOn(JedisShardInfo shardInfo, int timeout) {
482+
ReflectionUtils.invokeMethod(SET_TIMEOUT_METHOD, shardInfo, timeout);
483+
}
484+
485+
private int getTimeoutFrom(JedisShardInfo shardInfo) {
486+
return (Integer) ReflectionUtils.invokeMethod(GET_TIMEOUT_METHOD, shardInfo);
487+
}
488+
459489
}

0 commit comments

Comments
 (0)