Skip to content

Commit d5395c9

Browse files
committed
Merge pull request #37603 from garyrussell
* gh-37603: Polish "Add config prop for Rabbit's max inbound message body size" Add config prop for Rabbit's max inbound message body size Closes gh-37603
2 parents f9b4a1e + 4e5f16f commit d5395c9

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitConnectionFactoryBeanConfigurer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.context.properties.PropertyMapper;
2727
import org.springframework.core.io.ResourceLoader;
2828
import org.springframework.util.Assert;
29+
import org.springframework.util.unit.DataSize;
2930

3031
/**
3132
* Configures {@link RabbitConnectionFactoryBean} with sensible defaults.
@@ -133,6 +134,10 @@ public void configure(RabbitConnectionFactoryBean factory) {
133134
.to(factory::setChannelRpcTimeout);
134135
map.from(this.credentialsProvider).whenNonNull().to(factory::setCredentialsProvider);
135136
map.from(this.credentialsRefreshService).whenNonNull().to(factory::setCredentialsRefreshService);
137+
map.from(this.rabbitProperties.getMaxInboundMessageBodySize())
138+
.whenNonNull()
139+
.asInt(DataSize::toBytes)
140+
.to(factory::setMaxInboundMessageBodySize);
136141
}
137142

138143
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.boot.convert.DurationUnit;
3232
import org.springframework.util.CollectionUtils;
3333
import org.springframework.util.StringUtils;
34+
import org.springframework.util.unit.DataSize;
3435

3536
/**
3637
* Configuration properties for Rabbit.
@@ -130,6 +131,11 @@ public class RabbitProperties {
130131
*/
131132
private Duration channelRpcTimeout = Duration.ofMinutes(10);
132133

134+
/**
135+
* Maximum size of the body of inbound (received) messages.
136+
*/
137+
private DataSize maxInboundMessageBodySize = DataSize.ofMegabytes(64);
138+
133139
/**
134140
* Cache configuration.
135141
*/
@@ -360,6 +366,14 @@ public void setChannelRpcTimeout(Duration channelRpcTimeout) {
360366
this.channelRpcTimeout = channelRpcTimeout;
361367
}
362368

369+
public DataSize getMaxInboundMessageBodySize() {
370+
return this.maxInboundMessageBodySize;
371+
}
372+
373+
public void setMaxInboundMessageBodySize(DataSize maxInboundMessageBodySize) {
374+
this.maxInboundMessageBodySize = maxInboundMessageBodySize;
375+
}
376+
363377
public Cache getCache() {
364378
return this.cache;
365379
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ void testDefaultConnectionFactoryConfiguration() {
150150
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context);
151151
assertThat(rabbitConnectionFactory.getUsername()).isEqualTo(properties.getUsername());
152152
assertThat(rabbitConnectionFactory.getPassword()).isEqualTo(properties.getPassword());
153+
assertThat(rabbitConnectionFactory).extracting("maxInboundMessageBodySize")
154+
.isEqualTo((int) properties.getMaxInboundMessageBodySize().toBytes());
153155
});
154156
}
155157

@@ -160,7 +162,8 @@ void testConnectionFactoryWithOverrides() {
160162
.withPropertyValues("spring.rabbitmq.host:remote-server", "spring.rabbitmq.port:9000",
161163
"spring.rabbitmq.address-shuffle-mode=random", "spring.rabbitmq.username:alice",
162164
"spring.rabbitmq.password:secret", "spring.rabbitmq.virtual_host:/vhost",
163-
"spring.rabbitmq.connection-timeout:123", "spring.rabbitmq.channel-rpc-timeout:140")
165+
"spring.rabbitmq.connection-timeout:123", "spring.rabbitmq.channel-rpc-timeout:140",
166+
"spring.rabbitmq.max-inbound-message-body-size:128MB")
164167
.run((context) -> {
165168
CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
166169
assertThat(connectionFactory.getHost()).isEqualTo("remote-server");
@@ -172,6 +175,7 @@ void testConnectionFactoryWithOverrides() {
172175
assertThat(rcf.getConnectionTimeout()).isEqualTo(123);
173176
assertThat(rcf.getChannelRpcTimeout()).isEqualTo(140);
174177
assertThat((List<Address>) ReflectionTestUtils.getField(connectionFactory, "addresses")).hasSize(1);
178+
assertThat(rcf).hasFieldOrPropertyWithValue("maxInboundMessageBodySize", 1024 * 1024 * 128);
175179
});
176180
}
177181

0 commit comments

Comments
 (0)