Skip to content

Commit ce0714e

Browse files
committed
Switch to single generic customizer contract
1 parent cf2a94c commit ce0714e

File tree

12 files changed

+264
-99
lines changed

12 files changed

+264
-99
lines changed

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ private void applyConsumerBuilderCustomizers(List<ConsumerBuilderCustomizer<?>>
178178
ConcurrentPulsarListenerContainerFactory<?> pulsarListenerContainerFactory(
179179
PulsarConsumerFactory<Object> pulsarConsumerFactory, SchemaResolver schemaResolver,
180180
TopicResolver topicResolver, ObjectProvider<PulsarAwareTransactionManager> pulsarTransactionManager,
181-
ObjectProvider<ConcurrentPulsarListenerContainerFactoryCustomizer> customizersProvider,
182-
Environment environment) {
181+
PulsarContainerFactoryCustomizers containerFactoryCustomizers, Environment environment) {
183182
PulsarContainerProperties containerProperties = new PulsarContainerProperties();
184183
containerProperties.setSchemaResolver(schemaResolver);
185184
containerProperties.setTopicResolver(topicResolver);
@@ -190,7 +189,7 @@ ConcurrentPulsarListenerContainerFactory<?> pulsarListenerContainerFactory(
190189
this.propertiesMapper.customizeContainerProperties(containerProperties);
191190
ConcurrentPulsarListenerContainerFactory<?> containerFactory = new ConcurrentPulsarListenerContainerFactory<>(
192191
pulsarConsumerFactory, containerProperties);
193-
customizersProvider.orderedStream().forEachOrdered((customizer) -> customizer.customize(containerFactory));
192+
containerFactoryCustomizers.customize(containerFactory);
194193
return containerFactory;
195194
}
196195

@@ -219,8 +218,7 @@ private void applyReaderBuilderCustomizers(List<ReaderBuilderCustomizer<?>> cust
219218
@Bean
220219
@ConditionalOnMissingBean(name = "pulsarReaderContainerFactory")
221220
DefaultPulsarReaderContainerFactory<?> pulsarReaderContainerFactory(PulsarReaderFactory<?> pulsarReaderFactory,
222-
SchemaResolver schemaResolver,
223-
ObjectProvider<DefaultPulsarReaderContainerFactoryCustomizer> customizersProvider,
221+
SchemaResolver schemaResolver, PulsarContainerFactoryCustomizers containerFactoryCustomizers,
224222
Environment environment) {
225223
PulsarReaderContainerProperties readerContainerProperties = new PulsarReaderContainerProperties();
226224
readerContainerProperties.setSchemaResolver(schemaResolver);
@@ -230,7 +228,7 @@ DefaultPulsarReaderContainerFactory<?> pulsarReaderContainerFactory(PulsarReader
230228
this.propertiesMapper.customizeReaderContainerProperties(readerContainerProperties);
231229
DefaultPulsarReaderContainerFactory<?> containerFactory = new DefaultPulsarReaderContainerFactory<>(
232230
pulsarReaderFactory, readerContainerProperties);
233-
customizersProvider.orderedStream().forEachOrdered((customizer) -> customizer.customize(containerFactory));
231+
containerFactoryCustomizers.customize(containerFactory);
234232
return containerFactory;
235233
}
236234

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,11 @@ PulsarTopicBuilder pulsarTopicBuilder() {
188188
this.properties.getDefaults().getTopic().getNamespace());
189189
}
190190

191+
@Bean
192+
@ConditionalOnMissingBean
193+
PulsarContainerFactoryCustomizers pulsarContainerFactoryCustomizers(
194+
ObjectProvider<PulsarContainerFactoryCustomizer<?>> customizers) {
195+
return new PulsarContainerFactoryCustomizers(customizers.orderedStream().toList());
196+
}
197+
191198
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@
1616

1717
package org.springframework.boot.autoconfigure.pulsar;
1818

19-
import org.springframework.pulsar.config.DefaultPulsarReaderContainerFactory;
19+
import org.springframework.pulsar.config.PulsarContainerFactory;
2020

2121
/**
22-
* Callback interface that can be implemented to customize a
23-
* {@link DefaultPulsarReaderContainerFactory}.
22+
* Callback interface that can be implemented by beans wishing to customize a
23+
* {@link PulsarContainerFactory} before it is fully initialized, in particular to tune
24+
* its configuration.
2425
*
26+
* @param <T> the type of the {@link PulsarContainerFactory}
2527
* @author Chris Bono
2628
* @since 3.4.0
2729
*/
2830
@FunctionalInterface
29-
public interface DefaultPulsarReaderContainerFactoryCustomizer {
31+
public interface PulsarContainerFactoryCustomizer<T extends PulsarContainerFactory<?, ?>> {
3032

3133
/**
32-
* Customize a {@link DefaultPulsarReaderContainerFactory}.
33-
* @param containerFactory the factory to customize
34+
* Customize the container factory.
35+
* @param containerFactory the {@code PulsarContainerFactory} to customize
3436
*/
35-
void customize(DefaultPulsarReaderContainerFactory<?> containerFactory);
37+
void customize(T containerFactory);
3638

3739
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.pulsar;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
import org.springframework.boot.util.LambdaSafe;
24+
import org.springframework.pulsar.config.PulsarContainerFactory;
25+
import org.springframework.pulsar.core.PulsarConsumerFactory;
26+
27+
/**
28+
* Invokes the available {@link PulsarContainerFactoryCustomizer} instances in the context
29+
* for a given {@link PulsarConsumerFactory}.
30+
*
31+
* @author Chris Bono
32+
* @since 3.4.0
33+
*/
34+
public class PulsarContainerFactoryCustomizers {
35+
36+
private final List<PulsarContainerFactoryCustomizer<?>> customizers;
37+
38+
public PulsarContainerFactoryCustomizers(List<? extends PulsarContainerFactoryCustomizer<?>> customizers) {
39+
this.customizers = (customizers != null) ? new ArrayList<>(customizers) : Collections.emptyList();
40+
}
41+
42+
/**
43+
* Customize the specified {@link PulsarContainerFactory}. Locates all
44+
* {@link PulsarContainerFactoryCustomizer} beans able to handle the specified
45+
* instance and invoke {@link PulsarContainerFactoryCustomizer#customize} on them.
46+
* @param <T> the type of container factory
47+
* @param containerFactory the container factory to customize
48+
* @return the customized container factory
49+
*/
50+
@SuppressWarnings("unchecked")
51+
public <T extends PulsarContainerFactory<?, ?>> T customize(T containerFactory) {
52+
LambdaSafe.callbacks(PulsarContainerFactoryCustomizer.class, this.customizers, containerFactory)
53+
.withLogger(PulsarContainerFactoryCustomizers.class)
54+
.invoke((customizer) -> customizer.customize(containerFactory));
55+
return containerFactory;
56+
}
57+
58+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,14 @@ private void applyMessageConsumerBuilderCustomizers(List<ReactiveMessageConsumer
164164
@ConditionalOnMissingBean(name = "reactivePulsarListenerContainerFactory")
165165
DefaultReactivePulsarListenerContainerFactory<?> reactivePulsarListenerContainerFactory(
166166
ReactivePulsarConsumerFactory<Object> reactivePulsarConsumerFactory, SchemaResolver schemaResolver,
167-
TopicResolver topicResolver,
168-
ObjectProvider<DefaultReactivePulsarListenerContainerFactoryCustomizer> customizersProvider) {
167+
TopicResolver topicResolver, PulsarContainerFactoryCustomizers containerFactoryCustomizers) {
169168
ReactivePulsarContainerProperties<Object> containerProperties = new ReactivePulsarContainerProperties<>();
170169
containerProperties.setSchemaResolver(schemaResolver);
171170
containerProperties.setTopicResolver(topicResolver);
172171
this.propertiesMapper.customizeContainerProperties(containerProperties);
173172
DefaultReactivePulsarListenerContainerFactory<?> containerFactory = new DefaultReactivePulsarListenerContainerFactory<>(
174173
reactivePulsarConsumerFactory, containerProperties);
175-
customizersProvider.orderedStream().forEachOrdered((customizer) -> customizer.customize(containerFactory));
174+
containerFactoryCustomizers.customize(containerFactory);
176175
return containerFactory;
177176
}
178177

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.springframework.pulsar.core.SchemaResolver;
7474
import org.springframework.pulsar.core.TopicResolver;
7575
import org.springframework.pulsar.listener.PulsarContainerProperties.TransactionSettings;
76+
import org.springframework.pulsar.reactive.config.DefaultReactivePulsarListenerContainerFactory;
7677
import org.springframework.pulsar.transaction.PulsarAwareTransactionManager;
7778
import org.springframework.test.util.ReflectionTestUtils;
7879

@@ -596,15 +597,23 @@ void whenHasUserDefinedCustomizersAppliesInCorrectOrder() {
596597
@TestConfiguration(proxyBeanMethods = false)
597598
static class ListenerContainerFactoryCustomizersConfig {
598599

600+
@Bean
601+
@Order(50)
602+
PulsarContainerFactoryCustomizer<DefaultReactivePulsarListenerContainerFactory<?>> customizerIgnored() {
603+
return (__) -> {
604+
throw new RuntimeException("should-not-have-matched");
605+
};
606+
}
607+
599608
@Bean
600609
@Order(200)
601-
ConcurrentPulsarListenerContainerFactoryCustomizer customizerFoo() {
610+
PulsarContainerFactoryCustomizer<ConcurrentPulsarListenerContainerFactory<?>> customizerFoo() {
602611
return (containerFactory) -> appendToSubscriptionName(containerFactory, ":foo");
603612
}
604613

605614
@Bean
606615
@Order(100)
607-
ConcurrentPulsarListenerContainerFactoryCustomizer customizerBar() {
616+
PulsarContainerFactoryCustomizer<ConcurrentPulsarListenerContainerFactory<?>> customizerBar() {
608617
return (containerFactory) -> appendToSubscriptionName(containerFactory, ":bar");
609618
}
610619

@@ -712,15 +721,23 @@ ReaderBuilderCustomizer<?> customizerBar() {
712721
@TestConfiguration(proxyBeanMethods = false)
713722
static class ReaderContainerFactoryCustomizersConfig {
714723

724+
@Bean
725+
@Order(50)
726+
PulsarContainerFactoryCustomizer<DefaultReactivePulsarListenerContainerFactory<?>> customizerIgnored() {
727+
return (__) -> {
728+
throw new RuntimeException("should-not-have-matched");
729+
};
730+
}
731+
715732
@Bean
716733
@Order(200)
717-
DefaultPulsarReaderContainerFactoryCustomizer customizerFoo() {
734+
PulsarContainerFactoryCustomizer<DefaultPulsarReaderContainerFactory<?>> customizerFoo() {
718735
return (containerFactory) -> appendToReaderListener(containerFactory, ":foo");
719736
}
720737

721738
@Bean
722739
@Order(100)
723-
DefaultPulsarReaderContainerFactoryCustomizer customizerBar() {
740+
PulsarContainerFactoryCustomizer<DefaultPulsarReaderContainerFactory<?>> customizerBar() {
724741
return (containerFactory) -> appendToReaderListener(containerFactory, ":bar");
725742
}
726743

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ void whenHasUserDefinedConnectionDetailsBeanDoesNotAutoConfigureBean() {
8686
.isSameAs(customConnectionDetails));
8787
}
8888

89+
@Test
90+
void whenHasUserDefinedContainerFactoryCustomizersBeanDoesNotAutoConfigureBean() {
91+
PulsarContainerFactoryCustomizers customizers = mock(PulsarContainerFactoryCustomizers.class);
92+
this.contextRunner
93+
.withBean("customContainerFactoryCustomizers", PulsarContainerFactoryCustomizers.class, () -> customizers)
94+
.run((context) -> assertThat(context).getBean(PulsarContainerFactoryCustomizers.class)
95+
.isSameAs(customizers));
96+
}
97+
8998
@Nested
9099
class ClientTests {
91100

0 commit comments

Comments
 (0)