Polish "Apply container customizer bean to AMQP MessageListenerContainer"

See gh-27625
pull/27659/head
Andy Wilkinson 3 years ago
parent 774941e958
commit 0ed9085ba2

@ -51,23 +51,14 @@ class RabbitAnnotationDrivenConfiguration {
private final ObjectProvider<RabbitRetryTemplateCustomizer> retryTemplateCustomizers; private final ObjectProvider<RabbitRetryTemplateCustomizer> retryTemplateCustomizers;
private final ObjectProvider<ContainerCustomizer<SimpleMessageListenerContainer>> simpleContainerCustomizer;
private final ObjectProvider<ContainerCustomizer<DirectMessageListenerContainer>> directContainerCustomizer;
private final RabbitProperties properties; private final RabbitProperties properties;
RabbitAnnotationDrivenConfiguration(ObjectProvider<MessageConverter> messageConverter, RabbitAnnotationDrivenConfiguration(ObjectProvider<MessageConverter> messageConverter,
ObjectProvider<MessageRecoverer> messageRecoverer, ObjectProvider<MessageRecoverer> messageRecoverer,
ObjectProvider<RabbitRetryTemplateCustomizer> retryTemplateCustomizers, ObjectProvider<RabbitRetryTemplateCustomizer> retryTemplateCustomizers, RabbitProperties properties) {
ObjectProvider<ContainerCustomizer<SimpleMessageListenerContainer>> simpleContainerCustomizer,
ObjectProvider<ContainerCustomizer<DirectMessageListenerContainer>> directContainerCustomizer,
RabbitProperties properties) {
this.messageConverter = messageConverter; this.messageConverter = messageConverter;
this.messageRecoverer = messageRecoverer; this.messageRecoverer = messageRecoverer;
this.retryTemplateCustomizers = retryTemplateCustomizers; this.retryTemplateCustomizers = retryTemplateCustomizers;
this.simpleContainerCustomizer = simpleContainerCustomizer;
this.directContainerCustomizer = directContainerCustomizer;
this.properties = properties; this.properties = properties;
} }
@ -88,10 +79,11 @@ class RabbitAnnotationDrivenConfiguration {
@ConditionalOnProperty(prefix = "spring.rabbitmq.listener", name = "type", havingValue = "simple", @ConditionalOnProperty(prefix = "spring.rabbitmq.listener", name = "type", havingValue = "simple",
matchIfMissing = true) matchIfMissing = true)
SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory( SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(
SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) { SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory,
ObjectProvider<ContainerCustomizer<SimpleMessageListenerContainer>> simpleContainerCustomizer) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory); configurer.configure(factory, connectionFactory);
this.simpleContainerCustomizer.ifUnique(factory::setContainerCustomizer); simpleContainerCustomizer.ifUnique(factory::setContainerCustomizer);
return factory; return factory;
} }
@ -111,10 +103,11 @@ class RabbitAnnotationDrivenConfiguration {
@ConditionalOnMissingBean(name = "rabbitListenerContainerFactory") @ConditionalOnMissingBean(name = "rabbitListenerContainerFactory")
@ConditionalOnProperty(prefix = "spring.rabbitmq.listener", name = "type", havingValue = "direct") @ConditionalOnProperty(prefix = "spring.rabbitmq.listener", name = "type", havingValue = "direct")
DirectRabbitListenerContainerFactory directRabbitListenerContainerFactory( DirectRabbitListenerContainerFactory directRabbitListenerContainerFactory(
DirectRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) { DirectRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory,
ObjectProvider<ContainerCustomizer<DirectMessageListenerContainer>> directContainerCustomizer) {
DirectRabbitListenerContainerFactory factory = new DirectRabbitListenerContainerFactory(); DirectRabbitListenerContainerFactory factory = new DirectRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory); configurer.configure(factory, connectionFactory);
this.directContainerCustomizer.ifUnique(factory::setContainerCustomizer); directContainerCustomizer.ifUnique(factory::setContainerCustomizer);
return factory; return factory;
} }

@ -64,6 +64,7 @@ import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; 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.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; 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.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.ArgumentMatchers.isNull;
@ -854,17 +856,20 @@ class RabbitAutoConfigurationTests {
} }
@Test @Test
void simpleContainerCustomizer() { @SuppressWarnings("unchecked")
this.contextRunner.withUserConfiguration(SimpleContainerCustomizerConfiguration.class).run( void whenASimpleContainerCustomizerIsDefinedThenItIsCalledToConfigureTheContainer() {
(context) -> assertThat(context.getBean(SimpleContainerCustomizerConfiguration.class).customizerCalled) this.contextRunner.withUserConfiguration(SimpleContainerCustomizerConfiguration.class)
.isTrue()); .run((context) -> verify(context.getBean(ContainerCustomizer.class))
.configure(any(SimpleMessageListenerContainer.class)));
} }
@Test @Test
void directContainerCustomizer() { @SuppressWarnings("unchecked")
void whenADirectContainerCustomizerIsDefinedThenItIsCalledToConfigureTheContainer() {
this.contextRunner.withUserConfiguration(DirectContainerCustomizerConfiguration.class) this.contextRunner.withUserConfiguration(DirectContainerCustomizerConfiguration.class)
.withPropertyValues("spring.rabbitmq.listener.type:direct").run((context) -> assertThat( .withPropertyValues("spring.rabbitmq.listener.type:direct")
context.getBean(DirectContainerCustomizerConfiguration.class).customizerCalled).isTrue()); .run((context) -> verify(context.getBean(ContainerCustomizer.class))
.configure(any(DirectMessageListenerContainer.class)));
} }
private com.rabbitmq.client.ConnectionFactory getTargetConnectionFactory(AssertableApplicationContext context) { private com.rabbitmq.client.ConnectionFactory getTargetConnectionFactory(AssertableApplicationContext context) {
@ -1131,36 +1136,36 @@ class RabbitAutoConfigurationTests {
} }
@Import(TestListener.class)
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class SimpleContainerCustomizerConfiguration { static class SimpleContainerCustomizerConfiguration {
boolean customizerCalled;
@RabbitListener(queues = "test", autoStartup = "false")
void listen(String in) {
}
@Bean @Bean
@SuppressWarnings("unchecked")
ContainerCustomizer<SimpleMessageListenerContainer> customizer() { ContainerCustomizer<SimpleMessageListenerContainer> customizer() {
return (container) -> this.customizerCalled = true; return mock(ContainerCustomizer.class);
} }
} }
@Import(TestListener.class)
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class DirectContainerCustomizerConfiguration { static class DirectContainerCustomizerConfiguration {
boolean customizerCalled; @Bean
@SuppressWarnings("unchecked")
ContainerCustomizer<DirectMessageListenerContainer> customizer() {
return mock(ContainerCustomizer.class);
}
}
static class TestListener {
@RabbitListener(queues = "test", autoStartup = "false") @RabbitListener(queues = "test", autoStartup = "false")
void listen(String in) { void listen(String in) {
} }
@Bean
ContainerCustomizer<DirectMessageListenerContainer> customizer() {
return (container) -> this.customizerCalled = true;
}
} }
} }

Loading…
Cancel
Save