diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index 82b223904c..0fd123117d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -114,6 +114,9 @@ public class RabbitAutoConfiguration { factory.setTrustStore(ssl.getTrustStore()); factory.setTrustStorePassphrase(ssl.getTrustStorePassword()); } + if (config.getConnectionTimeout() != null) { + factory.setConnectionTimeout(config.getConnectionTimeout()); + } factory.afterPropertiesSet(); CachingConnectionFactory connectionFactory = new CachingConnectionFactory( factory.getObject()); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index 82a45f04d5..d264f43b88 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -88,6 +88,11 @@ public class RabbitProperties { */ private boolean publisherReturns; + /** + * The connection timeout, in milliseconds; zero for infinite. + */ + private Integer connectionTimeout; + /** * Cache configuration. */ @@ -222,6 +227,14 @@ public class RabbitProperties { this.publisherReturns = publisherReturns; } + public Integer getConnectionTimeout() { + return this.connectionTimeout; + } + + public void setConnectionTimeout(Integer connectionTimeout) { + this.connectionTimeout = connectionTimeout; + } + public Cache getCache() { return this.cache; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java index 6ddf2790ec..a777c129e4 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java @@ -101,12 +101,17 @@ public class RabbitAutoConfigurationTests { public void testConnectionFactoryWithOverrides() { load(TestConfiguration.class, "spring.rabbitmq.host:remote-server", "spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice", - "spring.rabbitmq.password:secret", "spring.rabbitmq.virtual_host:/vhost"); + "spring.rabbitmq.password:secret", "spring.rabbitmq.virtual_host:/vhost", + "spring.rabbitmq.connection-timeout:123"); CachingConnectionFactory connectionFactory = this.context .getBean(CachingConnectionFactory.class); assertThat(connectionFactory.getHost()).isEqualTo("remote-server"); assertThat(connectionFactory.getPort()).isEqualTo(9000); assertThat(connectionFactory.getVirtualHost()).isEqualTo("/vhost"); + DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory); + com.rabbitmq.client.ConnectionFactory rcf = (com.rabbitmq.client.ConnectionFactory) dfa + .getPropertyValue("rabbitConnectionFactory"); + assertThat(rcf.getConnectionTimeout()).isEqualTo(123); } @Test 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 50e468cc22..7c14584d2f 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -841,6 +841,7 @@ content into your application; rather pick only the properties that you need. spring.rabbitmq.cache.channel.size= # Number of channels to retain in the cache. spring.rabbitmq.cache.connection.mode=CHANNEL # Connection factory cache mode. spring.rabbitmq.cache.connection.size= # Number of connections to cache. + spring.rabbitmq.connection-timeout= # Connection Timeout, in milliseconds. spring.rabbitmq.dynamic=true # Create an AmqpAdmin bean. spring.rabbitmq.host=localhost # RabbitMQ host. spring.rabbitmq.listener.acknowledge-mode= # Acknowledge mode of container.