Spring Boot: Rest endpoint integration with Kafka

2019-07-24 23:42发布

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.

Spring supports @Async for asynchronous methods but it doesn't work as expected. 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.

What is missing here?

1条回答
我想做一个坏孩纸
2楼-- · 2019-07-25 00:30

Try to use sync(blocking) method to send message producer.send(id).get(); This will make execution wait for the result.

查看更多
登录 后发表回答