2016-12-08 7 views
0

完了後のクエリ:私たちが知っているように、すべての古いコレクションが
しかしを削除された後、いくつかの新しいコレクションを作成したい
を実行し、私はこのコードで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

すべてのコレクションを1つのコマンドで破棄しない理由は何ですか?これを確認してくださいhttp://vertx.io/docs/vertx-mongo-client/java/#_running_other_mongodb_commands –

+0

これは単なるテスト用です私はこれを必要とします複数の非同期クエリの完了後にクエリを実行 – user298582

+0

completablefutureまたはrxjavaを使用 –

答えて

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

これは一般的なasynchronous coordinationの問題です。 Vert.xのドキュメントのConcurrent compositionのセクションをご覧ください。

+0

コンカレント構成6先物については – user298582

+0

[同時コンポジションは未来のリストと連動](http: /vertx.io/docs/apidocs/io/vertx/core/CompositeFuture.html#all-java.util.List-)同様に – tsegismont

関連する問題