Provides when.java wrappers for standard vert.x 3 objects to return promises.
Add a dependency to vertx-when
<dependency>
<groupId>com.englishtown.vertx</groupId>
<artifactId>vertx-when</artifactId>
<version>4.0.0</version>
</dependency>
Use dependency injection to get an instance of:
com.englishtown.promises.When
com.englishtown.vertx.promises.WhenEventBus
com.englishtown.vertx.promises.WhenHttpClient
com.englishtown.vertx.promises.WhenVertx
HK2 and Guice binders are provided
com.englishtown.vertx.promises.hk2.HK2WhenBinder
com.englishtown.vertx.promises.guice.GuiceWhenBinder
If not using DI, you can manually construct the default implementations like this:
// Create the vert.x executor for callbacks to run on the vert.x event loop
VertxExecutor executor = new VertxExecutor(vertx);
when = WhenFactory.createFor(() -> executor);
whenVertx = new DefaultWhenVertx(vertx, when);
whenEventBus = new DefaultWhenEventBus(vertx, when);
whenHttpClient = new DefaultWhenHttpClient(vertx, when);
(See the com.englishtown.vertx.promises.integration.simple.NoDIIntegrationTest
integration test for an example.)
If running vert.x 2.x, then you should use module vertx-mod-when 3.0.1. See earlier README.md files for details.
<dependency>
<groupId>com.englishtown</groupId>
<artifactId>vertx-mod-when</artifactId>
<version>3.0.1</version>
</dependency>
whenVertx.deployVerticle("com.englishtown.vertx.TestVerticle")
.then(deploymentID -> {
// On success
return null;
})
.otherwise(t -> {
// On fail
return null;
});
List<Promise<String>> promises = new ArrayList<>();
promises.add(vertxWhen.deployVerticle("com.englishtown.vertx.TestVerticle1"));
promises.add(vertxWhen.deployVerticle("com.englishtown.vertx.TestVerticle2"));
when.all(promises)
.then(deploymentIDs -> {
// Handle success
return null;
})
.otherwise(t -> {
// Handle failure
return null;
});
List<Promise<Message<JsonObject>>> promises = new ArrayList<>();
promises.add(whenEventBus.<JsonObject>send("et.vertx.eb.1", new JsonObject().putString("message", "hello")));
promises.add(whenEventBus.<JsonObject>send("et.vertx.eb.2", new JsonObject().putString("message", "world")));
when.all(promises).then(
replies -> {
// On success
return null;
},
t -> {
// On fail
return null;
});
List<Promise<HttpClientResponse>> promises = new ArrayList<>();
promises.add(whenHttpClient.requestAbs(HttpMethod.GET, "http://test.englishtown.com/test1", new RequestOptions()));
promises.add(whenHttpClient.requestAbs(HttpMethod.POST, "http://test.englishtown.com/test2", new RequestOptions()));
when.all(promises).then(
responses -> {
// On success
return null;
},
t -> {
// On fail
return null;
}
);
RequestOptions options = new RequestOptions().setPauseResponse(true);
whenHttpClient.requestAbs(HttpMethod.GET, "http://localhost:8081/test", options)
.then(response -> {
return whenHttpClient.body(response);
})
.then(body -> {
// Do something with the body
return null;
})
.otherwise(t ->
// On fail
return null;
});