From 5b25a37a36d99c174234bcf9fb815e44662bf833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Tue, 10 Sep 2024 12:01:07 +0200 Subject: [PATCH] Polish "Add Pulsar container factory customizer infrastructure" See gh-42182 --- .../pulsar/PulsarAutoConfiguration.java | 6 +++--- .../pulsar/PulsarAutoConfigurationTests.java | 19 ++++++++++--------- ...ulsarContainerFactoryCustomizersTests.java | 8 ++++---- .../PulsarReactiveAutoConfigurationTests.java | 10 +++++----- .../reference/pages/messaging/pulsar.adoc | 8 ++++---- 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarAutoConfiguration.java index d5cf598b20..a38a95f5a5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarAutoConfiguration.java @@ -178,7 +178,7 @@ public class PulsarAutoConfiguration { ConcurrentPulsarListenerContainerFactory pulsarListenerContainerFactory( PulsarConsumerFactory pulsarConsumerFactory, SchemaResolver schemaResolver, TopicResolver topicResolver, ObjectProvider pulsarTransactionManager, - PulsarContainerFactoryCustomizers containerFactoryCustomizers, Environment environment) { + Environment environment, PulsarContainerFactoryCustomizers containerFactoryCustomizers) { PulsarContainerProperties containerProperties = new PulsarContainerProperties(); containerProperties.setSchemaResolver(schemaResolver); containerProperties.setTopicResolver(topicResolver); @@ -218,8 +218,8 @@ public class PulsarAutoConfiguration { @Bean @ConditionalOnMissingBean(name = "pulsarReaderContainerFactory") DefaultPulsarReaderContainerFactory pulsarReaderContainerFactory(PulsarReaderFactory pulsarReaderFactory, - SchemaResolver schemaResolver, PulsarContainerFactoryCustomizers containerFactoryCustomizers, - Environment environment) { + SchemaResolver schemaResolver, Environment environment, + PulsarContainerFactoryCustomizers containerFactoryCustomizers) { PulsarReaderContainerProperties readerContainerProperties = new PulsarReaderContainerProperties(); readerContainerProperties.setSchemaResolver(schemaResolver); if (Threading.VIRTUAL.isActive(environment)) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarAutoConfigurationTests.java index 6a467810fc..fdc10771c1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarAutoConfigurationTests.java @@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.pulsar; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; @@ -600,8 +599,8 @@ class PulsarAutoConfigurationTests { @Bean @Order(50) PulsarContainerFactoryCustomizer> customizerIgnored() { - return (__) -> { - throw new RuntimeException("should-not-have-matched"); + return (containerFactory) -> { + throw new IllegalStateException("should-not-have-matched"); }; } @@ -619,8 +618,9 @@ class PulsarAutoConfigurationTests { private void appendToSubscriptionName(ConcurrentPulsarListenerContainerFactory containerFactory, String valueToAppend) { - String name = Objects.toString(containerFactory.getContainerProperties().getSubscriptionName(), ""); - containerFactory.getContainerProperties().setSubscriptionName(name.concat(valueToAppend)); + String subscriptionName = containerFactory.getContainerProperties().getSubscriptionName(); + String updatedValue = (subscriptionName != null) ? subscriptionName + valueToAppend : valueToAppend; + containerFactory.getContainerProperties().setSubscriptionName(updatedValue); } } @@ -724,8 +724,8 @@ class PulsarAutoConfigurationTests { @Bean @Order(50) PulsarContainerFactoryCustomizer> customizerIgnored() { - return (__) -> { - throw new RuntimeException("should-not-have-matched"); + return (containerFactory) -> { + throw new IllegalStateException("should-not-have-matched"); }; } @@ -743,8 +743,9 @@ class PulsarAutoConfigurationTests { private void appendToReaderListener(DefaultPulsarReaderContainerFactory containerFactory, String valueToAppend) { - String name = Objects.toString(containerFactory.getContainerProperties().getReaderListener(), ""); - containerFactory.getContainerProperties().setReaderListener(name.concat(valueToAppend)); + Object readerListener = containerFactory.getContainerProperties().getReaderListener(); + String updatedValue = (readerListener != null) ? readerListener + valueToAppend : valueToAppend; + containerFactory.getContainerProperties().setReaderListener(updatedValue); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarContainerFactoryCustomizersTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarContainerFactoryCustomizersTests.java index dfc290745a..8b03635db2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarContainerFactoryCustomizersTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarContainerFactoryCustomizersTests.java @@ -21,7 +21,6 @@ import java.util.Collections; import java.util.List; import org.junit.jupiter.api.Test; -import org.mockito.BDDMockito; import org.springframework.pulsar.config.ConcurrentPulsarListenerContainerFactory; import org.springframework.pulsar.config.DefaultPulsarReaderContainerFactory; @@ -33,10 +32,11 @@ import org.springframework.pulsar.listener.PulsarContainerProperties; import org.springframework.pulsar.reactive.config.DefaultReactivePulsarListenerContainerFactory; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.mock; /** - * Unit tests for {@link PulsarContainerFactoryCustomizers}. + * Tests for {@link PulsarContainerFactoryCustomizers}. * * @author Chris Bono */ @@ -46,11 +46,11 @@ class PulsarContainerFactoryCustomizersTests { void customizeWithNullCustomizersShouldDoNothing() { PulsarContainerFactory containerFactory = mock(PulsarContainerFactory.class); new PulsarContainerFactoryCustomizers(null).customize(containerFactory); - BDDMockito.verifyNoInteractions(containerFactory); + then(containerFactory).shouldHaveNoInteractions(); } - @SuppressWarnings("unchecked") @Test + @SuppressWarnings({ "unchecked", "rawtypes" }) void customizeSimplePulsarContainerFactory() { PulsarContainerFactoryCustomizers customizers = new PulsarContainerFactoryCustomizers( Collections.singletonList(new SimplePulsarContainerFactoryCustomizer())); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarReactiveAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarReactiveAutoConfigurationTests.java index 9b2d58d000..933b9a2717 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarReactiveAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarReactiveAutoConfigurationTests.java @@ -19,7 +19,6 @@ package org.springframework.boot.autoconfigure.pulsar; import java.time.Duration; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.function.Supplier; import com.github.benmanes.caffeine.cache.Caffeine; @@ -397,8 +396,8 @@ class PulsarReactiveAutoConfigurationTests { @Bean @Order(50) PulsarContainerFactoryCustomizer> customizerIgnored() { - return (__) -> { - throw new RuntimeException("should-not-have-matched"); + return (containerFactory) -> { + throw new IllegalStateException("should-not-have-matched"); }; } @@ -416,8 +415,9 @@ class PulsarReactiveAutoConfigurationTests { private void appendToSubscriptionName(DefaultReactivePulsarListenerContainerFactory containerFactory, String valueToAppend) { - String name = Objects.toString(containerFactory.getContainerProperties().getSubscriptionName(), ""); - containerFactory.getContainerProperties().setSubscriptionName(name.concat(valueToAppend)); + String subscriptionName = containerFactory.getContainerProperties().getSubscriptionName(); + String updatedValue = (subscriptionName != null) ? subscriptionName + valueToAppend : valueToAppend; + containerFactory.getContainerProperties().setSubscriptionName(updatedValue); } } diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/messaging/pulsar.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/messaging/pulsar.adoc index bc6a8c6e38..f758ba85eb 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/messaging/pulsar.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/messaging/pulsar.adoc @@ -150,11 +150,11 @@ include-code::MyBean[] Spring Boot auto-configuration provides all the components necessary for `PulsarListener`, such as the `PulsarListenerContainerFactory` and the consumer factory it uses to construct the underlying Pulsar consumers. You can configure these components by specifying any of the `spring.pulsar.listener.\*` and `spring.pulsar.consumer.*` prefixed application properties. -If you need more control over the configuration of the consumer factory used by the container factory to create consumers, consider registering one or more `ConsumerBuilderCustomizer` beans. +If you need more control over the configuration of the consumer factory, consider registering one or more `ConsumerBuilderCustomizer` beans. These customizers are applied to all consumers created by the factory, and therefore all `@PulsarListener` instances. You can also customize a single listener by setting the `consumerCustomizer` attribute of the `@PulsarListener` annotation. -If you need more control over the actual container factory configuration, consider registering one or more `PulsarContainerFactoryCustomizer>` beans. +If you need more control over the actual container factory configuration, consider registering one or more `PulsarContainerFactoryCustomizer>` beans. [[messaging.pulsar.receiving-reactive]] == Receiving a Message Reactively @@ -167,7 +167,7 @@ include-code::MyBean[] Spring Boot auto-configuration provides all the components necessary for `ReactivePulsarListener`, such as the `ReactivePulsarListenerContainerFactory` and the consumer factory it uses to construct the underlying reactive Pulsar consumers. You can configure these components by specifying any of the `spring.pulsar.listener.\*` and `spring.pulsar.consumer.*` prefixed application properties. -If you need more control over the configuration of the consumer factory used by the container factory to create consumers, consider registering one or more `ReactiveMessageConsumerBuilderCustomizer` beans. +If you need more control over the configuration of the consumer factory, consider registering one or more `ReactiveMessageConsumerBuilderCustomizer` beans. These customizers are applied to all consumers created by the factory, and therefore all `@ReactivePulsarListener` instances. You can also customize a single listener by setting the `consumerCustomizer` attribute of the `@ReactivePulsarListener` annotation. @@ -187,7 +187,7 @@ include-code::MyBean[] The `@PulsarReader` relies on a `PulsarReaderFactory` to create the underlying Pulsar reader. Spring Boot auto-configuration provides this reader factory which can be customized by setting any of the `spring.pulsar.reader.*` prefixed application properties. -If you need more control over the configuration of the reader factory used by the container factory to create readers, consider registering one or more `ReaderBuilderCustomizer` beans. +If you need more control over the configuration of the reader factory, consider registering one or more `ReaderBuilderCustomizer` beans. These customizers are applied to all readers created by the factory, and therefore all `@PulsarReader` instances. You can also customize a single listener by setting the `readerCustomizer` attribute of the `@PulsarReader` annotation.