Skip to content

Commit 2f048be

Browse files
committed
Fix Request/Reply with ConsumerRecord<?, ?>
Message conversion is bypassed when consuming the raw `ConsumerRecord`. However, the request message is needed when returning a result (for reply topic determination, correlation, etc). **I will do the backports; I expect conflicts in the test.**
1 parent bc086de commit 2f048be

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

spring-kafka/src/main/java/org/springframework/kafka/listener/adapter/MessagingMessageListenerAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ else if (methodParameter.hasParameterAnnotation(Header.class)) {
579579
}
580580
}
581581

582-
if (notConvertibleParameters == method.getParameterCount()) {
582+
if (notConvertibleParameters == method.getParameterCount() && method.getReturnType().equals(void.class)) {
583583
this.conversionNeeded = false;
584584
}
585585
boolean validParametersForBatch = method.getGenericParameterTypes().length <= allowedBatchParameters;

spring-kafka/src/test/java/org/springframework/kafka/requestreply/ReplyingKafkaTemplateTests.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@
106106
ReplyingKafkaTemplateTests.D_REPLY, ReplyingKafkaTemplateTests.D_REQUEST,
107107
ReplyingKafkaTemplateTests.E_REPLY, ReplyingKafkaTemplateTests.E_REQUEST,
108108
ReplyingKafkaTemplateTests.F_REPLY, ReplyingKafkaTemplateTests.F_REQUEST,
109-
ReplyingKafkaTemplateTests.J_REPLY, ReplyingKafkaTemplateTests.J_REQUEST })
109+
ReplyingKafkaTemplateTests.J_REPLY, ReplyingKafkaTemplateTests.J_REQUEST,
110+
ReplyingKafkaTemplateTests.K_REPLY, ReplyingKafkaTemplateTests.K_REQUEST })
110111
public class ReplyingKafkaTemplateTests {
111112

112113
public static final String A_REPLY = "aReply";
@@ -141,6 +142,10 @@ public class ReplyingKafkaTemplateTests {
141142

142143
public static final String J_REQUEST = "jRequest";
143144

145+
public static final String K_REPLY = "kReply";
146+
147+
public static final String K_REQUEST = "kRequest";
148+
144149
@Autowired
145150
private EmbeddedKafkaBroker embeddedKafka;
146151

@@ -187,6 +192,24 @@ public void testGood() throws Exception {
187192
}
188193
}
189194

195+
@Test
196+
void testConsumerRecord() throws Exception {
197+
ReplyingKafkaTemplate<Integer, String, String> template = createTemplate(K_REPLY);
198+
try {
199+
template.setDefaultReplyTimeout(Duration.ofSeconds(30));
200+
Headers headers = new RecordHeaders();
201+
ProducerRecord<Integer, String> record = new ProducerRecord<>(K_REQUEST, null, null, null, "foo", headers);
202+
RequestReplyFuture<Integer, String, String> future = template.sendAndReceive(record);
203+
future.getSendFuture().get(10, TimeUnit.SECONDS); // send ok
204+
ConsumerRecord<Integer, String> consumerRecord = future.get(30, TimeUnit.SECONDS);
205+
assertThat(consumerRecord.value()).isEqualTo("FOO");
206+
}
207+
finally {
208+
template.stop();
209+
template.destroy();
210+
}
211+
}
212+
190213
@Test
191214
public void testBadDeserialize() throws Exception {
192215
ReplyingKafkaTemplate<Integer, String, String> template = createTemplate(J_REPLY, true);
@@ -669,6 +692,12 @@ public void gListener(Message<String> in) {
669692
public String handleJ(String in) throws InterruptedException {
670693
return in.toUpperCase();
671694
}
695+
@KafkaListener(id = K_REQUEST, topics = { K_REQUEST })
696+
697+
@SendTo
698+
public String handleK(ConsumerRecord<String, String> in) {
699+
return in.value().toUpperCase();
700+
}
672701

673702
}
674703

0 commit comments

Comments
 (0)