Browse Source

Merge pull request #5303 from garyrussell/GH-5302

* pr/5303:
  Polish contribution
  Add Cache Properties for RabbitMQ
pull/5102/merge
Stephane Nicoll 10 years ago
parent
commit
b0bfc11aa0
  1. 13
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java
  2. 93
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java
  3. 18
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java
  4. 7
      spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

13
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java

@ -71,6 +71,7 @@ import org.springframework.context.annotation.Import; @@ -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 { @@ -138,6 +139,18 @@ public class RabbitAutoConfiguration {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
factory.getObject());
connectionFactory.setAddresses(config.getAddresses());
if (config.getCache().getChannel().getSize() != null) {
connectionFactory.setChannelCacheSize(config.getCache().getChannel().getSize());
}
if (config.getCache().getConnection().getMode() != null) {
connectionFactory.setCacheMode(config.getCache().getConnection().getMode());
}
if (config.getCache().getConnection().getSize() != null) {
connectionFactory.setConnectionCacheSize(config.getCache().getConnection().getSize());
}
if (config.getCache().getChannel().getCheckoutTimeout() != null) {
connectionFactory.setChannelCheckoutTimeout(config.getCache().getChannel().getCheckoutTimeout());
}
return connectionFactory;
}

93
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@ -20,6 +20,7 @@ import java.util.LinkedHashSet; @@ -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; @@ -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 { @@ -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,11 @@ public class RabbitProperties { @@ -75,6 +77,11 @@ public class RabbitProperties {
*/
private Integer requestedHeartbeat;
/**
* Cache configuration.
*/
private final Cache cache = new Cache();
/**
* Listener container configuration.
*/
@ -186,6 +193,10 @@ public class RabbitProperties { @@ -186,6 +193,10 @@ public class RabbitProperties {
this.requestedHeartbeat = requestedHeartbeat;
}
public Cache getCache() {
return this.cache;
}
public Listener getListener() {
return this.listener;
}
@ -259,6 +270,84 @@ public class RabbitProperties { @@ -259,6 +270,84 @@ public class RabbitProperties {
}
public static class Cache {
private final Channel channel = new Channel();
private final Connection connection = new Connection();
public Channel getChannel() {
return this.channel;
}
public Connection getConnection() {
return this.connection;
}
public static class Channel {
/**
* Number of channels to retain in the cache. When "check-timeout" > 0, max
* channels per connection.
*/
private Integer size;
/**
* Number of milliseconds to wait to obtain a channel if the cache size
* has been reached. If 0, always create a new channel.
*/
private Long checkoutTimeout;
public Integer getSize() {
return this.size;
}
public void setSize(Integer size) {
this.size = size;
}
public Long getCheckoutTimeout() {
return this.checkoutTimeout;
}
public void setCheckoutTimeout(Long checkoutTimeout) {
this.checkoutTimeout = checkoutTimeout;
}
}
public static class Connection {
/**
* Connection factory cache mode.
*/
private CacheMode mode = CacheMode.CHANNEL;
/**
* Number of connections to cache. Only applies when mode is CONNECTION.
*/
private Integer size;
public CacheMode getMode() {
return this.mode;
}
public void setMode(CacheMode mode) {
this.mode = mode;
}
public Integer getSize() {
return this.size;
}
public void setSize(Integer size) {
this.size = size;
}
}
}
public static class Listener {
/**

18
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java

@ -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.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; @@ -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 { @@ -149,6 +151,22 @@ public class RabbitAutoConfigurationTests {
assertThat(connectionFactory.getPort()).isEqualTo(8001);
}
@Test
public void testConnectionFactoryCacheSettings() {
load(TestConfiguration.class,
"spring.rabbitmq.cache.channel.size=23",
"spring.rabbitmq.cache.channel.checkoutTimeout=1000",
"spring.rabbitmq.cache.connection.mode=CONNECTION",
"spring.rabbitmq.cache.connection.size=2");
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);

7
spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

@ -755,7 +755,11 @@ content into your application; rather pick only the properties that you need. @@ -755,7 +755,11 @@ 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.cache.channel.checkout-timeout= # Number of milliseconds to wait to obtain a channel if the cache size has been reached.
spring.rabbitmq.cache.channel.size= # Number of channels to retain in the cache.
spring.rabbitmq.cache.connection.mode= # Connection factory cache mode.
spring.rabbitmq.cache.connection.size= # Number of connections to cache.
spring.rabbitmq.dynamic=true # Create an AmqpAdmin bean.
spring.rabbitmq.host=localhost # RabbitMQ host.
spring.rabbitmq.listener.acknowledge-mode= # Acknowledge mode of container.
@ -775,7 +779,6 @@ content into your application; rather pick only the properties that you need. @@ -775,7 +779,6 @@ content into your application; rather pick only the properties that you need.
spring.rabbitmq.username= # Login user to authenticate to the broker.
spring.rabbitmq.virtual-host= # Virtual host to use when connecting to the broker.
# ----------------------------------------
# ACTUATOR PROPERTIES
# ----------------------------------------

Loading…
Cancel
Save