我有一个从处理程序获得List<Future<String>>
的场景,现在必须返回Future<List<String>>
List<Future<String>> futures = new ArrayList<>();
for (Map.Entry<String, String> entry : services.entrySet()) {
Future<String> future =
Future.future(
fut -> {
if (fut.failed()) {
//do something
} else {
// do something
}
});
futures.add(future);
更新:
您对此有何想法,因为每个处理程序将异步执行此操作,一旦完成,我们就可以得到将来,然后添加到列表中:
for (Map.Entry<String, String> entry : services.entrySet()) {
Future future = Future.future();
WebClient.getAbs(entry.getKey()).send( ar -> {
if (ar.succeeded() && ar.result().statusCode() == 200) {
future.complete(ar.result());
} else {
futures.add("Failed");
}
} );
}
不确定您的意思,但应该通过CompositeFuture
https://vertx.io/docs/vertx-core/java/#_async_coordination实施“列表的未来”>
我会像这样放置您的代码: