Vert.x - Verticle

Java Conceptuel Diagram

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.

verticles are similar to modules.

They are executing in their own context on top of a Vert.x instance.

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

See Vert.x - Launcher class

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/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());





Discover More
Java Conceptuel Diagram
Java - Vert.X Framework

Vert.x is not a framework but a toolkit: the core library defines the fundamental APIs for writing asynchronous networked applications, and then you can pick the useful modules for your application (e.g.,...
Java Conceptuel Diagram
Vert.x - Event Bus

The Vert.x event bus is the main tool for different verticles to communicate through asynchronous message passing. Code: io/vertx/core/eventbus/EventBus For instance suppose that we have a verticle...
Java Conceptuel Diagram
Vert.x - Event Bus Message

An event bus message consists of a body, options, and it can optionally expect a reply The event-bus allows passing any kind of data (JSON is the preferred payload format - As Vert.x is polyglot, JSON...
Java Conceptuel Diagram
Vert.x - High availability deployment (HA)

When a verticle is deployed in High availability mode, it can fail over to any other nodes in the cluster started with the same HA group The HA mode can be defined in a run or start command A...
Java Conceptuel Diagram
Vert.x - Main Method

A Main method is one of the two start point of a Vert.x application. verticle Create a application script or a runnable jar. Example with the gradle application plugin ...
Java Conceptuel Diagram
Vert.x - Main Verticle

A main verticle is a main entry verticle that deploys other verticle.
Java Conceptuel Diagram
Vert.x - Promise/Future

promise and future in Vert.x Do not confuse futures with promises. futures represent the “read-side” of an asynchronous result, promises represent the “write-side”. They allow you to defer...
Vertx Service Helper Class
Vert.x - Services Proxy

A service is just a functionality that simplify Vertx code by generating boilerplate code that will handle all event bus operations. When creating a service there’s a certain amount of code to listen...
Java Conceptuel Diagram
Vert.x - Test

with
Java Conceptuel Diagram
Vert.x - The launcher run command

The run command (Code) is a command of the launcher. It will: create a instance...



Share this page:
Follow us:
Task Runner