Vert.x Object

Vert.x Object

In this tutorial, we are going to discuss about the Vert.x Object. In Vert.x, the core object is the Vert.x object, which is the starting point for any Vert.x application. It acts as the container and runtime environment for managing the lifecycle of verticles, handling event loops, and providing access to core functionalities such as event bus, timers, HTTP servers, and more.

We have an application ready and now we want to do something with it. The whole vert.x framework builds around the vert.x object, which is the control center of vert.x. With this object, you can do a lot like creating servers. Clients interact with the event bus or setting timers.

The Vert.x object allows you to deploy and undeploy verticles. Verticles are the fundamental building blocks of a Vert.x application, similar to lightweight processes or actors.

Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new MainVerticle());

To start our application, we need a vert.x object. We can achieve this by creating one vert.x object in our main class as the Java main class acts as the entry point of a Java application. So In our main verticle class, I’m adding a Java main method.

package com.ashok.vertx.vertx_starter;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;

public class MainVerticle extends AbstractVerticle {

  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    vertx.createHttpServer().requestHandler(req -> {
      req.response()
        .putHeader("content-type", "text/plain")
        .end("Hello Welcome to Waytoeasylearn!");
    }).listen(8888).onComplete(http -> {
      if (http.succeeded()) {
        startPromise.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startPromise.fail(http.cause());
      }
    });
  }

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

In the above code main method we are creating a vertx object by writing Vertx.vertx();. We can assign this to a variable vertx and this is now the main control center. As I mentioned previously, we can do a lot with this vertx object. Now we want to deploy the main verticle so we can start our application. To do this, we are assigning the vertx instance to a new variable vertx and then we can call the methods of it with vertx.deployVerticle(new MainVerticle()), we can deploy our application. That means Vertx is deploying this application internally and the start(Promise startPromise) method above is called on line number 10.

Inside the verticle object start(Promise startPromise) we have a vertx instance again (Line number 11) and here a new Http server is created. Notice that this vertx instance is coming from the AbstractVerticle class. So each verticle what you are deploying has a reference to the vertx instance. If you want to check By pressing F3, we can look into the source code and we see the vertx instance is protected and it’s available in the abstract verticle class.

Vert.x Object

Now if we run our MainVerticle class then the application is starting up and you can see following output in the terminal console.

12:11:07 pm: Executing ':MainVerticle.main()'...

> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE

> Task :MainVerticle.main()
HTTP server started on port 8888

After some time we see a message Http server started on port 8888. So we can do now a rest API call to localhost:8888 and we should see a message for this.

$ curl localhost:8888
Hello Welcome to Waytoeasylearn!

Great..!! This was now our first interaction with Vertx. Let’s go back to Intelex and see how this works.

public void start(Promise<Void> startPromise) throws Exception {
    vertx.createHttpServer().requestHandler(req -> {
      req.response()
        .putHeader("content-type", "text/plain")
        .end("Hello Welcome to Waytoeasylearn!");
    }).listen(8888).onComplete(http -> {
      if (http.succeeded()) {
        startPromise.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startPromise.fail(http.cause());
      }
    });
}

We see in the start method a new Http server is created with vertx, createHttpServer() method and then Vertx is heavily using the builder pattern. So when the .createHttpServer() is called immediately afterwards, .requestHandler() method is called and in there we have a callback. The first parameter is req (request), and then for the req.response() is called .putHeader()and .end().

This is creating an Http response with content-type text plain and the message Hello Welcome to Waytoeasylearn!. And then after this the .listen() method is called on port 8888 and this is starting an Http server. When the http server is started, a message is logged. If not, it fails. And we can see a startPromise is called here in line number 8 in above snippet. This is an asynchronous callback. We will see in the upcoming tutorials how this is used.

We have learned how to create the vert.x object. Normally only one vert.x instance is needed for each application, so make sure to not create multiple ones and always rely on the instance references from the different verticles.

Vert.x Object Features
  • Core Engine: Handles event-driven, asynchronous behavior.
  • Verticle Management: Deploy, undeploy, and manage verticles.
  • Event Bus: A publish-subscribe mechanism for communication between components.
  • HTTP Server/Client: Easily create reactive web applications.
  • Timers: Schedule one-off or periodic tasks.
  • File System: Non-blocking file system access.
  • Worker Verticles: Handle blocking code without blocking the event loop.

The Vert.x object is central to managing and coordinating the behavior of a Vert.x application, allowing you to easily build scalable and reactive applications.

That’s all about the Vert.x object with simple example. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Vert.x tutorials..!!

Vert.x Object
Scroll to top