44
55import org .apache .commons .pool2 .impl .GenericObjectPool ;
66import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
7+ import org .springframework .beans .factory .annotation .Autowired ;
78import org .springframework .boot .autoconfigure .data .redis .RedisProperties ;
89import org .springframework .boot .autoconfigure .data .redis .RedisProperties .Pool ;
910import org .springframework .boot .context .properties .EnableConfigurationProperties ;
2223@ EnableConfigurationProperties (RedisProperties .class )
2324public class RediSearchAutoConfiguration {
2425
26+ @ Autowired
27+ private RedisProperties redisProperties ;
28+
2529 @ Bean (destroyMethod = "shutdown" )
2630 ClientResources clientResources () {
2731 return DefaultClientResources .create ();
2832 }
2933
3034 @ Bean (destroyMethod = "shutdown" )
31- RediSearchClient client (RedisProperties props , ClientResources clientResources ) {
32- RedisURI redisURI = RedisURI .create (props .getHost (), props .getPort ());
33- if (props .getPassword () != null ) {
34- redisURI .setPassword (props .getPassword ());
35+ RediSearchClient client (ClientResources clientResources ) {
36+ RedisURI redisURI = RedisURI .create (redisProperties .getHost (), redisProperties .getPort ());
37+ if (redisProperties .getPassword () != null ) {
38+ redisURI .setPassword (redisProperties .getPassword ());
3539 }
36- Duration timeout = props .getTimeout ();
40+ Duration timeout = redisProperties .getTimeout ();
3741 if (timeout != null ) {
3842 redisURI .setTimeout (timeout );
3943 }
@@ -45,23 +49,30 @@ StatefulRediSearchConnection<String, String> connection(RediSearchClient rediSea
4549 return rediSearchClient .connect ();
4650 }
4751
48- @ Bean (name = "rediSearchConnectionPool" , destroyMethod = "close" )
49- GenericObjectPool <StatefulRediSearchConnection <String , String >> pool (RedisProperties props ,
50- RediSearchClient rediSearchClient ) {
51- GenericObjectPoolConfig <StatefulRediSearchConnection <String , String >> config = new GenericObjectPoolConfig <StatefulRediSearchConnection <String , String >>();
52+ @ Bean (name = "rediSearchConnectionPoolConfig" )
53+ GenericObjectPoolConfig <StatefulRediSearchConnection <String , String >> poolConfig () {
54+ return configure (new GenericObjectPoolConfig <StatefulRediSearchConnection <String , String >>());
55+ }
56+
57+ public <K , V > GenericObjectPoolConfig <StatefulRediSearchConnection <K , V >> configure (
58+ GenericObjectPoolConfig <StatefulRediSearchConnection <K , V >> config ) {
5259 config .setJmxEnabled (false );
53- GenericObjectPool <StatefulRediSearchConnection <String , String >> pool = ConnectionPoolSupport
54- .createGenericObjectPool (() -> rediSearchClient .connect (), config );
55- Pool poolProps = props .getLettuce ().getPool ();
60+ Pool poolProps = redisProperties .getLettuce ().getPool ();
5661 if (poolProps != null ) {
57- pool .setMaxTotal (poolProps .getMaxActive ());
58- pool .setMaxIdle (poolProps .getMaxIdle ());
59- pool .setMinIdle (poolProps .getMinIdle ());
62+ config .setMaxTotal (poolProps .getMaxActive ());
63+ config .setMaxIdle (poolProps .getMaxIdle ());
64+ config .setMinIdle (poolProps .getMinIdle ());
6065 if (poolProps .getMaxWait () != null ) {
61- pool .setMaxWaitMillis (poolProps .getMaxWait ().toMillis ());
66+ config .setMaxWaitMillis (poolProps .getMaxWait ().toMillis ());
6267 }
6368 }
64- return pool ;
69+ return config ;
70+ }
71+
72+ @ Bean (name = "rediSearchConnectionPool" , destroyMethod = "close" )
73+ GenericObjectPool <StatefulRediSearchConnection <String , String >> pool (
74+ GenericObjectPoolConfig <StatefulRediSearchConnection <String , String >> config , RediSearchClient client ) {
75+ return ConnectionPoolSupport .createGenericObjectPool (client ::connect , config );
6576 }
6677
6778}
0 commit comments