Skip to content

Commit 62300bb

Browse files
authored
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 8ccd863 commit 62300bb

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
@@ -377,7 +377,7 @@ public Object postProcessAfterInitialization(final Object bean, final String bea
377377
AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null);
378378
multiMethods.addAll(methodsWithHandler);
379379
}
380-
if (annotatedMethods.isEmpty()) {
380+
if (annotatedMethods.isEmpty() && !hasClassLevelListeners) {
381381
this.nonAnnotatedClasses.add(bean.getClass());
382382
this.logger.trace(() -> "No @KafkaListener annotations found on bean type: " + bean.getClass());
383383
}

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

@@ -1004,6 +1004,12 @@ void proto(@Autowired ApplicationContext context) {
10041004
this.registry.setAlwaysStartAfterRefresh(true);
10051005
}
10061006

1007+
@Test
1008+
void classLevelTwoInstancesSameClass() {
1009+
assertThat(this.registry.getListenerContainer("multiTwoOne")).isNotNull();
1010+
assertThat(this.registry.getListenerContainer("multiTwoTwo")).isNotNull();
1011+
}
1012+
10071013
@Configuration
10081014
@EnableKafka
10091015
@EnableTransactionManagement(proxyTargetClass = true)
@@ -1739,6 +1745,16 @@ ProtoListener proto() {
17391745
return new ProtoListener();
17401746
}
17411747

1748+
@Bean
1749+
MultiListenerTwoInstances multiInstanceOne() {
1750+
return new MultiListenerTwoInstances("multiTwoOne");
1751+
}
1752+
1753+
@Bean
1754+
MultiListenerTwoInstances multiInstanceTwo() {
1755+
return new MultiListenerTwoInstances("multiTwoTwo");
1756+
}
1757+
17421758
}
17431759

17441760
static class ProtoListener {
@@ -2480,6 +2496,25 @@ public String bar(@Payload(required = false) KafkaNull nul,
24802496

24812497
}
24822498

2499+
@KafkaListener(id = "#{__listener.id}", topics = "multiWithTwoInstances", autoStartup = "false")
2500+
static class MultiListenerTwoInstances {
2501+
2502+
private final String id;
2503+
2504+
MultiListenerTwoInstances(String id) {
2505+
this.id = id;
2506+
}
2507+
2508+
public String getId() {
2509+
return this.id;
2510+
}
2511+
2512+
@KafkaHandler
2513+
void listen(String in) {
2514+
}
2515+
2516+
}
2517+
24832518
public interface Bar {
24842519

24852520
String getBar();

0 commit comments

Comments
 (0)