15
15
*/
16
16
package org .springframework .data .redis .connection .jedis ;
17
17
18
- import redis .clients .jedis .Client ;
18
+ import redis .clients .jedis .DefaultJedisClientConfig ;
19
19
import redis .clients .jedis .HostAndPort ;
20
20
import redis .clients .jedis .Jedis ;
21
+ import redis .clients .jedis .JedisClientConfig ;
21
22
import redis .clients .jedis .JedisCluster ;
22
23
import redis .clients .jedis .JedisPool ;
23
24
import redis .clients .jedis .JedisPoolConfig ;
41
42
import org .apache .commons .logging .Log ;
42
43
import org .apache .commons .logging .LogFactory ;
43
44
import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
45
+
44
46
import org .springframework .beans .factory .DisposableBean ;
45
47
import org .springframework .beans .factory .InitializingBean ;
46
48
import org .springframework .dao .DataAccessException ;
58
60
import org .springframework .util .Assert ;
59
61
import org .springframework .util .ClassUtils ;
60
62
import org .springframework .util .CollectionUtils ;
63
+ import org .springframework .util .ObjectUtils ;
61
64
import org .springframework .util .StringUtils ;
62
65
63
66
/**
@@ -88,6 +91,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
88
91
89
92
private final JedisClientConfiguration clientConfiguration ;
90
93
private @ Nullable JedisShardInfo shardInfo ;
94
+ private JedisClientConfig clientConfig = DefaultJedisClientConfig .builder ().build ();
91
95
private boolean providedShardInfo = false ;
92
96
private @ Nullable Pool <Jedis > pool ;
93
97
private boolean convertPipelineAndTxResults = true ;
@@ -290,17 +294,7 @@ private Jedis createJedis() {
290
294
return new Jedis (getShardInfo ());
291
295
}
292
296
293
- Jedis jedis = new Jedis (getHostName (), getPort (), getConnectTimeout (), getReadTimeout (), isUseSsl (),
294
- clientConfiguration .getSslSocketFactory ().orElse (null ), //
295
- clientConfiguration .getSslParameters ().orElse (null ), //
296
- clientConfiguration .getHostnameVerifier ().orElse (null ));
297
-
298
- Client client = jedis .getClient ();
299
-
300
- getRedisPassword ().map (String ::new ).ifPresent (client ::setPassword );
301
- client .setDb (getDatabase ());
302
-
303
- return jedis ;
297
+ return new Jedis (new HostAndPort (getHostName (), getPort ()), this .clientConfig );
304
298
}
305
299
306
300
/**
@@ -320,6 +314,8 @@ protected JedisConnection postProcessConnection(JedisConnection connection) {
320
314
*/
321
315
public void afterPropertiesSet () {
322
316
317
+ clientConfig = createClientConfig (getRedisUsername (), getRedisPassword ());
318
+
323
319
if (shardInfo == null && clientConfiguration instanceof MutableJedisClientConfiguration ) {
324
320
325
321
providedShardInfo = false ;
@@ -357,6 +353,33 @@ public void afterPropertiesSet() {
357
353
}
358
354
}
359
355
356
+ private JedisClientConfig createClientConfig (@ Nullable String username , RedisPassword password ) {
357
+
358
+ DefaultJedisClientConfig .Builder builder = DefaultJedisClientConfig .builder ();
359
+
360
+ clientConfiguration .getClientName ().ifPresent (builder ::clientName );
361
+ builder .connectionTimeoutMillis (getConnectTimeout ());
362
+ builder .socketTimeoutMillis (getReadTimeout ());
363
+
364
+ builder .databse (getDatabase ());
365
+
366
+ if (!ObjectUtils .isEmpty (username )) {
367
+ builder .user (username );
368
+ }
369
+ password .toOptional ().map (String ::new ).ifPresent (builder ::password );
370
+
371
+ if (isUseSsl ()) {
372
+
373
+ builder .ssl (true );
374
+
375
+ clientConfiguration .getSslSocketFactory ().ifPresent (builder ::sslSocketFactory );
376
+ clientConfiguration .getHostnameVerifier ().ifPresent (builder ::hostnameVerifier );
377
+ clientConfiguration .getSslParameters ().ifPresent (builder ::sslParameters );
378
+ }
379
+
380
+ return builder .build ();
381
+ }
382
+
360
383
private Pool <Jedis > createPool () {
361
384
362
385
if (isRedisSentinelAware ()) {
@@ -374,13 +397,12 @@ private Pool<Jedis> createPool() {
374
397
*/
375
398
protected Pool <Jedis > createRedisSentinelPool (RedisSentinelConfiguration config ) {
376
399
377
- GenericObjectPoolConfig <? > poolConfig = getPoolConfig () != null ? getPoolConfig () : new JedisPoolConfig ();
400
+ GenericObjectPoolConfig <Jedis > poolConfig = getPoolConfig () != null ? getPoolConfig () : new JedisPoolConfig ();
378
401
String sentinelUser = null ;
379
- String sentinelPassword = config .getSentinelPassword ().toOptional ().map (String ::new ).orElse (null );
380
402
403
+ JedisClientConfig sentinelConfig = createClientConfig (sentinelUser , config .getSentinelPassword ());
381
404
return new JedisSentinelPool (config .getMaster ().getName (), convertToJedisSentinelSet (config .getSentinels ()),
382
- poolConfig , getConnectTimeout (), getReadTimeout (), getUsername (), getPassword (), getDatabase (), getClientName (),
383
- getConnectTimeout (), getReadTimeout (), sentinelUser , sentinelPassword , getClientName ());
405
+ poolConfig , this .clientConfig , sentinelConfig );
384
406
}
385
407
386
408
/**
@@ -390,12 +412,7 @@ poolConfig, getConnectTimeout(), getReadTimeout(), getUsername(), getPassword(),
390
412
* @since 1.4
391
413
*/
392
414
protected Pool <Jedis > createRedisPool () {
393
-
394
- return new JedisPool (getPoolConfig (), getHostName (), getPort (), getConnectTimeout (), getReadTimeout (),
395
- getUsername (), getPassword (), getDatabase (), getClientName (), isUseSsl (),
396
- clientConfiguration .getSslSocketFactory ().orElse (null ), //
397
- clientConfiguration .getSslParameters ().orElse (null ), //
398
- clientConfiguration .getHostnameVerifier ().orElse (null ));
415
+ return new JedisPool (getPoolConfig (), new HostAndPort (getHostName (), getPort ()), this .clientConfig );
399
416
}
400
417
401
418
private JedisCluster createCluster () {
@@ -423,7 +440,8 @@ protected ClusterTopologyProvider createTopologyProvider(JedisCluster cluster) {
423
440
* @return the actual {@link JedisCluster}.
424
441
* @since 1.7
425
442
*/
426
- protected JedisCluster createCluster (RedisClusterConfiguration clusterConfig , GenericObjectPoolConfig <?> poolConfig ) {
443
+ protected JedisCluster createCluster (RedisClusterConfiguration clusterConfig ,
444
+ GenericObjectPoolConfig <Jedis > poolConfig ) {
427
445
428
446
Assert .notNull (clusterConfig , "Cluster configuration must not be null!" );
429
447
@@ -434,10 +452,7 @@ protected JedisCluster createCluster(RedisClusterConfiguration clusterConfig, Ge
434
452
435
453
int redirects = clusterConfig .getMaxRedirects () != null ? clusterConfig .getMaxRedirects () : 5 ;
436
454
437
- return new JedisCluster (hostAndPort , getConnectTimeout (), getReadTimeout (), redirects , getUsername (), getPassword (),
438
- getClientName (), poolConfig , isUseSsl (), clientConfiguration .getSslSocketFactory ().orElse (null ),
439
- clientConfiguration .getSslParameters ().orElse (null ), clientConfiguration .getHostnameVerifier ().orElse (null ),
440
- null );
455
+ return new JedisCluster (hostAndPort , this .clientConfig , redirects , poolConfig );
441
456
}
442
457
443
458
/*
@@ -553,16 +568,6 @@ public void setUseSsl(boolean useSsl) {
553
568
getMutableConfiguration ().setUseSsl (useSsl );
554
569
}
555
570
556
- /**
557
- * Returns the username used for authenticating with the Redis server.
558
- *
559
- * @return username for authentication.
560
- */
561
- @ Nullable
562
- private String getUsername () {
563
- return getRedisUsername ();
564
- }
565
-
566
571
/**
567
572
* Returns the password used for authenticating with the Redis server.
568
573
*
@@ -715,7 +720,7 @@ public void setUsePool(boolean usePool) {
715
720
* @return the poolConfig
716
721
*/
717
722
@ Nullable
718
- public GenericObjectPoolConfig getPoolConfig () {
723
+ public GenericObjectPoolConfig < Jedis > getPoolConfig () {
719
724
return clientConfiguration .getPoolConfig ().orElse (null );
720
725
}
721
726
@@ -877,35 +882,41 @@ private Jedis getActiveSentinel() {
877
882
Assert .isTrue (RedisConfiguration .isSentinelConfiguration (configuration ), "SentinelConfig must not be null!" );
878
883
SentinelConfiguration sentinelConfiguration = (SentinelConfiguration ) configuration ;
879
884
885
+ JedisClientConfig clientConfig = createClientConfig (null , sentinelConfiguration .getSentinelPassword ());
880
886
for (RedisNode node : sentinelConfiguration .getSentinels ()) {
881
887
882
- Jedis jedis = new Jedis ( node . getHost (), node . getPort (), getConnectTimeout (), getReadTimeout ()) ;
883
- sentinelConfiguration . getSentinelPassword (). toOptional (). map ( String :: new ). ifPresent ( jedis :: auth ) ;
888
+ Jedis jedis = null ;
889
+ boolean success = false ;
884
890
885
891
try {
886
- if (jedis .ping ().equalsIgnoreCase ("pong" )) {
887
892
888
- potentiallySetClientName (jedis );
893
+ jedis = new Jedis (new HostAndPort (node .getHost (), node .getPort ()), clientConfig );
894
+ if (jedis .ping ().equalsIgnoreCase ("pong" )) {
895
+ success = true ;
889
896
return jedis ;
890
897
}
891
898
} catch (Exception ex ) {
892
899
log .warn (String .format ("Ping failed for sentinel host:%s" , node .getHost ()), ex );
900
+ } finally {
901
+ if (!success && jedis != null ) {
902
+ jedis .close ();
903
+ }
893
904
}
894
905
}
895
906
896
907
throw new InvalidDataAccessResourceUsageException ("No Sentinel found" );
897
908
}
898
909
899
- private Set <String > convertToJedisSentinelSet (Collection <RedisNode > nodes ) {
910
+ private static Set <HostAndPort > convertToJedisSentinelSet (Collection <RedisNode > nodes ) {
900
911
901
912
if (CollectionUtils .isEmpty (nodes )) {
902
913
return Collections .emptySet ();
903
914
}
904
915
905
- Set <String > convertedNodes = new LinkedHashSet <>(nodes .size ());
916
+ Set <HostAndPort > convertedNodes = new LinkedHashSet <>(nodes .size ());
906
917
for (RedisNode node : nodes ) {
907
918
if (node != null ) {
908
- convertedNodes .add (node .asString ( ));
919
+ convertedNodes .add (new HostAndPort ( node .getHost (), node . getPort () ));
909
920
}
910
921
}
911
922
return convertedNodes ;
0 commit comments