Add Cache Properties for RabbitMQ

Closes gh-3502
This commit is contained in:
Gary Russell 2016-03-01 12:54:10 -05:00 committed by Stephane Nicoll
parent 13f03b4161
commit c0cb813a01
4 changed files with 93 additions and 3 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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