1
1
/*
2
- * Copyright 2011-2014 the original author or authors.
2
+ * Copyright 2011-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
-
17
16
package org .springframework .data .redis .connection .jedis ;
18
17
18
+ import java .lang .reflect .Method ;
19
19
import java .util .Collection ;
20
20
import java .util .Collections ;
21
21
import java .util .LinkedHashSet ;
37
37
import org .springframework .data .redis .connection .RedisSentinelConnection ;
38
38
import org .springframework .util .Assert ;
39
39
import org .springframework .util .CollectionUtils ;
40
+ import org .springframework .util .ReflectionUtils ;
40
41
import org .springframework .util .StringUtils ;
41
42
42
43
import redis .clients .jedis .Jedis ;
@@ -60,6 +61,27 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
60
61
private static final ExceptionTranslationStrategy EXCEPTION_TRANSLATION = new PassThroughExceptionTranslationStrategy (
61
62
JedisConverters .exceptionConverter ());
62
63
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
+
63
85
private JedisShardInfo shardInfo ;
64
86
private String hostName = "localhost" ;
65
87
private int port = Protocol .DEFAULT_PORT ;
@@ -165,7 +187,7 @@ public void afterPropertiesSet() {
165
187
}
166
188
167
189
if (timeout > 0 ) {
168
- shardInfo . setTimeout ( timeout );
190
+ setTimeoutOn ( shardInfo , timeout );
169
191
}
170
192
}
171
193
@@ -191,8 +213,8 @@ private Pool<Jedis> createPool() {
191
213
*/
192
214
protected Pool <Jedis > createRedisSentinelPool (RedisSentinelConfiguration config ) {
193
215
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 ());
196
218
}
197
219
198
220
/**
@@ -202,8 +224,8 @@ protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config)
202
224
* @since 1.4
203
225
*/
204
226
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 ());
207
229
}
208
230
209
231
/*
@@ -424,7 +446,7 @@ public RedisSentinelConnection getSentinelConnection() {
424
446
if (!isRedisSentinelAware ()) {
425
447
throw new InvalidDataAccessResourceUsageException ("No Sentinels configured" );
426
448
}
427
-
449
+
428
450
return new JedisSentinelConnection (getActiveSentinel ());
429
451
}
430
452
@@ -456,4 +478,12 @@ private Set<String> convertToJedisSentinelSet(Collection<RedisNode> nodes) {
456
478
return convertedNodes ;
457
479
}
458
480
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
+
459
489
}
0 commit comments