Java - Vert.X Framework
Table of Contents
About
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., database connection, monitoring, authentication, logging, service discovery, clustering support, etc). Vert.x is based on the Netty project.
Vert.x implements the reactor pattern. In practice, this means many concurrent requests can be handled by the same thread, thus preventing usage of ThreadLocals to store contextual data.
Vert.x is an event-driven, reactive, non-blocking, polyglot framework to implement microservices.
Because Vert.x was designed for asynchronous communications it can deal with more concurrent network connections with less threads than synchronous APIs such as:
- or java.net socket classes.
The code is already written for scaling up: asynchronous processing of events.
Usage
- high volume message
- event processing,
- micro-services,
- API gateways,
- HTTP APIs for mobile applications,
- etc
Concept
Two concepts:
- event bus (allows verticles to communicate)
Vert.x is almost entirely non-blocking (of kernel threads) - this allows it to handle a lot of concurrency (e.g. handle many connections, or messages) using a very small number of kernel threads, which allows it to scale very well.
Threading model
Synchronous
Synchronous I/O: Many networking libraries and frameworks rely on a simple threading strategy: each network client is being assigned a thread upon connection, and this thread deals with the client until it disconnects. This is the case
Too many concurrent connections will hurt scalability as system threads are not cheap, and under heavy loads an operating system kernel spends significant time just on thread scheduling management.
Asynchronous
A verticle processes incoming events over an event-loop, where events can be anything like receiving network buffers, timing events, or messages sent by other verticles.
Module
- Vert.x JDBC client to provide an asynchronous API over JDBC. - provides access to any JDBC-compliant database
- MySQL and PostgreSQL client libraries - these libraries offers better performance by working with these 2 database server network protocols rather than going through the (blocking) JDBC APIs.
- Apache Cassandra, OrientDB or ElasticSearch
Launching a verticle
App
- Verticle
package examples.vertx;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
public class MyVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> startFuture) {
System.out.println("MyVerticle started!");
}
@Override
public void stop(Future stopFuture) throws Exception {
System.out.println("MyVerticle stopped!");
}
}
- Main
import io.vertx.core.Vertx;
public class VertxApp {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new MyVerticle());
}
}
Installation
the Vert.x command line makes it easy to deploy verticles implemented in different programming languages in the same Vertx instance.
vertx -version
vertx run javascript:my-verticle.js
vertx run com.package.MyVerticle -cp MyVerticle.jar;thirdLib.jar
Snippet
Documentation
- https://vertx-starter.jetdrone.xyz/#gradle - To generate from OpenAPI file such as Petstore