17
17
package org .springframework .boot .autoconfigure .data .redis ;
18
18
19
19
import java .net .UnknownHostException ;
20
+ import java .time .Duration ;
21
+ import java .util .Collections ;
22
+ import java .util .List ;
20
23
21
24
import org .apache .commons .pool2 .impl .GenericObjectPool ;
22
25
import redis .clients .jedis .Jedis ;
30
33
import org .springframework .data .redis .connection .RedisClusterConfiguration ;
31
34
import org .springframework .data .redis .connection .RedisConnectionFactory ;
32
35
import org .springframework .data .redis .connection .RedisSentinelConfiguration ;
36
+ import org .springframework .data .redis .connection .jedis .JedisClientConfiguration ;
37
+ import org .springframework .data .redis .connection .jedis .JedisClientConfiguration .JedisClientConfigurationBuilder ;
33
38
import org .springframework .data .redis .connection .jedis .JedisConnection ;
34
39
import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
35
40
import org .springframework .util .StringUtils ;
@@ -46,66 +51,68 @@ class JedisConnectionConfiguration extends RedisConnectionConfiguration {
46
51
47
52
private final RedisProperties properties ;
48
53
54
+ private final List <JedisClientConfigurationBuilderCustomizer > builderCustomizers ;
55
+
49
56
JedisConnectionConfiguration (RedisProperties properties ,
50
57
ObjectProvider <RedisSentinelConfiguration > sentinelConfiguration ,
51
- ObjectProvider <RedisClusterConfiguration > clusterConfiguration ) {
58
+ ObjectProvider <RedisClusterConfiguration > clusterConfiguration ,
59
+ ObjectProvider <List <JedisClientConfigurationBuilderCustomizer >> builderCustomizers ) {
52
60
super (properties , sentinelConfiguration , clusterConfiguration );
53
61
this .properties = properties ;
62
+ this .builderCustomizers = builderCustomizers
63
+ .getIfAvailable (Collections ::emptyList );
54
64
}
55
65
56
66
@ Bean
57
67
@ ConditionalOnMissingBean (RedisConnectionFactory .class )
58
68
public JedisConnectionFactory redisConnectionFactory () throws UnknownHostException {
59
- return applyProperties ( createJedisConnectionFactory () );
69
+ return createJedisConnectionFactory ();
60
70
}
61
71
62
- private JedisConnectionFactory applyProperties (JedisConnectionFactory factory ) {
63
- configureConnection (factory );
64
- if (this .properties .isSsl ()) {
65
- factory .setUseSsl (true );
72
+ private JedisConnectionFactory createJedisConnectionFactory () {
73
+ JedisClientConfiguration clientConfiguration = getJedisClientConfiguration ();
74
+
75
+ if (getSentinelConfig () != null ) {
76
+ return new JedisConnectionFactory (getSentinelConfig (), clientConfiguration );
66
77
}
67
- factory .setDatabase (this .properties .getDatabase ());
68
- if (this .properties .getTimeout () > 0 ) {
69
- factory .setTimeout (this .properties .getTimeout ());
78
+
79
+ if (getClusterConfiguration () != null ) {
80
+ return new JedisConnectionFactory (getClusterConfiguration (),
81
+ clientConfiguration );
70
82
}
71
- return factory ;
83
+
84
+ return new JedisConnectionFactory (getStandaloneConfig (), clientConfiguration );
72
85
}
73
86
74
- private void configureConnection (JedisConnectionFactory factory ) {
75
- if (StringUtils .hasText (this .properties .getUrl ())) {
76
- configureConnectionFromUrl (factory );
87
+ private JedisClientConfiguration getJedisClientConfiguration () {
88
+ JedisClientConfigurationBuilder builder = applyProperties (
89
+ JedisClientConfiguration .builder ());
90
+ RedisProperties .Pool pool = this .properties .getJedis ().getPool ();
91
+ if (pool != null ) {
92
+ applyPooling (pool , builder );
77
93
}
78
- else {
79
- factory .setHostName (this .properties .getHost ());
80
- factory .setPort (this .properties .getPort ());
81
- if (this .properties .getPassword () != null ) {
82
- factory .setPassword (this .properties .getPassword ());
83
- }
94
+ if (StringUtils .hasText (this .properties .getUrl ())) {
95
+ customizeConfigurationFromUrl (builder );
84
96
}
97
+ customize (builder );
98
+ return builder .build ();
85
99
}
86
100
87
- private void configureConnectionFromUrl (JedisConnectionFactory factory ) {
88
- ConnectionInfo connectionInfo = parseUrl (this .properties .getUrl ());
89
- factory .setUseSsl (connectionInfo .isUseSsl ());
90
- factory .setHostName (connectionInfo .getHostName ());
91
- factory .setPort (connectionInfo .getPort ());
92
- if (connectionInfo .getPassword () != null ) {
93
- factory .setPassword (connectionInfo .getPassword ());
101
+ private JedisClientConfigurationBuilder applyProperties (
102
+ JedisClientConfigurationBuilder builder ) {
103
+ if (this .properties .isSsl ()) {
104
+ builder .useSsl ();
105
+ }
106
+ if (this .properties .getTimeout () != 0 ) {
107
+ Duration timeout = Duration .ofMillis (this .properties .getTimeout ());
108
+ builder .readTimeout (timeout ).connectTimeout (timeout );
94
109
}
110
+ return builder ;
95
111
}
96
112
97
- private JedisConnectionFactory createJedisConnectionFactory () {
98
- RedisProperties .Pool pool = this .properties .getJedis ().getPool ();
99
- JedisPoolConfig poolConfig = pool != null ? jedisPoolConfig (pool )
100
- : new JedisPoolConfig ();
101
-
102
- if (getSentinelConfig () != null ) {
103
- return new JedisConnectionFactory (getSentinelConfig (), poolConfig );
104
- }
105
- if (getClusterConfiguration () != null ) {
106
- return new JedisConnectionFactory (getClusterConfiguration (), poolConfig );
107
- }
108
- return new JedisConnectionFactory (poolConfig );
113
+ private void applyPooling (RedisProperties .Pool pool ,
114
+ JedisClientConfiguration .JedisClientConfigurationBuilder builder ) {
115
+ builder .usePooling ().poolConfig (jedisPoolConfig (pool ));
109
116
}
110
117
111
118
private JedisPoolConfig jedisPoolConfig (RedisProperties .Pool pool ) {
@@ -117,4 +124,19 @@ private JedisPoolConfig jedisPoolConfig(RedisProperties.Pool pool) {
117
124
return config ;
118
125
}
119
126
127
+ private void customizeConfigurationFromUrl (
128
+ JedisClientConfiguration .JedisClientConfigurationBuilder builder ) {
129
+ ConnectionInfo connectionInfo = parseUrl (this .properties .getUrl ());
130
+ if (connectionInfo .isUseSsl ()) {
131
+ builder .useSsl ();
132
+ }
133
+ }
134
+
135
+ private void customize (
136
+ JedisClientConfiguration .JedisClientConfigurationBuilder builder ) {
137
+ for (JedisClientConfigurationBuilderCustomizer customizer : this .builderCustomizers ) {
138
+ customizer .customize (builder );
139
+ }
140
+ }
141
+
120
142
}
0 commit comments