Verticles are the technical units of deployments of code in Vert.x.
Verticles share certain similarities with actors in the actor model.
Verticles communicate with each other by generating messages in a single event bus. Those messages are sent on the event bus to a specific address, and verticles can register to this address by using handlers.
verticles are similar to modules.
They are executing in their own context on top of a Vert.x instance.
Verticles:
io.vertx.core.AbstractVerticle is the base class for verticles that mainly provides:
With a promise object that provide feedback (if the operations succeeded or not)
public class MainVerticle extends AbstractVerticle {
@Override
public void start(Promise<Void> promise) {
promise.complete();
}
}
Vert.x futures are not JDK futures: they can be composed and queried in a non-blocking fashion.
The Vert.x core APIs are based on callbacks to notify of asynchronous events. callbacks allow the implementation of different models that better cope with asynchronous programming:
@Override
public void start(Promise<Void> promise) throws Exception {
// When the future of prepareDatabase completes successfully, then startHttpServer is called and the steps future completes depending of the outcome of the future
// returned by startHttpServer. startHttpServer is never called if prepareDatabase encounters an error, in which case the steps future is in a failed state and becomes
// completed with the exception describing the error.
Future<Void> steps = prepareDatabase().compose(v -> startHttpServer());
// setHandler defines a handler to be called upon completion.
// ar is of type AsyncResult<Void>. AsyncResult<T> is used to pass the result of an asynchronous processing and may either yield a value of type T on success or a failure exception if the processing failed.
// AsyncResult (ar) is actually a super-interface of Future
steps.setHandler(ar -> { (1)
if (ar.succeeded()) {
promise.complete();
} else {
promise.fail(ar.cause());
}
});
}
You can deploy more than one verticle:
If you get an error like this:
2019-12-16T14:14:41.992+0100 SEVERE Task io.vertx.core.impl.TaskQueue$$Lambda$15/633121918@2b9dcb8c rejected from java.util.concurrent.ThreadPoolExecutor@5426275f[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.RejectedExecutionException: Task io.vertx.core.impl.TaskQueue$$Lambda$15/633121918@2b9dcb8c rejected from java.util.concurrent.ThreadPoolExecutor@5426275f[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at io.vertx.core.impl.TaskQueue.execute(TaskQueue.java:93)
at io.vertx.core.impl.ContextImpl.executeBlocking(ContextImpl.java:337
..................
it may comes from the fact that you deploy your verticle in a test unit like that:
vertx.deployVerticle(new DatabaseVerticle());
you need to add an handler
vertx.deployVerticle(new DatabaseVerticle(),context.asyncAssertSuccess());