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<ContainerCustomizer<SimpleMessageListenerContainer>> simpleContainerCustomizer;
private final ObjectProvider<ContainerCustomizer<DirectMessageListenerContainer>> directContainerCustomizer;
private final RabbitProperties properties;
RabbitAnnotationDrivenConfiguration(ObjectProvider<MessageConverter> messageConverter,
ObjectProvider<MessageRecoverer> messageRecoverer,
ObjectProvider<RabbitRetryTemplateCustomizer> retryTemplateCustomizers,
ObjectProvider<ContainerCustomizer<SimpleMessageListenerContainer>> simpleContainerCustomizer,
ObjectProvider<ContainerCustomizer<DirectMessageListenerContainer>> directContainerCustomizer,
RabbitProperties properties) {
ObjectProvider<RabbitRetryTemplateCustomizer> 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<ContainerCustomizer<SimpleMessageListenerContainer>> 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<ContainerCustomizer<DirectMessageListenerContainer>> directContainerCustomizer) {
DirectRabbitListenerContainerFactory factory = new DirectRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
this.directContainerCustomizer.ifUnique(factory::setContainerCustomizer);
directContainerCustomizer.ifUnique(factory::setContainerCustomizer);
return factory;
}

@ -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<SimpleMessageListenerContainer> 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<DirectMessageListenerContainer> customizer() {
return mock(ContainerCustomizer.class);
}
}
static class TestListener {
@RabbitListener(queues = "test", autoStartup = "false")
void listen(String in) {
}
@Bean
ContainerCustomizer<DirectMessageListenerContainer> customizer() {
return (container) -> this.customizerCalled = true;
}
}
}

Loading…
Cancel
Save