[英]Spring Boot: Rest endpoint integration with Kafka
Working on a rest endpoint which has to send a message to another service to process. 在其余端点上工作,该端点必须将消息发送到另一个服务进行处理。 It is a microservice architecture and all the services are connected via Kafka message broker. 它是一个微服务架构,所有服务都通过Kafka消息代理连接。
Spring supports @Async
for asynchronous methods but it doesn't work as expected. Spring支持@Async
用于异步方法,但是它不能按预期工作。 Code is something like 代码就像
@RequestMapping(method = RequestMethod.GET, value = "/responses/{id}", produces = "application/json")
@Async
public CompletableFuture<Response> getResponseById(@PathVariable @Valid Long id) {
//some code
producer.send(id);
//other service will send the response back and kafka consumer will save it to the db
responseRepository.findById(id);
}
It doesn't wait for the message to come back from kafka. 它不等待消息从kafka返回。
What is missing here? 这里缺少什么?
Try to use sync(blocking) method to send message producer.send(id).get();
尝试使用sync(blocking)方法发送消息producer.send(id).get();
This will make execution wait for the result. 这将使执行等待结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.