Skip to content

Commit 954f562

Browse files
garyrussellwilkinsona
authored andcommitted
Add config prop for Rabbit's max inbound message body size
See gh-37603
1 parent f9b4a1e commit 954f562

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ public void configure(RabbitConnectionFactoryBean factory) {
133133
.to(factory::setChannelRpcTimeout);
134134
map.from(this.credentialsProvider).whenNonNull().to(factory::setCredentialsProvider);
135135
map.from(this.credentialsRefreshService).whenNonNull().to(factory::setCredentialsRefreshService);
136+
map.from(this.rabbitProperties.getMaxInboundMessageBodySize())
137+
.whenNonNull()
138+
.to((mimbs) -> factory.setMaxInboundMessageBodySize(Math.toIntExact(mimbs.toBytes())));
136139
}
137140

138141
}

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 body size of inbound (received) messages in bytes.
136+
*/
137+
private DataSize maxInboundMessageBodySize;
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ 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+
com.rabbitmq.client.ConnectionFactory defaultCf = new com.rabbitmq.client.ConnectionFactory();
154+
assertThat(rabbitConnectionFactory).hasFieldOrPropertyWithValue("maxInboundMessageBodySize",
155+
ReflectionTestUtils.getField(defaultCf, "maxInboundMessageBodySize"));
153156
});
154157
}
155158

@@ -160,7 +163,8 @@ void testConnectionFactoryWithOverrides() {
160163
.withPropertyValues("spring.rabbitmq.host:remote-server", "spring.rabbitmq.port:9000",
161164
"spring.rabbitmq.address-shuffle-mode=random", "spring.rabbitmq.username:alice",
162165
"spring.rabbitmq.password:secret", "spring.rabbitmq.virtual_host:/vhost",
163-
"spring.rabbitmq.connection-timeout:123", "spring.rabbitmq.channel-rpc-timeout:140")
166+
"spring.rabbitmq.connection-timeout:123", "spring.rabbitmq.channel-rpc-timeout:140",
167+
"spring.rabbitmq.max-inbound-message-body-size:128MB")
164168
.run((context) -> {
165169
CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
166170
assertThat(connectionFactory.getHost()).isEqualTo("remote-server");
@@ -172,6 +176,7 @@ void testConnectionFactoryWithOverrides() {
172176
assertThat(rcf.getConnectionTimeout()).isEqualTo(123);
173177
assertThat(rcf.getChannelRpcTimeout()).isEqualTo(140);
174178
assertThat((List<Address>) ReflectionTestUtils.getField(connectionFactory, "addresses")).hasSize(1);
179+
assertThat(rcf).hasFieldOrPropertyWithValue("maxInboundMessageBodySize", 1024 * 1024 * 128);
175180
});
176181
}
177182

0 commit comments

Comments
 (0)