How to Convert callback to a Promise

前端 未结 1 1806
后悔当初
后悔当初 2021-01-23 17:56

I am using play framework and Apache Kafka.

I have a POST method which sends a message to Kafka. Kafka has an API method

public java.util.concurrent.Future send

相关标签:
1条回答
  • 2021-01-23 18:38

    After some searching found the answer with some help from this one.

    Following is the code

    RedeemablePromise<Result> promise = RedeemablePromise.empty();
    
    kafkaProducer.send(record, (metadata, ex) -> {
        if (ex != null) {
            promise.failure(ex);
        } else {
            promise.success(created(Json.toJson(new ProduceResult())));
        }
    });
    
    0 讨论(0)
提交回复
热议问题