Commit b0bfc11a authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #5303 from garyrussell/GH-5302

* pr/5303:
  Polish contribution
  Add Cache Properties for RabbitMQ
parents 13f03b41 d04155fc
...@@ -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.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; return connectionFactory;
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 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,11 @@ public class RabbitProperties { ...@@ -75,6 +77,11 @@ public class RabbitProperties {
*/ */
private Integer requestedHeartbeat; private Integer requestedHeartbeat;
/**
* Cache configuration.
*/
private final Cache cache = new Cache();
/** /**
* Listener container configuration. * Listener container configuration.
*/ */
...@@ -186,6 +193,10 @@ public class RabbitProperties { ...@@ -186,6 +193,10 @@ public class RabbitProperties {
this.requestedHeartbeat = requestedHeartbeat; this.requestedHeartbeat = requestedHeartbeat;
} }
public Cache getCache() {
return this.cache;
}
public Listener getListener() { public Listener getListener() {
return this.listener; 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 { public static class 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.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 @Test
public void testRabbitTemplateBackOff() { public void testRabbitTemplateBackOff() {
load(TestConfiguration3.class); load(TestConfiguration3.class);
......
...@@ -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. 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.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.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.
...@@ -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.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.
# ---------------------------------------- # ----------------------------------------
# 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