ListenableFuture - how to wait before return

Viewed 9705

I have a Spring Cloud microservice that posts message on a Kafka broker, this microservice is accessible thru a REST api.

I want to return the submit status back to the caller but seems like Java does not await. How to make this to wait either success or failure before my code returns?

Heres the code:

kafkaProduc.send("topictest", msg).addCallback(
                new ListenableFutureCallback<SendResult<String, ExecutionDataMessage>>() {
    @Override
    public void onSuccess(SendResult<String, ExecutionDataMessage> result) {
        eresp.status = "ok";
        eresp.msg = "message submitted successfully";
    }

    @Override
    public void onFailure(Throwable ex) {
        eresp.status = "error";
        eresp.msg = "failure while sending data to kafka. exception: " + ex.getMessage();
    }
});
HttpStatus erespStatus = eresp.status == "ok" ? HttpStatus.CREATED : HttpStatus.BAD_REQUEST;
return new ResponseEntity<ExecutionResponse>(eresp, erespStatus);
2 Answers
Related