我在这段代码中使用了vertx mongoclient:
mongoClient.getCollections(res -> {
if (res.succeeded()) {
if (res.result().size() > 0) {
for (String collection : res.result()) {
mongoClient.dropCollection(collection, resDrop -> {
if (resDrop.succeeded()) {
LOGGER.warn(collection + "was dropped");
}
});
}
} else LOGGER.warn("database was dropped");
} else LOGGER.warn("database was dropped");
});
我想在删除所有旧收藏后创建一些新收藏品 但正如我们所知,删除异步执行的集合 我如何知道所有旧收藏品何时被删除?
答案 0 :(得分:2)
这是一个常见的asynchronous coordination问题。请查看Vert.x文档中的Concurrent composition部分。
答案 1 :(得分:1)
mongoClient.getCollections(res -> {
if (res.succeeded()) {
if (res.result().size() > 0) {
**List<Future> futureList = new ArrayList<>();**
for (String collection : res.result()) {
mongoClient.dropCollection(collection, resDrop -> {
if (resDrop.succeeded()) {
**Future future = Future.future();
futureList.add(future);
future.complete();**
LOGGER.warn(collection + "was dropped");
}else{
future.fail(ar.cause());
}
});
}
**CompositeFuture.all(futureList).setHandler(ar -> {
if (ar.succeeded()) {
LOGGER.info("All collections was dropped");
} else {
LOGGER.warn("One or all collections was not dropped");
}
});**
} else LOGGER.warn("database was dropped");
} else LOGGER.warn("database was dropped");
});
答案 2 :(得分:0)
我建议使用CompletableFutures。您可以调用allOf方法。