Gabriel Kohen
Gabriel Kohen

Reputation: 4286

Handle multiple responses with ReplyingKafkaTemplate

I'm trying to implement a reply response pattern in which I publish a message to a topic listened to by several consumer groups. That means that they will all get the message as well as submit the response in the reply topic.

The problem is because they all respond to the same message, only the first received message in the reply topic shall be answered. The others will be discarded. Given that I know how many responses I should be getting on the reply topic(call that number-n), how can I make ReplyingKafkaTemplate wait for n responses and then resolve the answer? I've tried inferring from the Spring Kafka documentation but could not quite figure it out. Thanks.

Upvotes: 1

Views: 1361

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

That template is strictly for single request/reply processing.

You can't use it for your use case.

Use a KafkaTemplate and a separate listener; you will have to correlate the replies yourself.

EDIT

There is now an AggregatingReplyingTemplate for this use case: https://docs.spring.io/spring-kafka/docs/current/reference/html/#aggregating-request-reply

Upvotes: 2

Related Questions