Skip to content

Commit 5b25a37

Browse files
committed
Polish "Add Pulsar container factory customizer infrastructure"
See gh-42182
1 parent 5cbe0e8 commit 5b25a37

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarAutoConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private void applyConsumerBuilderCustomizers(List<ConsumerBuilderCustomizer<?>>
178178
ConcurrentPulsarListenerContainerFactory<?> pulsarListenerContainerFactory(
179179
PulsarConsumerFactory<Object> pulsarConsumerFactory, SchemaResolver schemaResolver,
180180
TopicResolver topicResolver, ObjectProvider<PulsarAwareTransactionManager> pulsarTransactionManager,
181-
PulsarContainerFactoryCustomizers containerFactoryCustomizers, Environment environment) {
181+
Environment environment, PulsarContainerFactoryCustomizers containerFactoryCustomizers) {
182182
PulsarContainerProperties containerProperties = new PulsarContainerProperties();
183183
containerProperties.setSchemaResolver(schemaResolver);
184184
containerProperties.setTopicResolver(topicResolver);
@@ -218,8 +218,8 @@ private void applyReaderBuilderCustomizers(List<ReaderBuilderCustomizer<?>> cust
218218
@Bean
219219
@ConditionalOnMissingBean(name = "pulsarReaderContainerFactory")
220220
DefaultPulsarReaderContainerFactory<?> pulsarReaderContainerFactory(PulsarReaderFactory<?> pulsarReaderFactory,
221-
SchemaResolver schemaResolver, PulsarContainerFactoryCustomizers containerFactoryCustomizers,
222-
Environment environment) {
221+
SchemaResolver schemaResolver, Environment environment,
222+
PulsarContainerFactoryCustomizers containerFactoryCustomizers) {
223223
PulsarReaderContainerProperties readerContainerProperties = new PulsarReaderContainerProperties();
224224
readerContainerProperties.setSchemaResolver(schemaResolver);
225225
if (Threading.VIRTUAL.isActive(environment)) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarAutoConfigurationTests.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
21-
import java.util.Objects;
2221
import java.util.concurrent.ThreadFactory;
2322
import java.util.concurrent.TimeUnit;
2423

@@ -600,8 +599,8 @@ static class ListenerContainerFactoryCustomizersConfig {
600599
@Bean
601600
@Order(50)
602601
PulsarContainerFactoryCustomizer<DefaultReactivePulsarListenerContainerFactory<?>> customizerIgnored() {
603-
return (__) -> {
604-
throw new RuntimeException("should-not-have-matched");
602+
return (containerFactory) -> {
603+
throw new IllegalStateException("should-not-have-matched");
605604
};
606605
}
607606

@@ -619,8 +618,9 @@ PulsarContainerFactoryCustomizer<ConcurrentPulsarListenerContainerFactory<?>> cu
619618

620619
private void appendToSubscriptionName(ConcurrentPulsarListenerContainerFactory<?> containerFactory,
621620
String valueToAppend) {
622-
String name = Objects.toString(containerFactory.getContainerProperties().getSubscriptionName(), "");
623-
containerFactory.getContainerProperties().setSubscriptionName(name.concat(valueToAppend));
621+
String subscriptionName = containerFactory.getContainerProperties().getSubscriptionName();
622+
String updatedValue = (subscriptionName != null) ? subscriptionName + valueToAppend : valueToAppend;
623+
containerFactory.getContainerProperties().setSubscriptionName(updatedValue);
624624
}
625625

626626
}
@@ -724,8 +724,8 @@ static class ReaderContainerFactoryCustomizersConfig {
724724
@Bean
725725
@Order(50)
726726
PulsarContainerFactoryCustomizer<DefaultReactivePulsarListenerContainerFactory<?>> customizerIgnored() {
727-
return (__) -> {
728-
throw new RuntimeException("should-not-have-matched");
727+
return (containerFactory) -> {
728+
throw new IllegalStateException("should-not-have-matched");
729729
};
730730
}
731731

@@ -743,8 +743,9 @@ PulsarContainerFactoryCustomizer<DefaultPulsarReaderContainerFactory<?>> customi
743743

744744
private void appendToReaderListener(DefaultPulsarReaderContainerFactory<?> containerFactory,
745745
String valueToAppend) {
746-
String name = Objects.toString(containerFactory.getContainerProperties().getReaderListener(), "");
747-
containerFactory.getContainerProperties().setReaderListener(name.concat(valueToAppend));
746+
Object readerListener = containerFactory.getContainerProperties().getReaderListener();
747+
String updatedValue = (readerListener != null) ? readerListener + valueToAppend : valueToAppend;
748+
containerFactory.getContainerProperties().setReaderListener(updatedValue);
748749
}
749750

750751
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarContainerFactoryCustomizersTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.List;
2222

2323
import org.junit.jupiter.api.Test;
24-
import org.mockito.BDDMockito;
2524

2625
import org.springframework.pulsar.config.ConcurrentPulsarListenerContainerFactory;
2726
import org.springframework.pulsar.config.DefaultPulsarReaderContainerFactory;
@@ -33,10 +32,11 @@
3332
import org.springframework.pulsar.reactive.config.DefaultReactivePulsarListenerContainerFactory;
3433

3534
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.mockito.BDDMockito.then;
3636
import static org.mockito.Mockito.mock;
3737

3838
/**
39-
* Unit tests for {@link PulsarContainerFactoryCustomizers}.
39+
* Tests for {@link PulsarContainerFactoryCustomizers}.
4040
*
4141
* @author Chris Bono
4242
*/
@@ -46,11 +46,11 @@ class PulsarContainerFactoryCustomizersTests {
4646
void customizeWithNullCustomizersShouldDoNothing() {
4747
PulsarContainerFactory<?, ?> containerFactory = mock(PulsarContainerFactory.class);
4848
new PulsarContainerFactoryCustomizers(null).customize(containerFactory);
49-
BDDMockito.verifyNoInteractions(containerFactory);
49+
then(containerFactory).shouldHaveNoInteractions();
5050
}
5151

52-
@SuppressWarnings("unchecked")
5352
@Test
53+
@SuppressWarnings({ "unchecked", "rawtypes" })
5454
void customizeSimplePulsarContainerFactory() {
5555
PulsarContainerFactoryCustomizers customizers = new PulsarContainerFactoryCustomizers(
5656
Collections.singletonList(new SimplePulsarContainerFactoryCustomizer()));

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarReactiveAutoConfigurationTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.time.Duration;
2020
import java.util.ArrayList;
2121
import java.util.List;
22-
import java.util.Objects;
2322
import java.util.function.Supplier;
2423

2524
import com.github.benmanes.caffeine.cache.Caffeine;
@@ -397,8 +396,8 @@ static class ListenerContainerFactoryCustomizersConfig {
397396
@Bean
398397
@Order(50)
399398
PulsarContainerFactoryCustomizer<ConcurrentPulsarListenerContainerFactory<?>> customizerIgnored() {
400-
return (__) -> {
401-
throw new RuntimeException("should-not-have-matched");
399+
return (containerFactory) -> {
400+
throw new IllegalStateException("should-not-have-matched");
402401
};
403402
}
404403

@@ -416,8 +415,9 @@ PulsarContainerFactoryCustomizer<DefaultReactivePulsarListenerContainerFactory<?
416415

417416
private void appendToSubscriptionName(DefaultReactivePulsarListenerContainerFactory<?> containerFactory,
418417
String valueToAppend) {
419-
String name = Objects.toString(containerFactory.getContainerProperties().getSubscriptionName(), "");
420-
containerFactory.getContainerProperties().setSubscriptionName(name.concat(valueToAppend));
418+
String subscriptionName = containerFactory.getContainerProperties().getSubscriptionName();
419+
String updatedValue = (subscriptionName != null) ? subscriptionName + valueToAppend : valueToAppend;
420+
containerFactory.getContainerProperties().setSubscriptionName(updatedValue);
421421
}
422422

423423
}

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/messaging/pulsar.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ include-code::MyBean[]
150150
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.
151151
You can configure these components by specifying any of the `spring.pulsar.listener.\*` and `spring.pulsar.consumer.*` prefixed application properties.
152152

153-
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.
153+
If you need more control over the configuration of the consumer factory, consider registering one or more `ConsumerBuilderCustomizer` beans.
154154
These customizers are applied to all consumers created by the factory, and therefore all `@PulsarListener` instances.
155155
You can also customize a single listener by setting the `consumerCustomizer` attribute of the `@PulsarListener` annotation.
156156

157-
If you need more control over the actual container factory configuration, consider registering one or more `PulsarContainerFactoryCustomizer<ConcurrentPulsarListenerContainerFactoryCustomizer<?>>` beans.
157+
If you need more control over the actual container factory configuration, consider registering one or more `PulsarContainerFactoryCustomizer<ConcurrentPulsarListenerContainerFactory<?>>` beans.
158158

159159
[[messaging.pulsar.receiving-reactive]]
160160
== Receiving a Message Reactively
@@ -167,7 +167,7 @@ include-code::MyBean[]
167167
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.
168168
You can configure these components by specifying any of the `spring.pulsar.listener.\*` and `spring.pulsar.consumer.*` prefixed application properties.
169169

170-
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.
170+
If you need more control over the configuration of the consumer factory, consider registering one or more `ReactiveMessageConsumerBuilderCustomizer` beans.
171171
These customizers are applied to all consumers created by the factory, and therefore all `@ReactivePulsarListener` instances.
172172
You can also customize a single listener by setting the `consumerCustomizer` attribute of the `@ReactivePulsarListener` annotation.
173173

@@ -187,7 +187,7 @@ include-code::MyBean[]
187187
The `@PulsarReader` relies on a `PulsarReaderFactory` to create the underlying Pulsar reader.
188188
Spring Boot auto-configuration provides this reader factory which can be customized by setting any of the `spring.pulsar.reader.*` prefixed application properties.
189189

190-
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.
190+
If you need more control over the configuration of the reader factory, consider registering one or more `ReaderBuilderCustomizer` beans.
191191
These customizers are applied to all readers created by the factory, and therefore all `@PulsarReader` instances.
192192
You can also customize a single listener by setting the `readerCustomizer` attribute of the `@PulsarReader` annotation.
193193

0 commit comments

Comments
 (0)