
Vert.x is an open-source, reactive polyglot platform or toolkit that runs on the JVM(Java Virtual Machine). We can think vert.x as an alternative to the JEE(Java Enterprise Edition). It provides a convenient way to implement reactive applications on the JVM. Reactive applications consists of components that send messages or events to each other.
Reactive applications are both scalable as workloads grow, and resilient when failures arise. Moreover, A reactive application is responsive as it keeps latency under control by making efficient usage of system resources, and by protecting itself from errors.
Vert.x is backed with a large ecosystem of reactive modules with just anything you need, when writing modern services: a comprehensive web stack, reactive database drivers, messaging, event streams, clustering, metrics, distributed tracing and more.
It comes with a different approach in the market to solve the issues such as developing networked and highly concurrent applications.
Vert.x is a toolkit to build distributed reactive systems on the top of the Java Virtual Machine using an asynchronous and non-blocking development model. We can use Vert.x as a toolkit in a standalone application or embedded in a Spring application by instantiate a Vert.x object and call methods on it to get Vert.x and do whatever we need. It does not provide an all-in-one solution, but provides the building blocks to build your own solution.
Vert.x is polyglot means, it allows you to write event-driven applications by developing components in the language that you think most appropriate for the task, such as Java itself, Kotlin, Scala, Ruby, and JavaScript.
It is event-driven, single-threaded, and non-blocking, which means you can handle many concurrent apps with a small number of threads.In this way, it lets your app scale with minimal hardware. It operates a single event loop, but it also has the ability to keep a thread pool to match the number of available cores. With greater concurrency support, Vert.x is suitable for not only IO but also CPU-heavy processes that require parallel computing.

Vert.x has some core concepts
– Verticle
– Event Loop
– Event Bus
Verticle
Verticles are chunks of code that get deployed and run. Verticles allow you to encapsulate your code for different needs, and can be run independently of each other.
An application would typically be composed of many verticle instances running in the same Vert.x instance at the same time. Verticle instances communicate with each other by sending messages on the event bus.
Event Loop
The event loop of Vert.x is similar in the existing asynchronous programming models. The method requestHandler is where the event loop delivers the request event. Its APIs are non-blocking and won’t block the event loop, but that’s not much help if you block the event loop yourself in a handler. But keep in mind the single important rule of non-blocking thread: don’t block it.

Event Bus
The communication between the Verticles occurs through messages that are managed and delivered by the event bus, which accepts two types of implementation: point-to-point and publish/subscribe, though Vert.x doesn’t provide a guaranteed delivery of the messages. Hence, there is a single event bus instance for every Vert.x instance.

If you are developing a public API, then vertx-core should be enough. If it’s a web app, you may add vertx-web which provides http parameter handling and JWT/Session authentication.
Let’s Code
To try out Vert.x, let’s create a simple Java project using maven.
Step-1: Create a new maven project and add vertx web dependency.
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>4.2.3</version>
</dependency>
public class MainVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
// Create a Router
Router router = Router.router(vertx);
// Mount the handler for all incoming requests at every path and HTTP method
router.route("/hello").handler(context -> {
// Get the address of the request
String address = context.request().connection().remoteAddress().toString();
// Get the query parameter "name"
MultiMap queryParams = context.queryParams();
String name = queryParams.contains("name") ? queryParams.get("name") : "unknown";
// Write a json response
context.json(
new JsonObject()
.put("name", name)
.put("address", address)
.put("message", "Hello " + name + " connected from " + address)
);
});
// Create the HTTP server
vertx.createHttpServer()
// Handle every request using the router
.requestHandler(router)
// Start listening
.listen(8888)
// Print the port
.onSuccess(server ->
System.out.println(
"HTTP server started on port " + server.actualPort()
)
);
}
}
The above code will create a vert.x web router and start an HTTP server on port 8888. It returns a JSON object on each request. The JSON object contains the query parameter names, the address of the request, and a greeting message.
To run the code, open a terminal and navigate to your project folder. Build the application as follows:
$ mvn package
Then, run the application:
$ mvn exec:java

Now that the server is up and running, try to send a request:
We are going to send a request as:
$ http http://localhost:8888\?name\=”Aasif Ali”
Output:

Conclusion
In this blog we have introduced vert.x library with example. The vert.x library is the evolving technology in the reactive programming and event driven applications.Vert.x gives you the freedom to make a slow transition from one language to another.