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:
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-rx-java2</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-rx-java2-gen</artifactId>
<version>${vertx.version}</version>
</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:
Single<String> dbVerticleDeployment = vertx.rxDeployVerticle(
"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
.