Enabling the RxJava APIs

In addition to the callback-based API, the Vert.x modules offer an “Rxified” API. To enable it, start by adding the vertx-rx-java2 module to the Maven POM file:

  1. <dependency>
  2. <groupId>io.vertx</groupId>
  3. <artifactId>vertx-rx-java2</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>io.vertx</groupId>
  7. <artifactId>vertx-rx-java2-gen</artifactId>
  8. <version>${vertx.version}</version>
  9. </dependency>

Verticles then have to be modified so that they extend io.vertx.reactivex.core.AbstractVerticle instead of io.vertx.core.AbstractVerticle. How is this different? The former class extends the latter and exposes a io.vertx.reactivex.core.Vertx field.

io.vertx.reactivex.core.Vertx defines extra rxSomething(…​) methods that are equivalent to their callback-based counterparts.

Let’s take a look at the MainVerticle to get a better idea of how it works in practice:

  1. Single<String> dbVerticleDeployment = vertx.rxDeployVerticle(
  2. "io.vertx.guides.wiki.database.WikiDatabaseVerticle");

The rxDeploy method does not take a Handler<AsyncResult<String>> as final parameter. Instead, it returns a Single<String>.

Besides, the operation does not start when the method is called. It starts when you subscribe to the Single. When the operation completes, it emits the deployment id or signals the cause of the problem with a Throwable.