From c0430685782fddc3a3db78030b0e42ad9990d08a Mon Sep 17 00:00:00 2001 From: Franjo Zilic Date: Sat, 23 Nov 2019 13:13:59 +0100 Subject: [PATCH 1/2] Add configuration for RabbitMQ requested channel max property See gh-19106 --- .../amqp/RabbitAutoConfiguration.java | 1 + .../autoconfigure/amqp/RabbitProperties.java | 16 ++++++++++++++++ .../amqp/RabbitAutoConfigurationTests.java | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index 8d81b0932b..5f146086c9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -140,6 +140,7 @@ public class RabbitAutoConfiguration { } map.from(properties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis) .to(factory::setConnectionTimeout); + map.from(properties::getRequestedChannelMax).whenNonNull().to(factory::setRequestedChannelMax); factory.afterPropertiesSet(); return factory; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index a10787d99b..403d6a033f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -40,6 +40,7 @@ import org.springframework.util.StringUtils; * @author Josh Thornhill * @author Gary Russell * @author Artsiom Yudovin + * @author Franjo Zilic * @since 1.0.0 */ @ConfigurationProperties(prefix = "spring.rabbitmq") @@ -102,6 +103,13 @@ public class RabbitProperties { */ private Duration connectionTimeout; + /** + * Requested Channel Max; zero for unlimited. Number of channels per connection client + * will request from server, actual maximum will be negotiated between client and + * server for lowest value (excluding zero as it represents unlimited). + */ + private Integer requestedChannelMax; + /** * Cache configuration. */ @@ -310,6 +318,14 @@ public class RabbitProperties { this.connectionTimeout = connectionTimeout; } + public Integer getRequestedChannelMax() { + return this.requestedChannelMax; + } + + public void setRequestedChannelMax(Integer requestedChannelMax) { + this.requestedChannelMax = requestedChannelMax; + } + public Cache getCache() { return this.cache; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java index bfe14c13bc..560d8061f2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java @@ -81,6 +81,7 @@ import static org.mockito.Mockito.verify; * @author Stephane Nicoll * @author Gary Russell * @author HaiTao Zhang + * @author Franjo Zilic */ class RabbitAutoConfigurationTests { @@ -715,6 +716,24 @@ class RabbitAutoConfigurationTests { return (TrustManager) trustManager; } + @Test + void testChangeDefaultRequestedChannelMax() throws Exception { + this.contextRunner.withUserConfiguration(TestConfiguration.class) + .withPropertyValues("spring.rabbitmq.requestedChannelMax:12").run((context) -> { + com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context); + assertThat(rabbitConnectionFactory.getRequestedChannelMax()).isEqualTo(12); + }); + } + + @Test + void testKeepDefaultRequestedChannelMax() throws Exception { + this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> { + com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context); + assertThat(rabbitConnectionFactory.getRequestedChannelMax()) + .isEqualTo(com.rabbitmq.client.ConnectionFactory.DEFAULT_CHANNEL_MAX); + }); + } + private com.rabbitmq.client.ConnectionFactory getTargetConnectionFactory(AssertableApplicationContext context) { CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class); return connectionFactory.getRabbitConnectionFactory(); From d2f256abe8c5b42af8be81ae20a86bb64706fa79 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 27 Nov 2019 10:33:46 +0100 Subject: [PATCH 2/2] Polish "Add configuration for RabbitMQ requested channel max property" See gh-19106 --- .../amqp/RabbitAutoConfiguration.java | 2 +- .../autoconfigure/amqp/RabbitProperties.java | 28 +++++++++--------- .../amqp/RabbitAutoConfigurationTests.java | 29 +++++++------------ 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index 5f146086c9..e9f717f05f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -124,6 +124,7 @@ public class RabbitAutoConfiguration { map.from(properties::determineVirtualHost).whenNonNull().to(factory::setVirtualHost); map.from(properties::getRequestedHeartbeat).whenNonNull().asInt(Duration::getSeconds) .to(factory::setRequestedHeartbeat); + map.from(properties::getRequestedChannelMax).to(factory::setRequestedChannelMax); RabbitProperties.Ssl ssl = properties.getSsl(); if (ssl.determineEnabled()) { factory.setUseSSL(true); @@ -140,7 +141,6 @@ public class RabbitAutoConfiguration { } map.from(properties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis) .to(factory::setConnectionTimeout); - map.from(properties::getRequestedChannelMax).whenNonNull().to(factory::setRequestedChannelMax); factory.afterPropertiesSet(); return factory; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index 403d6a033f..6eed4a9369 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -88,6 +88,11 @@ public class RabbitProperties { @DurationUnit(ChronoUnit.SECONDS) private Duration requestedHeartbeat; + /** + * Number of channels per connection requested by the client. Use 0 for unlimited. + */ + private int requestedChannelMax = 2047; + /** * Whether to enable publisher returns. */ @@ -103,13 +108,6 @@ public class RabbitProperties { */ private Duration connectionTimeout; - /** - * Requested Channel Max; zero for unlimited. Number of channels per connection client - * will request from server, actual maximum will be negotiated between client and - * server for lowest value (excluding zero as it represents unlimited). - */ - private Integer requestedChannelMax; - /** * Cache configuration. */ @@ -283,6 +281,14 @@ public class RabbitProperties { this.requestedHeartbeat = requestedHeartbeat; } + public int getRequestedChannelMax() { + return this.requestedChannelMax; + } + + public void setRequestedChannelMax(int requestedChannelMax) { + this.requestedChannelMax = requestedChannelMax; + } + @DeprecatedConfigurationProperty(reason = "replaced to support additional confirm types", replacement = "spring.rabbitmq.publisher-confirm-type") public boolean isPublisherConfirms() { @@ -318,14 +324,6 @@ public class RabbitProperties { this.connectionTimeout = connectionTimeout; } - public Integer getRequestedChannelMax() { - return this.requestedChannelMax; - } - - public void setRequestedChannelMax(Integer requestedChannelMax) { - this.requestedChannelMax = requestedChannelMax; - } - public Cache getCache() { return this.cache; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java index 560d8061f2..1630f03c05 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java @@ -100,6 +100,8 @@ class RabbitAutoConfigurationTests { assertThat(messagingTemplate.getRabbitTemplate()).isEqualTo(rabbitTemplate); assertThat(amqpAdmin).isNotNull(); assertThat(connectionFactory.getHost()).isEqualTo("localhost"); + assertThat(getTargetConnectionFactory(context).getRequestedChannelMax()) + .isEqualTo(com.rabbitmq.client.ConnectionFactory.DEFAULT_CHANNEL_MAX); assertThat(connectionFactory.isPublisherConfirms()).isFalse(); assertThat(connectionFactory.isPublisherReturns()).isFalse(); assertThat(context.containsBean("rabbitListenerContainerFactory")) @@ -601,6 +603,15 @@ class RabbitAutoConfigurationTests { }); } + @Test + void customizeRequestedChannelMax() { + this.contextRunner.withUserConfiguration(TestConfiguration.class) + .withPropertyValues("spring.rabbitmq.requestedChannelMax:12").run((context) -> { + com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context); + assertThat(rabbitConnectionFactory.getRequestedChannelMax()).isEqualTo(12); + }); + } + @Test void noSslByDefault() { this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> { @@ -716,24 +727,6 @@ class RabbitAutoConfigurationTests { return (TrustManager) trustManager; } - @Test - void testChangeDefaultRequestedChannelMax() throws Exception { - this.contextRunner.withUserConfiguration(TestConfiguration.class) - .withPropertyValues("spring.rabbitmq.requestedChannelMax:12").run((context) -> { - com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context); - assertThat(rabbitConnectionFactory.getRequestedChannelMax()).isEqualTo(12); - }); - } - - @Test - void testKeepDefaultRequestedChannelMax() throws Exception { - this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> { - com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context); - assertThat(rabbitConnectionFactory.getRequestedChannelMax()) - .isEqualTo(com.rabbitmq.client.ConnectionFactory.DEFAULT_CHANNEL_MAX); - }); - } - private com.rabbitmq.client.ConnectionFactory getTargetConnectionFactory(AssertableApplicationContext context) { CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class); return connectionFactory.getRabbitConnectionFactory();