From c0cb813a0182fd5fb882e8da6376377c4fcc9705 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 1 Mar 2016 12:54:10 -0500 Subject: [PATCH] Add Cache Properties for RabbitMQ Closes gh-3502 --- .../amqp/RabbitAutoConfiguration.java | 13 +++++ .../autoconfigure/amqp/RabbitProperties.java | 58 ++++++++++++++++++- .../amqp/RabbitAutoConfigurationTests.java | 18 ++++++ .../appendix-application-properties.adoc | 7 ++- 4 files changed, 93 insertions(+), 3 deletions(-) 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 bec4af2795..f7092c029e 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 @@ -71,6 +71,7 @@ import org.springframework.context.annotation.Import; * @author Greg Turnquist * @author Josh Long * @author Stephane Nicoll + * @author Gary Russell */ @Configuration @ConditionalOnClass({ RabbitTemplate.class, Channel.class }) @@ -138,6 +139,18 @@ public class RabbitAutoConfiguration { CachingConnectionFactory connectionFactory = new CachingConnectionFactory( factory.getObject()); connectionFactory.setAddresses(config.getAddresses()); + if (config.getChannelCacheSize() != null) { + connectionFactory.setChannelCacheSize(config.getChannelCacheSize()); + } + if (config.getCacheMode() != null) { + connectionFactory.setCacheMode(config.getCacheMode()); + } + if (config.getConnectionCacheSize() != null) { + connectionFactory.setConnectionCacheSize(config.getConnectionCacheSize()); + } + if (config.getChannelCheckoutTimeout() != null) { + connectionFactory.setChannelCheckoutTimeout(config.getChannelCheckoutTimeout()); + } return connectionFactory; } 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 fc25f2952c..203a4a38bb 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 @@ -20,6 +20,7 @@ import java.util.LinkedHashSet; import java.util.Set; import org.springframework.amqp.core.AcknowledgeMode; +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.util.StringUtils; @@ -31,6 +32,7 @@ import org.springframework.util.StringUtils; * @author Stephane Nicoll * @author Andy Wilkinson * @author Josh Thornhill + * @author Gary Russell */ @ConfigurationProperties(prefix = "spring.rabbitmq") public class RabbitProperties { @@ -66,7 +68,7 @@ public class RabbitProperties { private String virtualHost; /** - * Comma-separated list of addresses to which the client should connect to. + * Comma-separated list of addresses to which the client should connect. */ private String addresses; @@ -75,6 +77,28 @@ public class RabbitProperties { */ private Integer requestedHeartbeat; + /** + * The number of channels to retain in the cache, or max channels per connection + * when channelCheckoutTimeout is > 0. + */ + private Integer channelCacheSize; + + /** + * The connection factory cache mode; CHANNEL (default) or CONNECTION. + */ + private CacheMode cacheMode; + + /** + * The number of connections to cache (only applies when cacheMode is CONNECTION). + */ + private Integer connectionCacheSize; + + /** + * The number of milliseconds to wait to obtain a channel if the channelCacheSize + * has been reached; if 0, always create a new channel. + */ + private Long channelCheckoutTimeout; + /** * Listener container configuration. */ @@ -186,6 +210,38 @@ public class RabbitProperties { this.requestedHeartbeat = requestedHeartbeat; } + public Integer getChannelCacheSize() { + return this.channelCacheSize; + } + + public void setChannelCacheSize(Integer channelCacheSize) { + this.channelCacheSize = channelCacheSize; + } + + public CacheMode getCacheMode() { + return this.cacheMode; + } + + public void setCacheMode(CacheMode cacheMode) { + this.cacheMode = cacheMode; + } + + public Integer getConnectionCacheSize() { + return this.connectionCacheSize; + } + + public void setConnectionCacheSize(Integer connectionCacheSize) { + this.connectionCacheSize = connectionCacheSize; + } + + public Long getChannelCheckoutTimeout() { + return this.channelCheckoutTimeout; + } + + public void setChannelCheckoutTimeout(Long channelCheckoutTimeout) { + this.channelCheckoutTimeout = channelCheckoutTimeout; + } + public Listener getListener() { return this.listener; } 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 c981b79a17..aa38580ad0 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 @@ -30,6 +30,7 @@ import org.springframework.amqp.rabbit.annotation.EnableRabbit; import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate; @@ -53,6 +54,7 @@ import static org.mockito.Mockito.verify; * * @author Greg Turnquist * @author Stephane Nicoll + * @author Gary Russell */ public class RabbitAutoConfigurationTests { @@ -149,6 +151,22 @@ public class RabbitAutoConfigurationTests { assertThat(connectionFactory.getPort()).isEqualTo(8001); } + @Test + public void testConnectionFactoryCacheSettings() { + load(TestConfiguration.class, + "spring.rabbitmq.channelCacheSize=23", + "spring.rabbitmq.cacheMode=CONNECTION", + "spring.rabbitmq.connectionCacheSize=2", + "spring.rabbitmq.channelCheckoutTimeout=1000"); + CachingConnectionFactory connectionFactory = this.context + .getBean(CachingConnectionFactory.class); + DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory); + assertThat(dfa.getPropertyValue("channelCacheSize")).isEqualTo(23); + assertThat(dfa.getPropertyValue("cacheMode")).isEqualTo(CacheMode.CONNECTION); + assertThat(dfa.getPropertyValue("connectionCacheSize")).isEqualTo(2); + assertThat(dfa.getPropertyValue("channelCheckoutTimeout")).isEqualTo(1000L); + } + @Test public void testRabbitTemplateBackOff() { load(TestConfiguration3.class); 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 7670bbba3f..c01feeb5c6 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -755,7 +755,7 @@ content into your application; rather pick only the properties that you need. spring.jms.pub-sub-domain=false # Specify if the default destination type is topic. # RABBIT ({sc-spring-boot-autoconfigure}/amqp/RabbitProperties.{sc-ext}[RabbitProperties]) - spring.rabbitmq.addresses= # Comma-separated list of addresses to which the client should connect to. + spring.rabbitmq.addresses= # Comma-separated list of addresses to which the client should connect. spring.rabbitmq.dynamic=true # Create an AmqpAdmin bean. spring.rabbitmq.host=localhost # RabbitMQ host. spring.rabbitmq.listener.acknowledge-mode= # Acknowledge mode of container. @@ -774,7 +774,10 @@ content into your application; rather pick only the properties that you need. spring.rabbitmq.ssl.trust-store-password= # Password used to access the trust store. spring.rabbitmq.username= # Login user to authenticate to the broker. spring.rabbitmq.virtual-host= # Virtual host to use when connecting to the broker. - + spring.rabbitmq.cacheMode= # The connection factory cache mode; CHANNEL (default) or CONNECTION. + spring.rabbitmq.channelCacheSize= # The number of channels to retain in the cache, or max channels per connection when `channelCheckoutTimeout` is > 0. + spring.rabbitmq.connectionCacheSize= # The number of connections to cache (only applies when cacheMode is CONNECTION). + spring.rabbitmq.channelCheckoutTimeout= # The number of milliseconds to wait to obtain a channel if the channelCacheSize has been reached; if 0, always create a new channel. # ---------------------------------------- # ACTUATOR PROPERTIES