From 954f56287fdb169db04d98562ec8fa542efa7255 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Wed, 27 Sep 2023 09:40:23 -0400 Subject: [PATCH 1/2] Add config prop for Rabbit's max inbound message body size See gh-37603 --- .../RabbitConnectionFactoryBeanConfigurer.java | 3 +++ .../boot/autoconfigure/amqp/RabbitProperties.java | 14 ++++++++++++++ .../amqp/RabbitAutoConfigurationTests.java | 7 ++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitConnectionFactoryBeanConfigurer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitConnectionFactoryBeanConfigurer.java index f54e91ace5..5e839306d5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitConnectionFactoryBeanConfigurer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitConnectionFactoryBeanConfigurer.java @@ -133,6 +133,9 @@ public class RabbitConnectionFactoryBeanConfigurer { .to(factory::setChannelRpcTimeout); map.from(this.credentialsProvider).whenNonNull().to(factory::setCredentialsProvider); map.from(this.credentialsRefreshService).whenNonNull().to(factory::setCredentialsRefreshService); + map.from(this.rabbitProperties.getMaxInboundMessageBodySize()) + .whenNonNull() + .to((mimbs) -> factory.setMaxInboundMessageBodySize(Math.toIntExact(mimbs.toBytes()))); } } 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 a9ae5efc41..bd92568080 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 @@ -31,6 +31,7 @@ import org.springframework.boot.context.properties.source.InvalidConfigurationPr import org.springframework.boot.convert.DurationUnit; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; +import org.springframework.util.unit.DataSize; /** * Configuration properties for Rabbit. @@ -130,6 +131,11 @@ public class RabbitProperties { */ private Duration channelRpcTimeout = Duration.ofMinutes(10); + /** + * Maximum body size of inbound (received) messages in bytes. + */ + private DataSize maxInboundMessageBodySize; + /** * Cache configuration. */ @@ -360,6 +366,14 @@ public class RabbitProperties { this.channelRpcTimeout = channelRpcTimeout; } + public DataSize getMaxInboundMessageBodySize() { + return this.maxInboundMessageBodySize; + } + + public void setMaxInboundMessageBodySize(DataSize maxInboundMessageBodySize) { + this.maxInboundMessageBodySize = maxInboundMessageBodySize; + } + 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 e9a9b69d94..a66472cf9d 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 @@ -150,6 +150,9 @@ class RabbitAutoConfigurationTests { com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context); assertThat(rabbitConnectionFactory.getUsername()).isEqualTo(properties.getUsername()); assertThat(rabbitConnectionFactory.getPassword()).isEqualTo(properties.getPassword()); + com.rabbitmq.client.ConnectionFactory defaultCf = new com.rabbitmq.client.ConnectionFactory(); + assertThat(rabbitConnectionFactory).hasFieldOrPropertyWithValue("maxInboundMessageBodySize", + ReflectionTestUtils.getField(defaultCf, "maxInboundMessageBodySize")); }); } @@ -160,7 +163,8 @@ class RabbitAutoConfigurationTests { .withPropertyValues("spring.rabbitmq.host:remote-server", "spring.rabbitmq.port:9000", "spring.rabbitmq.address-shuffle-mode=random", "spring.rabbitmq.username:alice", "spring.rabbitmq.password:secret", "spring.rabbitmq.virtual_host:/vhost", - "spring.rabbitmq.connection-timeout:123", "spring.rabbitmq.channel-rpc-timeout:140") + "spring.rabbitmq.connection-timeout:123", "spring.rabbitmq.channel-rpc-timeout:140", + "spring.rabbitmq.max-inbound-message-body-size:128MB") .run((context) -> { CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class); assertThat(connectionFactory.getHost()).isEqualTo("remote-server"); @@ -172,6 +176,7 @@ class RabbitAutoConfigurationTests { assertThat(rcf.getConnectionTimeout()).isEqualTo(123); assertThat(rcf.getChannelRpcTimeout()).isEqualTo(140); assertThat((List
) ReflectionTestUtils.getField(connectionFactory, "addresses")).hasSize(1); + assertThat(rcf).hasFieldOrPropertyWithValue("maxInboundMessageBodySize", 1024 * 1024 * 128); }); } From 4e5f16f2bc9f484eea1ec54b5800b0dcfe22c77c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 27 Sep 2023 19:21:17 +0100 Subject: [PATCH 2/2] Polish "Add config prop for Rabbit's max inbound message body size" See gh-37603 --- .../amqp/RabbitConnectionFactoryBeanConfigurer.java | 4 +++- .../boot/autoconfigure/amqp/RabbitProperties.java | 4 ++-- .../autoconfigure/amqp/RabbitAutoConfigurationTests.java | 5 ++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitConnectionFactoryBeanConfigurer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitConnectionFactoryBeanConfigurer.java index 5e839306d5..607ac96462 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitConnectionFactoryBeanConfigurer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitConnectionFactoryBeanConfigurer.java @@ -26,6 +26,7 @@ import org.springframework.boot.autoconfigure.amqp.RabbitConnectionDetails.Addre import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.core.io.ResourceLoader; import org.springframework.util.Assert; +import org.springframework.util.unit.DataSize; /** * Configures {@link RabbitConnectionFactoryBean} with sensible defaults. @@ -135,7 +136,8 @@ public class RabbitConnectionFactoryBeanConfigurer { map.from(this.credentialsRefreshService).whenNonNull().to(factory::setCredentialsRefreshService); map.from(this.rabbitProperties.getMaxInboundMessageBodySize()) .whenNonNull() - .to((mimbs) -> factory.setMaxInboundMessageBodySize(Math.toIntExact(mimbs.toBytes()))); + .asInt(DataSize::toBytes) + .to(factory::setMaxInboundMessageBodySize); } } 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 bd92568080..dd9aae0800 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 @@ -132,9 +132,9 @@ public class RabbitProperties { private Duration channelRpcTimeout = Duration.ofMinutes(10); /** - * Maximum body size of inbound (received) messages in bytes. + * Maximum size of the body of inbound (received) messages. */ - private DataSize maxInboundMessageBodySize; + private DataSize maxInboundMessageBodySize = DataSize.ofMegabytes(64); /** * Cache configuration. 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 a66472cf9d..c6c32c7daa 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 @@ -150,9 +150,8 @@ class RabbitAutoConfigurationTests { com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context); assertThat(rabbitConnectionFactory.getUsername()).isEqualTo(properties.getUsername()); assertThat(rabbitConnectionFactory.getPassword()).isEqualTo(properties.getPassword()); - com.rabbitmq.client.ConnectionFactory defaultCf = new com.rabbitmq.client.ConnectionFactory(); - assertThat(rabbitConnectionFactory).hasFieldOrPropertyWithValue("maxInboundMessageBodySize", - ReflectionTestUtils.getField(defaultCf, "maxInboundMessageBodySize")); + assertThat(rabbitConnectionFactory).extracting("maxInboundMessageBodySize") + .isEqualTo((int) properties.getMaxInboundMessageBodySize().toBytes()); }); }