英文:
Vert.x with RxJava
问题
在我的当前项目中,使用了Vert.x与RxJava,我不明白为什么要这样做?
RxJava相比Vert.x还提供了哪些功能?
虽然有这个链接,但它并没有解答我的问题。
英文:
On my current project Vert.x is used with RxJava and I don't understand why?
What does RxJava offer on top of what Vert.x offers.
There is this but it doesn't answer my question.
答案1
得分: 4
核心 Vert.x API 使用回调来处理异步操作,这在组合(链接)操作时可能会很困难。例如,使用回调按顺序部署一组 Verticle,代码如下所示:
vertx.deployVerticle(MyClass1.class.getCanonicalName(), result1 -> {
if (result2.succeeded()) {
vertx.deployVerticle(MyClass2.class.getCanonicalName(), result2 -> {
if (result2.suceeded()) {
vertx.deployVerticle(MyClass3.class.getCanonicalName(), result3 -> {
if (result3.succeeded()) {
System.out.println("Deployed the verticles");
} else {
System.err.println("Failed to deploy verticle " + result3.cause());
}
});
} else {
System.err.println("Failed to deploy verticle " + result2.cause());
}
});
} else {
System.out.println("Failed to deploy verticle " + result1.cause());
}
);
使用 Rxified Vert.x API 的等效代码如下:
vertx.rxDeployVerticle(MyClass1.class.getCanonicalName())
.flatMap(ign -> vertx.rxDeployVerticle(MyClass2.class.getCanonicalName()))
.flatMap(ign -> vertx.rxDeployVerticle(MyClass3.class.getCanonicalName()))
.subscribe(
ign -> System.out.println("Deployed the verticles"),
err -> System.err.println("Failed to deploy verticle " + err)
);
RxJava 让处理像这样组合异步操作变得更加简单。诚然,这个示例有点人为(你当然可以清理回调版本以使其更易读),但它让你了解如何使用 RxJava 简单地链式操作。RxJava 提供了丰富的操作符,用于组合和转换异步操作,这允许你用很少的代码表达复杂的逻辑。这是使用核心 Vert.x API 不可能做到的。
Vert.x 4.0 将在核心 Vert.x 中添加基于 Future 的 API,这将减少对 RxJava 的需求,用于简单的工作流程(如上面的示例),但更复杂的流程仍将从中受益。
英文:
The core Vert.x API uses callbacks to deal with asynchronous operations, which can be difficult to compose (chain together). For example, deploying a bunch of Verticles in sequence looks like this using callbacks:
vertx.deployVerticle(MyClass1.class.getCanonicalName(), result1 -> {
if (result2.succeeded()) {
vertx.deployVerticle(MyClass2.class.getCanonicalName(), result2 -> {
if (result2.suceeded()) {
vertx.deployVerticle(MyClass3.class.getCanonicalName(), result3 -> {
if (result3.succeeded()) {
System.out.println("Deployed the verticles");
} else {
System.err.println("Failed to deploy verticle " + result3.cause());
}
});
} else {
System.err.println("Failed to deploy verticle " + result2.cause());
}
});
} else {
System.out.println("Failed to deploy verticle " + result1.cause());
}
);
The equivalent code using the Rxified Vert.x API would be this:
vertx.rxDeployVerticle(MyClass1.class.getCanonicalName())
.flatMap(ign -> vertx.rxDeployVerticle(MyClass2.class.getCanonicalName()))
.flatMap(ign -> vertx.rxDeployVerticle(MyClass3.class.getCanonicalName()))
.subscribe(
ign -> System.out.println("Deployed the verticles"),
err -> System.err.println("Failed to deploy verticle " + err)
);
RxJava makes it much easier to deal with composing asynchronous operations like this. Granted, this example is a little bit contrived (you could definitely clean up the callback version to be more readable), but it gives you an idea of how simple it is to chain the operations with RxJava. RxJava has a very rich set of operators for combining and transforming asynchronous operations, which allows you express complex logic with very little code. It's just not possible to do that with the core Vert.x API.
Vert.x 4.0 is going to add a Future-based API to core Vert.x, which will reduce the need for RxJava for simpler workflows (like the example above), but more complex flows will still benefit from it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论