简体   繁体   English

Vert.x 如何处理一个周期一个周期?

[英]How Vert.x deal with cycle by cycle?

This is shown in the following code:这显示在以下代码中:

public Future<Boolean> sendHeartbeat(List<BrokerMeta> brokerMetas) {

    PromiseInternal<Boolean> resultPromise = ContextInternal.current().promise();
    for (BrokerMeta brokerMeta : brokerMetas) {
        if (null == brokerMeta.getAdr()) {
            continue;
        }
        remote(brokerMeta.getAdr());
    }

    return resultPromise.future();
}

private Future<String> remote(String addr) {
    PromiseInternal<String> promise = ContextInternal.current().promise();
    // Request remote....
    promise.complete("From remote");
    return promise.future();
}

I want the Remote methods to execute one by one and return, and then I'm pushing resultPromise down,How do I write this code?我希望Remote方法一个一个执行并返回,然后我将resultPromise向下推,我该如何编写这段代码?

You need to call .compose on the future sent from the remote(string addr) function to make sure they run one after the other ie the last future returned from the remote function will call compose on it and assign it to a new future which in the next iteration would call compose on it and so on您需要在从remote(string addr) function 发送的未来上调用.compose以确保它们一个接一个地运行,即从远程 function 返回的最后一个未来将在其上调用 compose 并将其分配给一个新的未来下一次迭代将调用 compose 等等

Ref: Sequential composition for arbitrary number of calls in Vertx with Futures参考: 在 Vertx 中使用 Futures 进行任意数量调用的顺序组合

Stuff like this is a lot easier when you use kotlin with coroutines - because then each call can just suspend before moving on to the next iteration - so would highly recommend looking into kotlin if you are using vert.x当您将 kotlin 与协程一起使用时,这样的事情会容易得多 - 因为每次调用都可以在继续下一次迭代之前suspend - 所以如果您使用的是 vert.x,强烈建议您查看 kotlin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM