我正在使用play框架和Apache Kafka。
I am using play framework and Apache Kafka.
我有一个POST方法向Kafka发送消息。 Kafka有一个API方法
I have a POST method which sends a message to Kafka. Kafka has an API method
public java.util.concurrent.Future send(ProducerRecord record ,回调回调)
其中Javadoc表示
of which the Javadoc says
在确认发送后,异步向主题发送记录并调用提供的回调。
Asynchronously send a record to a topic and invoke the provided callback when the send has been acknowledged.
我使用play框架公开此功能。我想从Controller方法返回一个 Promise< Result> ,但无法弄清楚如何以非阻塞的方式实现。有人可以帮我解决这个问题吗?
I am exposing this functionality using the play framework. I want to return a Promise<Result> from the Controller method but can't figure out how to implement this in a non-blocking way. Can someone help me with this?
推荐答案/stackoverflow/questions/30956704/java-scala-future-driven-by-a-callback>这是一个。
After some searching found the answer with some help from this one.
以下是代码
RedeemablePromise<Result> promise = RedeemablePromise.empty(); kafkaProducer.send(record, (metadata, ex) -> { if (ex != null) { promise.failure(ex); } else { promise.success(created(Json.toJson(new ProduceResult()))); } });更多推荐
如何将回调转换为Promise
发布评论