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
pull/37626/head
Andy Wilkinson 1 year ago
commit d5395c97c0

@ -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.
@ -133,6 +134,10 @@ 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()
.asInt(DataSize::toBytes)
.to(factory::setMaxInboundMessageBodySize);
}
}

@ -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 size of the body of inbound (received) messages.
*/
private DataSize maxInboundMessageBodySize = DataSize.ofMegabytes(64);
/**
* 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;
}

@ -150,6 +150,8 @@ class RabbitAutoConfigurationTests {
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context);
assertThat(rabbitConnectionFactory.getUsername()).isEqualTo(properties.getUsername());
assertThat(rabbitConnectionFactory.getPassword()).isEqualTo(properties.getPassword());
assertThat(rabbitConnectionFactory).extracting("maxInboundMessageBodySize")
.isEqualTo((int) properties.getMaxInboundMessageBodySize().toBytes());
});
}
@ -160,7 +162,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 +175,7 @@ class RabbitAutoConfigurationTests {
assertThat(rcf.getConnectionTimeout()).isEqualTo(123);
assertThat(rcf.getChannelRpcTimeout()).isEqualTo(140);
assertThat((List<Address>) ReflectionTestUtils.getField(connectionFactory, "addresses")).hasSize(1);
assertThat(rcf).hasFieldOrPropertyWithValue("maxInboundMessageBodySize", 1024 * 1024 * 128);
});
}

Loading…
Cancel
Save