Skip to content

Commit a5a28cc

Browse files
garyrussellartembilan
authored andcommitted
GH-2304: Fix SendTo on Interface Etc.
Resolves #2304 `@SendTo` was only detected on the concrete listener method. Use `AnnotatedElementUtils.findMergedAnnotation()` instead. **cherry-pick to 2.9.x, 2.8.x**
1 parent f6c0996 commit a5a28cc

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

Diff for: spring-kafka/src/main/java/org/springframework/kafka/config/MethodKafkaListenerEndpoint.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.beans.factory.config.BeanExpressionContext;
2626
import org.springframework.beans.factory.config.BeanExpressionResolver;
2727
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
28-
import org.springframework.core.annotation.AnnotationUtils;
28+
import org.springframework.core.annotation.AnnotatedElementUtils;
2929
import org.springframework.core.log.LogAccessor;
3030
import org.springframework.expression.BeanResolver;
3131
import org.springframework.kafka.listener.KafkaListenerErrorHandler;
@@ -132,7 +132,7 @@ public void setMessagingConverter(SmartMessageConverter messagingConverter) {
132132
private String getReplyTopic() {
133133
Method replyingMethod = getMethod();
134134
if (replyingMethod != null) {
135-
SendTo ann = AnnotationUtils.getAnnotation(replyingMethod, SendTo.class);
135+
SendTo ann = AnnotatedElementUtils.findMergedAnnotation(replyingMethod, SendTo.class);
136136
if (ann != null) {
137137
if (replyingMethod.getReturnType().equals(void.class)) {
138138
this.logger.warn(() -> "Method "

Diff for: spring-kafka/src/main/java/org/springframework/kafka/listener/adapter/DelegatingInvocableHandler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.springframework.beans.factory.config.BeanExpressionResolver;
3232
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
3333
import org.springframework.core.MethodParameter;
34-
import org.springframework.core.annotation.AnnotationUtils;
34+
import org.springframework.core.annotation.AnnotatedElementUtils;
3535
import org.springframework.expression.Expression;
3636
import org.springframework.expression.spel.standard.SpelExpressionParser;
3737
import org.springframework.kafka.KafkaException;
@@ -193,12 +193,12 @@ private void setupReplyTo(InvocableHandlerMethod handler) {
193193
Method method = handler.getMethod();
194194
SendTo ann = null;
195195
if (method != null) {
196-
ann = AnnotationUtils.getAnnotation(method, SendTo.class);
196+
ann = AnnotatedElementUtils.findMergedAnnotation(method, SendTo.class);
197197
replyTo = extractSendTo(method.toString(), ann);
198198
}
199199
if (ann == null) {
200200
Class<?> beanType = handler.getBeanType();
201-
ann = AnnotationUtils.getAnnotation(beanType, SendTo.class);
201+
ann = AnnotatedElementUtils.findMergedAnnotation(beanType, SendTo.class);
202202
replyTo = extractSendTo(beanType.getSimpleName(), ann);
203203
}
204204
if (ann != null && replyTo == null) {

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

+35-6
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@
182182
"annotated25", "annotated25reply1", "annotated25reply2", "annotated26", "annotated27", "annotated28",
183183
"annotated29", "annotated30", "annotated30reply", "annotated31", "annotated32", "annotated33",
184184
"annotated34", "annotated35", "annotated36", "annotated37", "foo", "manualStart", "seekOnIdle",
185-
"annotated38", "annotated38reply", "annotated39", "annotated40", "annotated41", "annotated42" })
185+
"annotated38", "annotated38reply", "annotated39", "annotated40", "annotated41", "annotated42",
186+
"annotated43", "annotated43reply"})
186187
@TestPropertySource(properties = "spel.props=fetch.min.bytes=420000,max.poll.records=10")
187188
public class EnableKafkaIntegrationTests {
188189

@@ -431,6 +432,15 @@ public void testInterface() throws Exception {
431432
template.send("annotated7", 0, "foo");
432433
template.flush();
433434
assertThat(this.ifaceListener.getLatch1().await(60, TimeUnit.SECONDS)).isTrue();
435+
Map<String, Object> consumerProps = new HashMap<>(this.consumerFactory.getConfigurationProperties());
436+
consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "testInterface");
437+
ConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps);
438+
Consumer<Integer, String> consumer = cf.createConsumer();
439+
this.embeddedKafka.consumeFromAnEmbeddedTopic(consumer, "annotated43reply");
440+
template.send("annotated43", 0, "foo");
441+
ConsumerRecord<Integer, String> reply = KafkaTestUtils.getSingleRecord(consumer, "annotated43reply");
442+
assertThat(reply).extracting(rec -> rec.value()).isEqualTo("FOO");
443+
consumer.close();
434444
}
435445

436446
@Test
@@ -1373,8 +1383,8 @@ public MultiListenerNoDefault multiNoDefault() {
13731383
}
13741384

13751385
@Bean
1376-
public MultiListenerSendTo multiListenerSendTo() {
1377-
return new MultiListenerSendTo();
1386+
public MultiListenerSendToImpl multiListenerSendTo() {
1387+
return new MultiListenerSendToImpl();
13781388
}
13791389

13801390
@Bean
@@ -2296,6 +2306,10 @@ interface IfaceListener<T> {
22962306

22972307
void listen(T foo);
22982308

2309+
@SendTo("annotated43reply")
2310+
@KafkaListener(id = "ifcR", topics = "annotated43")
2311+
String reply(String in);
2312+
22992313
}
23002314

23012315
static class IfaceListenerImpl implements IfaceListener<String> {
@@ -2316,6 +2330,11 @@ public void listenTx(String foo) {
23162330
latch2.countDown();
23172331
}
23182332

2333+
@Override
2334+
public String reply(String in) {
2335+
return in.toUpperCase();
2336+
}
2337+
23192338
public CountDownLatch getLatch1() {
23202339
return latch1;
23212340
}
@@ -2422,15 +2441,25 @@ public void bar(@Valid ValidatedClass val) {
24222441

24232442
@KafkaListener(id = "multiSendTo", topics = "annotated25")
24242443
@SendTo("annotated25reply1")
2425-
static class MultiListenerSendTo {
2444+
interface MultiListenerSendTo {
2445+
2446+
@KafkaHandler
2447+
String foo(String in);
24262448

24272449
@KafkaHandler
2450+
@SendTo("!{'annotated25reply2'}")
2451+
String bar(KafkaNull nul, int key);
2452+
2453+
}
2454+
2455+
static class MultiListenerSendToImpl implements MultiListenerSendTo {
2456+
2457+
@Override
24282458
public String foo(String in) {
24292459
return in.toUpperCase();
24302460
}
24312461

2432-
@KafkaHandler
2433-
@SendTo("!{'annotated25reply2'}")
2462+
@Override
24342463
public String bar(@Payload(required = false) KafkaNull nul,
24352464
@Header(KafkaHeaders.RECEIVED_KEY) int key) {
24362465
return "BAR";

0 commit comments

Comments
 (0)