Skip to content

Commit

Permalink
DATAREDIS-374 - Adapt JedisConnectionFactory for changes in Jedis 2.7.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Thomas Darimont authored and christophstrobl committed Mar 19, 2015
1 parent 8bf624b commit c47292d
Showing 1 changed file with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -37,6 +37,7 @@
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;
Expand All @@ -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;
Expand Down Expand Up @@ -165,7 +187,7 @@ public void afterPropertiesSet() {
}

if (timeout > 0) {
shardInfo.setTimeout(timeout);
setTimeoutOn(shardInfo, timeout);
}
}

Expand All @@ -191,8 +213,8 @@ private Pool<Jedis> createPool() {
*/
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());
}

/**
Expand All @@ -202,8 +224,8 @@ protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config)
* @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());
}

/*
Expand Down Expand Up @@ -424,7 +446,7 @@ public RedisSentinelConnection getSentinelConnection() {
if (!isRedisSentinelAware()) {
throw new InvalidDataAccessResourceUsageException("No Sentinels configured");
}

return new JedisSentinelConnection(getActiveSentinel());
}

Expand Down Expand Up @@ -456,4 +478,12 @@ private Set<String> convertToJedisSentinelSet(Collection<RedisNode> nodes) {
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);
}

}

0 comments on commit c47292d

Please sign in to comment.