Skip to content

Commit 48b2017

Browse files
garyrussellartembilan
authored andcommitted
GH-2451: Fix Class Level Listener Multi Instances
Resolves #2451 Classes with class level `@KafkaListener` were incorrectly added to the `nonAnnotatedClasses` set, preventing multiple instances of the same class to be registered as listeners. **cherry-pick to 2.9.x, 2.8.x** * Fix CheckStyle.
1 parent a77125f commit 48b2017

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public Object postProcessAfterInitialization(final Object bean, final String bea
362362
AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null);
363363
multiMethods.addAll(methodsWithHandler);
364364
}
365-
if (annotatedMethods.isEmpty()) {
365+
if (annotatedMethods.isEmpty() && !hasClassLevelListeners) {
366366
this.nonAnnotatedClasses.add(bean.getClass());
367367
this.logger.trace(() -> "No @KafkaListener annotations found on bean type: " + bean.getClass());
368368
}

spring-kafka/src/test/java/org/springframework/kafka/annotation/EnableKafkaIntegrationTests.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@
185185
"annotated29", "annotated30", "annotated30reply", "annotated31", "annotated32", "annotated33",
186186
"annotated34", "annotated35", "annotated36", "annotated37", "foo", "manualStart", "seekOnIdle",
187187
"annotated38", "annotated38reply", "annotated39", "annotated40", "annotated41", "annotated42",
188-
"annotated43", "annotated43reply"})
188+
"annotated43", "annotated43reply" })
189189
@TestPropertySource(properties = "spel.props=fetch.min.bytes=420000,max.poll.records=10")
190190
public class EnableKafkaIntegrationTests {
191191

@@ -1009,6 +1009,12 @@ void proto(@Autowired ApplicationContext context) {
10091009
this.registry.setAlwaysStartAfterRefresh(true);
10101010
}
10111011

1012+
@Test
1013+
void classLevelTwoInstancesSameClass() {
1014+
assertThat(this.registry.getListenerContainer("multiTwoOne")).isNotNull();
1015+
assertThat(this.registry.getListenerContainer("multiTwoTwo")).isNotNull();
1016+
}
1017+
10121018
@Configuration
10131019
@EnableKafka
10141020
@EnableTransactionManagement(proxyTargetClass = true)
@@ -1731,6 +1737,16 @@ ProtoListener proto() {
17311737
return new ProtoListener();
17321738
}
17331739

1740+
@Bean
1741+
MultiListenerTwoInstances multiInstanceOne() {
1742+
return new MultiListenerTwoInstances("multiTwoOne");
1743+
}
1744+
1745+
@Bean
1746+
MultiListenerTwoInstances multiInstanceTwo() {
1747+
return new MultiListenerTwoInstances("multiTwoTwo");
1748+
}
1749+
17341750
}
17351751

17361752
static class ProtoListener {
@@ -2461,6 +2477,25 @@ public String bar(@Payload(required = false) KafkaNull nul,
24612477

24622478
}
24632479

2480+
@KafkaListener(id = "#{__listener.id}", topics = "multiWithTwoInstances", autoStartup = "false")
2481+
static class MultiListenerTwoInstances {
2482+
2483+
private final String id;
2484+
2485+
MultiListenerTwoInstances(String id) {
2486+
this.id = id;
2487+
}
2488+
2489+
public String getId() {
2490+
return this.id;
2491+
}
2492+
2493+
@KafkaHandler
2494+
void listen(String in) {
2495+
}
2496+
2497+
}
2498+
24642499
public interface Bar {
24652500

24662501
String getBar();

0 commit comments

Comments
 (0)