流畅地使用 Vertx mongodb 客户端与内联与嵌套

Using the Vertx mongodb client fluently vs inline vs nested

通读文档,我仍然对以流畅的方式使用 mongoClient 的优势(如果有的话)感到困惑。任何人都可以向我解释一下吗,是否可以保证订单;

运行 行 - 两者将同时 运行 且不保证顺序。

mongoClient.runCommand("aggregate", getSomeCommand1(), res -> {});
mongoClient.runCommand("aggregate", getSomeCommand2(), res -> {});

运行 嵌套 - getSomeCommand1 将 运行 在 getSomeCommand2 之前先完成。

mongoClient.runCommand("aggregate", getSomeCommand1(), res1 -> {
       mongoClient.runCommand("aggregate", getSomeCommand2(), res2 -> {});
});

运行 以一种流畅的方式 - 与行中的 运行ning 相同?

mongoClient.runCommand("aggregate", getSomeCommand1(), res -> {}).mongoClient.runCommand("aggregate", getSomeCommand2(), res -> {});

远非完整的答案,但 运行ning 一些基本测试表明 运行ning 以流利的方式与 运行ning 一致;

I 运行 大型数据集上的慢速命令(聚合)和快速命令(计数)。

mongoClient.runCommand("aggregate", getTotalRecsPerTypeCommand(sellerId, collection), res -> {
    result.put("totalRecsPerType", res.result());
}).count(collection, new JsonObject().put("sellerId", sellerId), res -> {
    result.put("totalRecs", res.result());
    requestMessage.reply(result);
});

最初只返回总数,但是当回复从快速命令移动到慢速命令时,两个结果都会返回。这表明它们同时是 运行,没有顺序保证。

    mongoClient.runCommand("aggregate", getTotalRecsPerTypeCommand(sellerId, collection), res -> {
        result.put("totalRecsPerType", res.result());
        requestMessage.reply(result);
    }).count(collection, new JsonObject().put("sellerId", sellerId), res -> {
        result.put("totalRecs", res.result());
    });

运行 行 not 保证执行顺序,也就是说可能 运行 在负载不重的机器上多次使用第一个代码保持秩序。

流利 API 也是一样。在这种情况下,它只会帮助您省略分号。如果你想创建一个 flow,下一个命令将在第一个命令结束后触发,使用 RxJava(或嵌套情况,但在长 运行 中你可能结束 callback hell).

看这里:https://github.com/vert-x3/vertx-mongo-client/blob/master/vertx-mongo-service/src/main/generated/io/vertx/rxjava/ext/mongo/MongoService.java 虽然我不是这个 class 中使用的 ObservableFuture 的忠实粉丝(我建议使用 http://reactivex.io/RxJava/javadoc/rx/subjects/AsyncSubject.html),但它是一个很好的起点。