Vert.x - Verticle

About

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.

Features

Verticles:

  • are technical units of deployments of code in Vert.x.
  • have a simple start / stop life-cycle,
  • can deploy other verticles.
  • communicate through asynchronous message passing delivered by the event bus
  • can received some configuration (e.g., credentials, network addresses, etc)
  • can be deployed several times

Management

Deploy

Create

io.vertx.core.AbstractVerticle is the base class for verticles that mainly provides:

  • life-cycle start and stop methods to override,
  • a protected field called vertx that references the Vert.x environment where the verticle is being deployed,
  • an accessor to some configuration object that allows passing external configuration to a verticle.

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:

  • reactive extensions (via RxJava - streams of processed events),
  • promises and futures,
  • fibers (using bytecode instrumentation),
  • etc.
@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());
  }
});
}

Start

You can deploy more than one verticle:

Support

RejectedExecutionException from ThreadPoolExecutor

If you get an error like this:

2019-12-16T14:14:41.992+0100  SEVERE  Task io.vertx.core.impl.TaskQueue$$Lambda$15/[email protected] rejected from [email protected][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/[email protected] rejected from [email protected][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());

Powered by ComboStrap