From 0ed9085ba2a2c35a6bdc593b249250c1124929d3 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 11 Aug 2021 11:19:04 +0100 Subject: [PATCH] Polish "Apply container customizer bean to AMQP MessageListenerContainer" See gh-27625 --- .../RabbitAnnotationDrivenConfiguration.java | 21 +++------ .../amqp/RabbitAutoConfigurationTests.java | 45 ++++++++++--------- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAnnotationDrivenConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAnnotationDrivenConfiguration.java index c959a22b18..3a2a6bceb7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAnnotationDrivenConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAnnotationDrivenConfiguration.java @@ -51,23 +51,14 @@ class RabbitAnnotationDrivenConfiguration { private final ObjectProvider retryTemplateCustomizers; - private final ObjectProvider> simpleContainerCustomizer; - - private final ObjectProvider> directContainerCustomizer; - private final RabbitProperties properties; RabbitAnnotationDrivenConfiguration(ObjectProvider messageConverter, ObjectProvider messageRecoverer, - ObjectProvider retryTemplateCustomizers, - ObjectProvider> simpleContainerCustomizer, - ObjectProvider> directContainerCustomizer, - RabbitProperties properties) { + ObjectProvider retryTemplateCustomizers, RabbitProperties properties) { this.messageConverter = messageConverter; this.messageRecoverer = messageRecoverer; this.retryTemplateCustomizers = retryTemplateCustomizers; - this.simpleContainerCustomizer = simpleContainerCustomizer; - this.directContainerCustomizer = directContainerCustomizer; this.properties = properties; } @@ -88,10 +79,11 @@ class RabbitAnnotationDrivenConfiguration { @ConditionalOnProperty(prefix = "spring.rabbitmq.listener", name = "type", havingValue = "simple", matchIfMissing = true) SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory( - SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) { + SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory, + ObjectProvider> simpleContainerCustomizer) { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); configurer.configure(factory, connectionFactory); - this.simpleContainerCustomizer.ifUnique(factory::setContainerCustomizer); + simpleContainerCustomizer.ifUnique(factory::setContainerCustomizer); return factory; } @@ -111,10 +103,11 @@ class RabbitAnnotationDrivenConfiguration { @ConditionalOnMissingBean(name = "rabbitListenerContainerFactory") @ConditionalOnProperty(prefix = "spring.rabbitmq.listener", name = "type", havingValue = "direct") DirectRabbitListenerContainerFactory directRabbitListenerContainerFactory( - DirectRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) { + DirectRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory, + ObjectProvider> directContainerCustomizer) { DirectRabbitListenerContainerFactory factory = new DirectRabbitListenerContainerFactory(); configurer.configure(factory, connectionFactory); - this.directContainerCustomizer.ifUnique(factory::setContainerCustomizer); + directContainerCustomizer.ifUnique(factory::setContainerCustomizer); return factory; } 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 31a523435c..db625a906a 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 @@ -64,6 +64,7 @@ import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Primary; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; @@ -78,6 +79,7 @@ import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; @@ -854,17 +856,20 @@ class RabbitAutoConfigurationTests { } @Test - void simpleContainerCustomizer() { - this.contextRunner.withUserConfiguration(SimpleContainerCustomizerConfiguration.class).run( - (context) -> assertThat(context.getBean(SimpleContainerCustomizerConfiguration.class).customizerCalled) - .isTrue()); + @SuppressWarnings("unchecked") + void whenASimpleContainerCustomizerIsDefinedThenItIsCalledToConfigureTheContainer() { + this.contextRunner.withUserConfiguration(SimpleContainerCustomizerConfiguration.class) + .run((context) -> verify(context.getBean(ContainerCustomizer.class)) + .configure(any(SimpleMessageListenerContainer.class))); } @Test - void directContainerCustomizer() { + @SuppressWarnings("unchecked") + void whenADirectContainerCustomizerIsDefinedThenItIsCalledToConfigureTheContainer() { this.contextRunner.withUserConfiguration(DirectContainerCustomizerConfiguration.class) - .withPropertyValues("spring.rabbitmq.listener.type:direct").run((context) -> assertThat( - context.getBean(DirectContainerCustomizerConfiguration.class).customizerCalled).isTrue()); + .withPropertyValues("spring.rabbitmq.listener.type:direct") + .run((context) -> verify(context.getBean(ContainerCustomizer.class)) + .configure(any(DirectMessageListenerContainer.class))); } private com.rabbitmq.client.ConnectionFactory getTargetConnectionFactory(AssertableApplicationContext context) { @@ -1131,36 +1136,36 @@ class RabbitAutoConfigurationTests { } + @Import(TestListener.class) @Configuration(proxyBeanMethods = false) static class SimpleContainerCustomizerConfiguration { - boolean customizerCalled; - - @RabbitListener(queues = "test", autoStartup = "false") - void listen(String in) { - } - @Bean + @SuppressWarnings("unchecked") ContainerCustomizer customizer() { - return (container) -> this.customizerCalled = true; + return mock(ContainerCustomizer.class); } } + @Import(TestListener.class) @Configuration(proxyBeanMethods = false) static class DirectContainerCustomizerConfiguration { - boolean customizerCalled; + @Bean + @SuppressWarnings("unchecked") + ContainerCustomizer customizer() { + return mock(ContainerCustomizer.class); + } + + } + + static class TestListener { @RabbitListener(queues = "test", autoStartup = "false") void listen(String in) { } - @Bean - ContainerCustomizer customizer() { - return (container) -> this.customizerCalled = true; - } - } }