Vert.x与RxJava

huangapple go评论88阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月22日 05:01:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63530012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定