
co.spraybot.messagerunner.CourierProcessorsLauncher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of message-runner Show documentation
Show all versions of message-runner Show documentation
A micro-framework to allow easily passing specific Vert.x messages to specific addresses for processing of those messages.
The newest version!
package co.spraybot.messagerunner;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Verticle;
import java.util.ArrayList;
import java.util.List;
/**
* A Verticle responsible for ensuring that your Courier implementations and Verticles acting as parcel processors are
* deployed correctly.
*
* Each Verticle that acts as a parcel processor is expected to perform at least 2 operations during its startup
* procedure. The first being the registering of an EventBus consumer against the address that the Verticle expects to
* receive Parcels on. Because of this combined with the asynchronous nature of deploying Verticles it could be possible
* that the Courier has not registered the EventBus consumer for handling Parcels and your Verticle will fail attempting
* to send its ProcessorAvailabilityParcel.
*
* This class ensures that your application Verticles interacting with the Courier are appropriately deployed according
* to the following rules:
*
* 1. The Courier passed in to the constructor MUST finish deploying before Verticles are deployed.
* 2. If the Courier does not deploy successfully the passed Verticles MUST NOT be deployed.
* 3. All passed Verticles MUST be deployed asynchronously and SHOULD be able to depend on the Courier running properly.
* 4. All Verticles SHOULD have the opportunity to deploy and if any Verticle fails deploying the Launcher MUST fail
* its deployment.
* 5. The CourierProcessorsLauncher MUST NOT undeploy itself or the children Verticles will also undeploy
*/
final public class CourierProcessorsLauncher extends AbstractVerticle {
private Courier courier;
private List verticles;
public CourierProcessorsLauncher(Courier courier, List verticles) {
this.courier = courier;
this.verticles = verticles;
}
@Override
public void start(Future future) {
deployCourier().setHandler(arVoid -> {
if (arVoid.failed()) {
future.fail(arVoid.cause());
} else {
deployVerticles().setHandler(arCompositeFuture -> {
if (arCompositeFuture.failed()) {
future.fail(arCompositeFuture.cause());
} else {
future.complete();
}
});
}
});
}
private Future deployCourier() {
Future future = Future.future();
getVertx().deployVerticle(courier, arDeployId -> {
if (arDeployId.failed()) {
future.fail(arDeployId.cause());
} else {
future.complete();
}
});
return future;
}
private CompositeFuture deployVerticles() {
List deployFutures = new ArrayList<>();
for (Verticle verticle : verticles) {
Future deployFuture = Future.future();
deployFutures.add(deployFuture);
getVertx().deployVerticle(verticle, arDeployId -> {
if (arDeployId.failed()) {
deployFuture.fail(arDeployId.cause());
} else {
deployFuture.complete(arDeployId.result());
}
});
}
return CompositeFuture.join(deployFutures);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy