How @SentTo sends the message to related topic?

瘦欲@ 提交于 2019-12-11 07:28:15

问题


I am using ReplyingKafkaTemplate in my Rest controller to return the synchronous response. I am also setting header REPLY_TOPIC. For listener microservice part,


 @KafkaListener(topics = "${kafka.topic.request-topic}")
 @SendTo
 public Model listen(Model<SumModel,SumResp> request) throws InterruptedException {

   SumModel model = request.getRequest();
   int sum = model.getNumber1() + model.getNumber2();

   SumResp resp = new SumResp(sum);
   request.setReply(resp);

   request.setAdditionalProperty("sum", sum);
   return request;
 }

My question is, How @sentTo annotation manage to publish the message to the topic provided in the header. There should be a KafkaTemplate bean or it does not need it? As we are just returning the message without using any KafkaTemplate or ReplyingKafkaTemplate bean.

Without any kafka template does it work or does it require KafkaTemplate or ReplyingKafkaTemplate beans?

Thanks


回答1:


Yes, a template is used to send the reply...

Assert.state(replyTopic == null || this.replyTemplate != null,
        "a KafkaTemplate is required to support replies");

Spring Boot automatically wires a template into the listener container factory if there is exactly one in the application context.

map.from(this.replyTemplate).to(factory::setReplyTemplate);

Boot also auto configures a template if there is not one already in the context.



来源:https://stackoverflow.com/questions/56402921/how-sentto-sends-the-message-to-related-topic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!