Skip to content

Commit 62ecbf0

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 93ee7ac commit 62ecbf0

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

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 "

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;
@@ -228,12 +228,12 @@ private void setupReplyTo(InvocableHandlerMethod handler) {
228228
Method method = handler.getMethod();
229229
SendTo ann = null;
230230
if (method != null) {
231-
ann = AnnotationUtils.getAnnotation(method, SendTo.class);
231+
ann = AnnotatedElementUtils.findMergedAnnotation(method, SendTo.class);
232232
replyTo = extractSendTo(method.toString(), ann);
233233
}
234234
if (ann == null) {
235235
Class<?> beanType = handler.getBeanType();
236-
ann = AnnotationUtils.getAnnotation(beanType, SendTo.class);
236+
ann = AnnotatedElementUtils.findMergedAnnotation(beanType, SendTo.class);
237237
replyTo = extractSendTo(beanType.getSimpleName(), ann);
238238
}
239239
if (ann != null && replyTo == null) {

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

+35-6
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@
185185
"annotated25", "annotated25reply1", "annotated25reply2", "annotated26", "annotated27", "annotated28",
186186
"annotated29", "annotated30", "annotated30reply", "annotated31", "annotated32", "annotated33",
187187
"annotated34", "annotated35", "annotated36", "annotated37", "foo", "manualStart", "seekOnIdle",
188-
"annotated38", "annotated38reply", "annotated39", "annotated40", "annotated41", "annotated42" })
188+
"annotated38", "annotated38reply", "annotated39", "annotated40", "annotated41", "annotated42",
189+
"annotated43", "annotated43reply"})
189190
@TestPropertySource(properties = "spel.props=fetch.min.bytes=420000,max.poll.records=10")
190191
public class EnableKafkaIntegrationTests {
191192

@@ -439,6 +440,15 @@ public void testInterface() throws Exception {
439440
template.send("annotated7", 0, "foo");
440441
template.flush();
441442
assertThat(this.ifaceListener.getLatch1().await(60, TimeUnit.SECONDS)).isTrue();
443+
Map<String, Object> consumerProps = new HashMap<>(this.consumerFactory.getConfigurationProperties());
444+
consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "testInterface");
445+
ConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps);
446+
Consumer<Integer, String> consumer = cf.createConsumer();
447+
this.embeddedKafka.consumeFromAnEmbeddedTopic(consumer, "annotated43reply");
448+
template.send("annotated43", 0, "foo");
449+
ConsumerRecord<Integer, String> reply = KafkaTestUtils.getSingleRecord(consumer, "annotated43reply");
450+
assertThat(reply).extracting(rec -> rec.value()).isEqualTo("FOO");
451+
consumer.close();
442452
}
443453

444454
@Test
@@ -1376,8 +1386,8 @@ public MultiListenerNoDefault multiNoDefault() {
13761386
}
13771387

13781388
@Bean
1379-
public MultiListenerSendTo multiListenerSendTo() {
1380-
return new MultiListenerSendTo();
1389+
public MultiListenerSendToImpl multiListenerSendTo() {
1390+
return new MultiListenerSendToImpl();
13811391
}
13821392

13831393
@Bean
@@ -2299,6 +2309,10 @@ interface IfaceListener<T> {
22992309

23002310
void listen(T foo);
23012311

2312+
@SendTo("annotated43reply")
2313+
@KafkaListener(id = "ifcR", topics = "annotated43")
2314+
String reply(String in);
2315+
23022316
}
23032317

23042318
static class IfaceListenerImpl implements IfaceListener<String> {
@@ -2319,6 +2333,11 @@ public void listenTx(String foo) {
23192333
latch2.countDown();
23202334
}
23212335

2336+
@Override
2337+
public String reply(String in) {
2338+
return in.toUpperCase();
2339+
}
2340+
23222341
public CountDownLatch getLatch1() {
23232342
return latch1;
23242343
}
@@ -2425,15 +2444,25 @@ public void bar(@Valid ValidatedClass val) {
24252444

24262445
@KafkaListener(id = "multiSendTo", topics = "annotated25")
24272446
@SendTo("annotated25reply1")
2428-
static class MultiListenerSendTo {
2447+
interface MultiListenerSendTo {
2448+
2449+
@KafkaHandler
2450+
String foo(String in);
24292451

24302452
@KafkaHandler
2453+
@SendTo("!{'annotated25reply2'}")
2454+
String bar(KafkaNull nul, int key);
2455+
2456+
}
2457+
2458+
static class MultiListenerSendToImpl implements MultiListenerSendTo {
2459+
2460+
@Override
24312461
public String foo(String in) {
24322462
return in.toUpperCase();
24332463
}
24342464

2435-
@KafkaHandler
2436-
@SendTo("!{'annotated25reply2'}")
2465+
@Override
24372466
public String bar(@Payload(required = false) KafkaNull nul,
24382467
@Header(KafkaHeaders.RECEIVED_KEY) int key) {
24392468
return "BAR";

0 commit comments

Comments
 (0)