Skip to content

Commit 3a54827

Browse files
garyrussellartembilan
authored andcommitted
Emit WARN log for @sendto with void return
Typo in doc.
1 parent 04110a6 commit 3a54827

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,9 @@
1919
import java.lang.reflect.Method;
2020
import java.util.Arrays;
2121

22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
2225
import org.springframework.core.annotation.AnnotationUtils;
2326
import org.springframework.kafka.listener.KafkaListenerErrorHandler;
2427
import org.springframework.kafka.listener.MessageListenerContainer;
@@ -48,6 +51,8 @@
4851
*/
4952
public class MethodKafkaListenerEndpoint<K, V> extends AbstractKafkaListenerEndpoint<K, V> {
5053

54+
private final Log logger = LogFactory.getLog(getClass());
55+
5156
private Object bean;
5257

5358
private Method method;
@@ -105,6 +110,9 @@ private String getReplyTopic() {
105110
if (method != null) {
106111
SendTo ann = AnnotationUtils.getAnnotation(method, SendTo.class);
107112
if (ann != null) {
113+
if (method.getReturnType().equals(void.class)) {
114+
this.logger.warn("Method has a void return type; @SendTo is ignored");
115+
}
108116
String[] destinations = ann.value();
109117
if (destinations.length > 1) {
110118
throw new IllegalStateException("Invalid @" + SendTo.class.getSimpleName() + " annotation on '"
@@ -132,7 +140,7 @@ protected MessagingMessageListenerAdapter<K, V> createMessageListener(MessageLis
132140
MessagingMessageListenerAdapter<K, V> messageListener = createMessageListenerInstance(messageConverter);
133141
messageListener.setHandlerMethod(configureListenerAdapter(messageListener));
134142
String replyTopic = getReplyTopic();
135-
if (replyTopic != null) {
143+
if (replyTopic != null && !getMethod().getReturnType().equals(void.class)) {
136144
Assert.state(getReplyTemplate() != null, "a KafkaTemplate is required to support replies");
137145
messageListener.setReplyTopic(replyTopic);
138146
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public Listener(String topic) {
190190
}
191191

192192
@KafkaListener(topics = "#{__listener.topic}", groupId = "#{__listener.topic}.group")
193+
// @SendTo("foo") test WARN log for void return
193194
public void listen1(List<Foo> foos, @Header(KafkaHeaders.RECEIVED_TOPIC) List<String> topics,
194195
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) List<Integer> partitions) {
195196
if (this.received == null) {

src/reference/asciidoc/kafka.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ containerProperties.setConsumerRebalanceListener(new ConsumerAwareRebalanceListe
10671067
[[annotation-send-to]]
10681068
===== Forwarding Listener Results using @SendTo
10691069

1070-
Starting with _version 2.0_, if you also annotate a `@KafkaListener` with a `@SendTo` annotation and the method invocation returns a result, the result will be forwared to the topic specified by the `@SendTo`.
1070+
Starting with _version 2.0_, if you also annotate a `@KafkaListener` with a `@SendTo` annotation and the method invocation returns a result, the result will be forwarded to the topic specified by the `@SendTo`.
10711071

10721072
The `@SendTo` value can have several forms:
10731073

0 commit comments

Comments
 (0)