Commit c0cb813a authored by Gary Russell's avatar Gary Russell Committed by Stephane Nicoll

Add Cache Properties for RabbitMQ

Closes gh-3502
parent 13f03b41
...@@ -71,6 +71,7 @@ import org.springframework.context.annotation.Import; ...@@ -71,6 +71,7 @@ import org.springframework.context.annotation.Import;
* @author Greg Turnquist * @author Greg Turnquist
* @author Josh Long * @author Josh Long
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Gary Russell
*/ */
@Configuration @Configuration
@ConditionalOnClass({ RabbitTemplate.class, Channel.class }) @ConditionalOnClass({ RabbitTemplate.class, Channel.class })
...@@ -138,6 +139,18 @@ public class RabbitAutoConfiguration { ...@@ -138,6 +139,18 @@ public class RabbitAutoConfiguration {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory( CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
factory.getObject()); factory.getObject());
connectionFactory.setAddresses(config.getAddresses()); 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; return connectionFactory;
} }
......
...@@ -20,6 +20,7 @@ import java.util.LinkedHashSet; ...@@ -20,6 +20,7 @@ import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import org.springframework.amqp.core.AcknowledgeMode; import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -31,6 +32,7 @@ import org.springframework.util.StringUtils; ...@@ -31,6 +32,7 @@ import org.springframework.util.StringUtils;
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Josh Thornhill * @author Josh Thornhill
* @author Gary Russell
*/ */
@ConfigurationProperties(prefix = "spring.rabbitmq") @ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitProperties { public class RabbitProperties {
...@@ -66,7 +68,7 @@ public class RabbitProperties { ...@@ -66,7 +68,7 @@ public class RabbitProperties {
private String virtualHost; 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; private String addresses;
...@@ -75,6 +77,28 @@ public class RabbitProperties { ...@@ -75,6 +77,28 @@ public class RabbitProperties {
*/ */
private Integer requestedHeartbeat; 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. * Listener container configuration.
*/ */
...@@ -186,6 +210,38 @@ public class RabbitProperties { ...@@ -186,6 +210,38 @@ public class RabbitProperties {
this.requestedHeartbeat = requestedHeartbeat; 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() { public Listener getListener() {
return this.listener; return this.listener;
} }
......
...@@ -30,6 +30,7 @@ import org.springframework.amqp.rabbit.annotation.EnableRabbit; ...@@ -30,6 +30,7 @@ import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils; import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; 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.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate; import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
...@@ -53,6 +54,7 @@ import static org.mockito.Mockito.verify; ...@@ -53,6 +54,7 @@ import static org.mockito.Mockito.verify;
* *
* @author Greg Turnquist * @author Greg Turnquist
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Gary Russell
*/ */
public class RabbitAutoConfigurationTests { public class RabbitAutoConfigurationTests {
...@@ -149,6 +151,22 @@ public class RabbitAutoConfigurationTests { ...@@ -149,6 +151,22 @@ public class RabbitAutoConfigurationTests {
assertThat(connectionFactory.getPort()).isEqualTo(8001); 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 @Test
public void testRabbitTemplateBackOff() { public void testRabbitTemplateBackOff() {
load(TestConfiguration3.class); load(TestConfiguration3.class);
......
...@@ -755,7 +755,7 @@ content into your application; rather pick only the properties that you need. ...@@ -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. spring.jms.pub-sub-domain=false # Specify if the default destination type is topic.
# RABBIT ({sc-spring-boot-autoconfigure}/amqp/RabbitProperties.{sc-ext}[RabbitProperties]) # 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.dynamic=true # Create an AmqpAdmin bean.
spring.rabbitmq.host=localhost # RabbitMQ host. spring.rabbitmq.host=localhost # RabbitMQ host.
spring.rabbitmq.listener.acknowledge-mode= # Acknowledge mode of container. 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. ...@@ -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.ssl.trust-store-password= # Password used to access the trust store.
spring.rabbitmq.username= # Login user to authenticate to the broker. 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.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 # ACTUATOR PROPERTIES
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment