Vert.x — Getting Started

Shubhamspeaks
Engineering @ Akiro
6 min readOct 20, 2021

“You don’t have to be good to start … you just have to start to be good!”

In the last post, we have explained the reactive frameworks and a glimpse of the Vert.x and how it helps us to build the Akiro MQTT which is one of the high throughput systems which can handle 10’s of millions of connections. Here we will discuss how we can create applications with the help of Vert.x and its entities.

Vert.x Architecture

Vert.x Reactive

Vert.x — X represents polyglot, hereby it is Polyglot in nature. Vert.x is the toolkit for building the Reactive applications on the JVM. Reactive applications consist of components that send messages and events together. Vert.x operates on event loop-based processing and also has the ability to have a thread pool. It is event-driven and non-blocking. This means you can handle a lot of concurrency in your applications using a small number of threads, it lets your applications scale with minimal hardware.

VX — Setup

To use the Vert.x we need to add these maven dependencies in our project.

To get the latest version, please refer to the link.

VX — Verticles

Vert.x has a processing component called a Verticle and it’s the smallest functional component in Vert.x architecture. It is typically a collection of code that serves a single purpose.

A Verticle can be created by extending the AbstractVerticle Class. Each Verticle has an entry point which is defined by the start() method and exit by the stop() method in a class that is derived from the AbstractVerticle. We need to override these methods to add our application logic.

A Simple Verticle

That’s it. We have created our first Verticle Successfully :)

HTTP Server

Here we have created the HTTP Server in a single statement, that’s the power of Async programming.

VX -Deploy Verticle

We can deploy a verticle by the below method call.

We can deploy any verticle and scale the verticle with the help of Deployment options.

VX — Event Bus

As Vert.x is distributed in nature, it needs some mechanism to communicate with each other. This is where the event bus comes into play. The event bus is how Vert.x allows for simplified concurrency throughout the system. Here we will be looking the how the communication happening between the 2 verticles with the help of the Event Bus.

EventBus Sender Verticle

EventBus Receiver Verticle

We can deploy the Verticle below. The names of the 2 verticles are RV1 and RV2.

This will be sent to at most one of the handlers registered to the address.

We will consume the messages which are sent to this address.

This way we can handle the event bus communications.

VX — Clustered Event Bus

The Cluster management in Vertx uses the HazleCast. This is the default cluster manager, but we can plugin other implementations also. It has many useful functions like

  • Discovery of nodes
  • Distributed Map, Locks, and Counters
  • Maintain topic-wise subscribers lists.

To set up the cluster mode, we need to have the cluster.xml file in our classpath. Below is the example of cluster.xml.

Clustered Sender Verticle

This sender Vert.x will send the message to the cluster where it will send the message to all the verticles which are there to consume the messages from the same address.

Cluster Receiver Verticle

The below receiver verticle gets registered to the cluster and is ready to consume the messages which are sent to the “anAddress”.

You can find the full Github code link here.

My sample code to show usage of Vertx and Event Bus concepts can be found here.

Akiro with Vert.x MQTT

Akiro MQTT broker leverages the Vert.x framework and is one of the High Scale Messaging and Connectivity throughput systems that can concurrently handle 10’s of Millions of connections in a single cluster deployment. Because of its reactive nature, it can handle parallel requests in a resource-efficient way.

Akiro Broker Architecture

Akiro Broker Based on Vert.x serving for multiple verticles

The platform is also equipped with an event processing engine to evaluate rules and receive instant alerts and notifications. The analytics capabilities of Akiro provide rich actionable insights to improve business efficiency. In our Subsequent blogs, we will speak about how Vert.x helped to create a High-Performance Messaging and Connectivity System like Akiro-MQTT. To use our Akiro MQTT broker click on the Sign-Up.

Conclusion

Summarising the above, Verticle encapsulates the technical functional unit for events in the form of exposing HTTP API, responding to API requests, requests to third-party systems, or interfaces on top of databases. Its polyglot nature can help you include multiple languages in your application. If you have worked with Node, probably Vert.x is an enhanced version of the same. Instead of dealing with a single thread in Node, Vert.x handles multiple threads independently.

Since it is an evolving community, the document could be less and you can find real-time challenges while working and debugging applications. The non-blocking nature of the framework makes it difficult to read, write and debug. You can use a third-party integration or an API straight away as you would need to know the implementation before using it. But it is a highly rewarding experience once you have cracked the concepts. The performance gain from a non-blocking code is tremendous. Also, the callbacks in a reactive paradigm modular approach helps to decouple the code. It covers almost every feature that requires an application to be deployed in production.

--

--