Vert.x Java

Reading Time: 6 minutes

An open source polyglot platform or tool kit is called Vert.x in Java. Vertx Forum works with JVM (Java Virtual Machine). We can say that it is one of the JEE. It comes with a unique way to market solutions for problems such as Java as developing networked and highly compatible applications. It shares the same functionality in all supported languages ​​such as Java, Ruby, Kotlin, Scala, and JavaScript.

Vertx is a toolkit, so we can embed it in our standalone Java program. We can use it to validate the Vertx item and call the path to it. It also works as a platform, so we can configure it using the command line and specify the components that it wants to work with.

Vertx Overview

Vert.x is a toolkit or platform for running applications running on Java Virtual Machine (JVM). Now, what does that mean? And what does the interior design of Vert.x look like? We will look in detail here.

Vertx : Verticles

Vert.x can use and operate components called Verticles. You can think of verticles like servlets or EJB driven messages in the Java Enterprise Edition model. Here is a simple diagram showing the Vert.x forum with 4 active verticles:

Vertx Verticles

The Vertx Event Bus

Verticles are run by event, which means they do not work unless they receive a message. Until then they are asleep. Verticles can be contacted via the Vert.x event bus. This diagram shows how the verticles connect with the Vert.x event bus:

Event Bus Vertx

Messages can be simple objects (e.g. Java objects), strings, CSV, JSON, binary data or whatever else you need.

Verticles can send and listen to addresses. The address is the same as the named channel. When a message is sent to a given address, all the verticles that listen to that address receive the message. Verticles can register and remove themselves from the address list without the senders knowing. This results in loose links between the senders and the recipients.

All message handling is asynchronous. If a verticle sends a message to another verticle, that message is first put on the event bus, and control returned to the sending verticle. Later, the message is dequeued and given to the verticles listening on the address the message was sent to.

The Vert.x Thread Model

Verticles run in single thread mode. That means, that a verticle is only executed by a single thread, and always by the same thread. That means that you will never have to think about multithreading inside your verticle (unless you yourself start other threads which your verticle communicates with etc).

Vert.x is capable of using all the CPUs in your machine, or cores in your CPU. Vert.x does this by creating one thread per CPU. Each thread can send messages to multiple verticles. Remember, verticles are event driven and are only running when receiving messages, so a verticle does not need to have its own exclusive thread. A single thread can distribute messages to multiple verticles.

When a thread delivers a message to a verticle, the message handling code of that verticle is executed by the thread. The message delivery and message handling logic is executed by calling a method in a handler (listener object) registered by the verticle. Once the verticle’s message handling logic finishes, the thread can deliver a message to another verticle.

Installation : Vertx

Installation of Vert.x is very easy. It is distributed in a zip file with large Jar files. We simply opened the file and added all the JAR files to our Java application class path. After adding all the JAR files, we are ready to use Vert.x in our application.

We can download the zip file from Maven Central or Bintray.

Let’s start with the new vert.x web project, but before that, we need to make sure our system has the following features:

  • JDK 1.8 or higher.
  • IDE or text editor.
  • Maven 3 or higher.
  • A Browser for HTTP requests

To create the first web project using Vert.x, we need to follow the following steps:

1) We go to https://start.vertx.io/ by creating a new project. We see the screen as:

Go to start.vertx.io

2) We select the Vert.x version, which we want to use in our application. We prefer language, Java or Kotlin, according to our interests. We also chose to build on Maven and Gradle. In our case, we use Maven. We complete the Group Id fields and Artifact Id. Also add Vert.x web as a dependency. After adding the dependency, we click the Generate Project button to create the project.

add vertx web as a dependency
Click on generate project

3) When we click the Generate Project button, it downloads the zip file to our computer. We open it in our favourite folder.

Extract the downloaded zip

The generated folder contains a maven build descriptor, i.e., pom.xml. Pom.xml is designed to build and run our app. Contains test sample and verticle using JUnit 5. Forcing code style, it also contains editor configuration. Finally, by ignoring the files, it contains Git configuration.

4) Now, we will open that Vert.x project in any IDE or editor we like and navigate to the following path:

src / main / java / com / example / starter / MainVerticle.java

Open in any IDE

The MainVerticle.java section contains a verticle sample that starts the HTTP server.

Create Vertx Instance

The first step to using Vert.x embedded in your own Java application is to create a Vertx instance. Here is how you create a Vertx instance:

import io.vertx.core.Vertx;

public class VertxApp {
    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
    }
}

You create a Vertx instance by calling Vertx.vertx().

The Vertx instance creates a number of threads internally to handle the exchange of messages between verticles. These threads are not daemon threads, so they prevent the JVM from shutting down, even if the main thread creating the Vertx instance terminates.

Creating a Verticle

The Vertx instance by itself doesn’t do much except all the thread management, creating an event bus etc. which are all communication and infrastructure tasks. In order to get the application to do something useful, you need to deploy one or more verticles (component) inside the Vertx instance.

Before you can deploy a verticle you need to create it. You do so by creating a class that extends AbstractVerticle. Here is a verticle example:

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!");
    }

A verticle has a start() and a stop() method which are called when the verticle is deployed and when it is undeployed. You should perform any necessary initialization work inside the start() method, and any necessary cleanup work inside the stop() method.

Deploying a Verticle

Once you have created a verticle you need to deploy it to the Vertx instance. You deploy a verticle using one of the deployVerticle() methods on the Vertx instance. Here is a Vert.x verticle deployment example:

import io.vertx.core.Vertx;

public class VertxVerticleMain {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(new MyVerticle());
    }
}

The verticle is deployed using this method call: vertx.deployVerticle(new MyVerticle());

This method call deploys the MyVerticle instance passed as parameter to the deployVerticle() method.

The Vertx instance has another deployVerticle() method which takes the fully qualified class name of the verticle to deploy as parameter. Here is how deploying the MyVerticle using that method would look:

vertx.deployVerticle(“examples.vertx.MyVerticle”);

There are more options available for deploying verticles. For instance, you can specify how many verticle instances of a given verticle class to deploy.

knoldus

Written by 

Prakhar is a Software Consultant at Knoldus . He has completed his Masters of Computer Applications from Bharati Vidyapeeth Institute of Computer Applications and Management, Paschim Vihar . He likes problem solving and exploring new technologies .