diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java index 08fb24d07f49..261e6d1262cd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java @@ -113,6 +113,10 @@ private JedisPoolConfig jedisPoolConfig(RedisProperties.Pool pool) { config.setMaxTotal(pool.getMaxActive()); config.setMaxIdle(pool.getMaxIdle()); config.setMinIdle(pool.getMinIdle()); + if (pool.getTimeBetweenEvictionRuns() != null) { + config.setTimeBetweenEvictionRunsMillis( + pool.getTimeBetweenEvictionRuns().toMillis()); + } if (pool.getMaxWait() != null) { config.setMaxWaitMillis(pool.getMaxWait().toMillis()); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java index 54fe321b8b74..7fc5902d0cd5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java @@ -146,6 +146,10 @@ private GenericObjectPoolConfig getPoolConfig(Pool properties) { config.setMaxTotal(properties.getMaxActive()); config.setMaxIdle(properties.getMaxIdle()); config.setMinIdle(properties.getMinIdle()); + if (properties.getTimeBetweenEvictionRuns() != null) { + config.setTimeBetweenEvictionRunsMillis( + properties.getTimeBetweenEvictionRuns().toMillis()); + } if (properties.getMaxWait() != null) { config.setMaxWaitMillis(properties.getMaxWait().toMillis()); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java index 8258ddd33551..4dbf06128245 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java @@ -172,6 +172,9 @@ public static class Pool { /** * Target for the minimum number of idle connections to maintain in the pool. This * setting only has an effect if it is positive. + * + * This setting only has an effect if it is positive and `timeBetweenEvictionRuns` + * is greater than zero. */ private int minIdle = 0; @@ -188,6 +191,14 @@ public static class Pool { */ private Duration maxWait = Duration.ofMillis(-1); + /** + * Time to sleep between runs of the idle object evictor thread. + * + * When positive, the idle object evictor thread starts. When non-positive, no + * idle object evictor thread runs. + */ + private Duration timeBetweenEvictionRuns; + public int getMaxIdle() { return this.maxIdle; } @@ -220,6 +231,14 @@ public void setMaxWait(Duration maxWait) { this.maxWait = maxWait; } + public Duration getTimeBetweenEvictionRuns() { + return this.timeBetweenEvictionRuns; + } + + public void setTimeBetweenEvictionRuns(Duration timeBetweenEvictionRuns) { + this.timeBetweenEvictionRuns = timeBetweenEvictionRuns; + } + } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java index dd7153333e05..050d94625f97 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java @@ -134,11 +134,14 @@ public void testPasswordInUrlStartsWithColon() { @Test public void testRedisConfigurationWithPool() { - this.contextRunner.withPropertyValues("spring.redis.host:foo", - "spring.redis.jedis.pool.min-idle:1", - "spring.redis.jedis.pool.max-idle:4", - "spring.redis.jedis.pool.max-active:16", - "spring.redis.jedis.pool.max-wait:2000").run((context) -> { + this.contextRunner + .withPropertyValues("spring.redis.host:foo", + "spring.redis.jedis.pool.min-idle:1", + "spring.redis.jedis.pool.max-idle:4", + "spring.redis.jedis.pool.max-active:16", + "spring.redis.jedis.pool.max-wait:2000", + "spring.redis.jedis.pool.time-between-eviction-runs:30000") + .run((context) -> { JedisConnectionFactory cf = context .getBean(JedisConnectionFactory.class); assertThat(cf.getHostName()).isEqualTo("foo"); @@ -146,6 +149,8 @@ public void testRedisConfigurationWithPool() { assertThat(cf.getPoolConfig().getMaxIdle()).isEqualTo(4); assertThat(cf.getPoolConfig().getMaxTotal()).isEqualTo(16); assertThat(cf.getPoolConfig().getMaxWaitMillis()).isEqualTo(2000); + assertThat(cf.getPoolConfig().getTimeBetweenEvictionRunsMillis()) + .isEqualTo(30000); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java index 0b8520089e16..79c5bcfed2b5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java @@ -155,6 +155,7 @@ public void testRedisConfigurationWithPool() { "spring.redis.lettuce.pool.max-idle:4", "spring.redis.lettuce.pool.max-active:16", "spring.redis.lettuce.pool.max-wait:2000", + "spring.redis.lettuce.pool.time-between-eviction-runs:30000", "spring.redis.lettuce.shutdown-timeout:1000").run((context) -> { LettuceConnectionFactory cf = context .getBean(LettuceConnectionFactory.class); @@ -165,6 +166,8 @@ public void testRedisConfigurationWithPool() { assertThat(poolConfig.getMaxIdle()).isEqualTo(4); assertThat(poolConfig.getMaxTotal()).isEqualTo(16); assertThat(poolConfig.getMaxWaitMillis()).isEqualTo(2000); + assertThat(poolConfig.getTimeBetweenEvictionRunsMillis()) + .isEqualTo(30000); assertThat(cf.getShutdownTimeout()).isEqualTo(1000); }); }