From 50eedefec1df5b8ef1a89f792cdba237a75099a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 5 Jun 2015 16:21:41 -0500 Subject: [PATCH 1/2] Add connection timeout property for redis See gh-3142 --- .../redis/RedisAutoConfiguration.java | 4 ++ .../autoconfigure/redis/RedisProperties.java | 14 ++++ .../redis/RedisAutoConfigurationTests.java | 69 ++++++++++++------- 3 files changed, 61 insertions(+), 26 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java index 83a1cdbe11..cdb347bd59 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java @@ -52,6 +52,7 @@ import redis.clients.jedis.JedisPoolConfig; * @author Christian Dupuis * @author Christoph Strobl * @author Phillip Webb + * @author Eddú Meléndez */ @Configuration @ConditionalOnClass({ JedisConnection.class, RedisOperations.class, Jedis.class }) @@ -83,6 +84,9 @@ public class RedisAutoConfiguration { factory.setPassword(this.properties.getPassword()); } factory.setDatabase(this.properties.getDatabase()); + if(this.properties.getTimeout() > 0) { + factory.setTimeout(this.properties.getTimeout()); + } return factory; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisProperties.java index 94c2f28288..776ae74753 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisProperties.java @@ -23,6 +23,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * * @author Dave Syer * @author Christoph Strobl + * @author Eddú Meléndez */ @ConfigurationProperties(prefix = "spring.redis") public class RedisProperties { @@ -51,6 +52,11 @@ public class RedisProperties { private Sentinel sentinel; + /** + * Timeout to set in milliseconds. + */ + private int timeout; + public String getHost() { return this.host; } @@ -95,6 +101,14 @@ public class RedisProperties { return this.sentinel; } + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + public int getTimeout() { + return this.timeout; + } + public void setSentinel(Sentinel sentinel) { this.sentinel = sentinel; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java index 80f942c48d..e96ebea392 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java @@ -19,6 +19,8 @@ package org.springframework.boot.autoconfigure.redis; import java.util.Arrays; import java.util.List; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.test.EnvironmentTestUtils; @@ -40,29 +42,34 @@ import static org.junit.Assert.assertTrue; * @author Dave Syer * @author Christian Dupuis * @author Christoph Strobl + * @author Eddú Meléndez */ public class RedisAutoConfigurationTests { private AnnotationConfigApplicationContext context; + @Before + public void setup() { + this.context = new AnnotationConfigApplicationContext(); + } + + @After + public void close() { + if (this.context != null) { + this.context.close(); + } + } + @Test public void testDefaultRedisConfiguration() throws Exception { - this.context = new AnnotationConfigApplicationContext(); - this.context.register(RedisAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(); assertNotNull(this.context.getBean("redisTemplate", RedisOperations.class)); assertNotNull(this.context.getBean(StringRedisTemplate.class)); } @Test public void testOverrideRedisConfiguration() throws Exception { - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, "spring.redis.host:foo"); - EnvironmentTestUtils.addEnvironment(this.context, "spring.redis.database:1"); - this.context.register(RedisAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load("spring.redis.host:foo", "spring.redis.database:1"); assertEquals("foo", this.context.getBean(JedisConnectionFactory.class) .getHostName()); assertEquals(1, this.context.getBean(JedisConnectionFactory.class).getDatabase()); @@ -70,33 +77,29 @@ public class RedisAutoConfigurationTests { @Test public void testRedisConfigurationWithPool() throws Exception { - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, "spring.redis.host:foo"); - EnvironmentTestUtils.addEnvironment(this.context, "spring.redis.pool.max-idle:1"); - this.context.register(RedisAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load("spring.redis.host:foo", "spring.redis.pool.max-idle:1"); assertEquals("foo", this.context.getBean(JedisConnectionFactory.class) .getHostName()); assertEquals(1, this.context.getBean(JedisConnectionFactory.class) .getPoolConfig().getMaxIdle()); } + @Test + public void testRedisConfigurationWithTimeout() throws Exception { + load("spring.redis.host:foo", "spring.redis.timeout:100"); + assertEquals("foo", this.context.getBean(JedisConnectionFactory.class) + .getHostName()); + assertEquals(100, this.context.getBean(JedisConnectionFactory.class) + .getTimeout()); + } + @Test public void testRedisConfigurationWithSentinel() throws Exception { List sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380"); if (isAtLeastOneSentinelAvailable(sentinels)) { - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.redis.sentinel.master:mymaster"); - EnvironmentTestUtils.addEnvironment( - this.context, - "spring.redis.sentinel.nodes:" - + StringUtils.collectionToCommaDelimitedString(sentinels)); - this.context.register(RedisAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load("spring.redis.sentinel.master:mymaster", "spring.redis.sentinel.nodes:" + + StringUtils.collectionToCommaDelimitedString(sentinels)); assertTrue(this.context.getBean(JedisConnectionFactory.class) .isRedisSentinelAware()); @@ -138,4 +141,18 @@ public class RedisAutoConfigurationTests { } } + private void load(String... environment) { + this.context = doLoad(environment); + } + + private AnnotationConfigApplicationContext doLoad( + String... environment) { + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(applicationContext, environment); + applicationContext.register(RedisAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + applicationContext.refresh(); + return applicationContext; + } + } From 432c00e857f5c440b6a4e740f33847f9fe93d7cf Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sat, 6 Jun 2015 13:33:07 +0200 Subject: [PATCH 2/2] Polish redis connection timeout support Closes gh-3142 --- .../autoconfigure/redis/RedisProperties.java | 58 +++++++++---------- .../redis/RedisAutoConfigurationTests.java | 2 +- .../appendix-application-properties.adoc | 1 + 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisProperties.java index 776ae74753..ab29aa4284 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,14 +48,22 @@ public class RedisProperties { */ private int port = 6379; + /** + * Connection timeout in milliseconds. + */ + private int timeout; + private Pool pool; private Sentinel sentinel; - /** - * Timeout to set in milliseconds. - */ - private int timeout; + public int getDatabase() { + return this.database; + } + + public void setDatabase(int database) { + this.database = database; + } public String getHost() { return this.host; @@ -65,14 +73,6 @@ public class RedisProperties { this.host = host; } - public int getPort() { - return this.port; - } - - public void setPort(int port) { - this.port = port; - } - public String getPassword() { return this.password; } @@ -81,24 +81,12 @@ public class RedisProperties { this.password = password; } - public Pool getPool() { - return this.pool; + public int getPort() { + return this.port; } - public void setPool(Pool pool) { - this.pool = pool; - } - - public int getDatabase() { - return this.database; - } - - public void setDatabase(int database) { - this.database = database; - } - - public Sentinel getSentinel() { - return this.sentinel; + public void setPort(int port) { + this.port = port; } public void setTimeout(int timeout) { @@ -109,10 +97,22 @@ public class RedisProperties { return this.timeout; } + public Sentinel getSentinel() { + return this.sentinel; + } + public void setSentinel(Sentinel sentinel) { this.sentinel = sentinel; } + public Pool getPool() { + return this.pool; + } + + public void setPool(Pool pool) { + this.pool = pool; + } + /** * Pool properties. */ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java index e96ebea392..910df70847 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index ce4bd3db3a..a59cfd6d54 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -455,6 +455,7 @@ content into your application; rather pick only the properties that you need. spring.redis.pool.max-wait=-1 spring.redis.sentinel.master= # name of Redis server spring.redis.sentinel.nodes= # comma-separated list of host:port pairs + spring.redis.timeout= # connection timeout in milliseconds # ACTIVEMQ ({sc-spring-boot-autoconfigure}/jms/activemq/ActiveMQProperties.{sc-ext}[ActiveMQProperties]) spring.activemq.broker-url=tcp://localhost:61616 # connection URL