Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jedisVersion=2.6.2
springVersion=4.0.9.RELEASE
springDataBuildVersion=1.6.0.BUILD-SNAPSHOT
log4jVersion=1.2.17
version=1.5.0.BUILD-SNAPSHOT
version=1.5.0.DATAREDIS-374-SNAPSHOT
srpVersion=0.7
jacksonVersion=1.8.8
fasterXmlJacksonVersion=2.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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 +38,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 +62,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 +188,7 @@ public void afterPropertiesSet() {
}

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

Expand All @@ -174,6 +197,14 @@ public void afterPropertiesSet() {
}
}

private static void setTimeoutOn(JedisShardInfo shardInfo, int timeout) {
ReflectionUtils.invokeMethod(SET_TIMEOUT_METHOD, shardInfo, timeout);
}

private static int getTimeoutFrom(JedisShardInfo shardInfo) {
return (Integer) ReflectionUtils.invokeMethod(GET_TIMEOUT_METHOD, shardInfo);
}

private Pool<Jedis> createPool() {

if (isRedisSentinelAware()) {
Expand All @@ -191,8 +222,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 +233,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 +455,7 @@ public RedisSentinelConnection getSentinelConnection() {
if (!isRedisSentinelAware()) {
throw new InvalidDataAccessResourceUsageException("No Sentinels configured");
}

return new JedisSentinelConnection(getActiveSentinel());
}

Expand Down