From 17e12ea025a6d2994d6038e2adf2a76780b87260 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 21 Oct 2020 08:49:34 +0200 Subject: [PATCH] Polish "Add configuration options for RabbitMQ's batch listener config" See gh-23766 --- .../autoconfigure/amqp/RabbitProperties.java | 14 ++++++---- .../amqp/RabbitAutoConfigurationTests.java | 28 ++++++++++++++----- 2 files changed, 29 insertions(+), 13 deletions(-) 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 07b861d21d..7511b37240 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 @@ -663,7 +663,8 @@ public class RabbitProperties { private Duration idleEventInterval; /** - * Whether to present batched messages (created by a BatchingRabbitTemplate) as discrete messages. + * Whether the container should present batched messages as discrete messages or + * call the listener with the batch. */ private boolean deBatchingEnabled = true; @@ -715,7 +716,7 @@ public class RabbitProperties { public abstract boolean isMissingQueuesFatal(); public boolean isDeBatchingEnabled() { - return deBatchingEnabled; + return this.deBatchingEnabled; } public void setDeBatchingEnabled(boolean deBatchingEnabled) { @@ -757,9 +758,10 @@ public class RabbitProperties { private boolean missingQueuesFatal = true; /** - * When true, the container will create a batch of messages based on the 'receiveTimeout' and 'batchSize'. - * Coerces 'deBatchingEnabled' to true to include the contents of a producer created batch in the batch as - * discrete records. + * Whether the container creates a batch of messages based on the + * 'receive-timeout' and 'batch-size'. Coerces 'de-batching-enabled' to true to + * include the contents of a producer created batch in the batch as discrete + * records. */ private boolean consumerBatchEnabled; @@ -797,7 +799,7 @@ public class RabbitProperties { } public boolean isConsumerBatchEnabled() { - return consumerBatchEnabled; + return this.consumerBatchEnabled; } public void setConsumerBatchEnabled(boolean consumerBatchEnabled) { 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 efca9ee97a..0904cf507d 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 @@ -33,8 +33,9 @@ import com.rabbitmq.client.impl.CredentialsRefreshService; import com.rabbitmq.client.impl.DefaultCredentialsProvider; import org.aopalliance.aop.Advice; import org.junit.jupiter.api.Test; - import org.mockito.InOrder; +import org.mockito.Mockito; + import org.springframework.amqp.core.AcknowledgeMode; import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.Message; @@ -76,7 +77,8 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; /** * Tests for {@link RabbitAutoConfiguration}. @@ -538,7 +540,22 @@ class RabbitAutoConfigurationTests { .withPropertyValues("spring.rabbitmq.listener.type:direct", "spring.rabbitmq.listener.simple.concurrency:5", "spring.rabbitmq.listener.simple.maxConcurrency:10", - "spring.rabbitmq.listener.simple.prefetch:40", + "spring.rabbitmq.listener.simple.prefetch:40") + .run((context) -> { + SimpleRabbitListenerContainerFactoryConfigurer configurer = context + .getBean(SimpleRabbitListenerContainerFactoryConfigurer.class); + SimpleRabbitListenerContainerFactory factory = mock(SimpleRabbitListenerContainerFactory.class); + configurer.configure(factory, mock(ConnectionFactory.class)); + verify(factory).setConcurrentConsumers(5); + verify(factory).setMaxConcurrentConsumers(10); + verify(factory).setPrefetchCount(40); + }); + } + + @Test + void testSimpleRabbitListenerContainerFactoryConfigurerEnableDeBatchingWithConsumerBatchEnabled() { + this.contextRunner.withUserConfiguration(TestConfiguration.class) + .withPropertyValues("spring.rabbitmq.listener.type:direct", "spring.rabbitmq.listener.simple.consumer-batch-enabled:true", "spring.rabbitmq.listener.simple.de-batching-enabled:false") .run((context) -> { @@ -546,10 +563,7 @@ class RabbitAutoConfigurationTests { .getBean(SimpleRabbitListenerContainerFactoryConfigurer.class); SimpleRabbitListenerContainerFactory factory = mock(SimpleRabbitListenerContainerFactory.class); configurer.configure(factory, mock(ConnectionFactory.class)); - InOrder inOrder = inOrder(factory); - verify(factory).setConcurrentConsumers(5); - verify(factory).setMaxConcurrentConsumers(10); - verify(factory).setPrefetchCount(40); + InOrder inOrder = Mockito.inOrder(factory); verify(factory).setConsumerBatchEnabled(true); inOrder.verify(factory).setDeBatchingEnabled(false); inOrder.verify(factory).setDeBatchingEnabled(true);