Vert.x Object
Welcome to another easy to understand tutorial on Vert.x! In this guide, we will explore one of the most important components of Vert.x — the Vertx
object. Whether you’re just getting started or want to understand Vert.x more deeply, this tutorial is crafted for all audiences, including absolute beginners.
What is the Vert.x Object?
In Vert.x, the Vertx
object is the core engine and entry point of every Vert.x application. Think of it as the control center. It manages:
- Event loops (the heart of Vert.x’s asynchronous processing)
- Deployment and lifecycle of Verticles
- Access to core features like HTTP servers, event bus, timers, and file system
Don’t worry about these key words like Event loops, Verticles, etc. We will learn about these in upcoming tutorials.
You must create a Vertx
object before doing anything in your Vert.x application.
🛠️ Setting Up a Simple Vert.x Application
Let’s see the Vertx
object in action with a basic example. Here’s how we can use it to deploy a Verticle and start an HTTP server.
package com.ashok.vertx.vertx_starter;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
/**
*
* @author ashok.mariyala
*
*/
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());
}
}
What’s Happening Here?
- Main Method:
Vertx vertx = Vertx.vertx();
— This creates a new Vert.x instance.vertx.deployVerticle(new MainVerticle());
— Deploys your custom Verticle.
- Inside the Verticle:
vertx.createHttpServer()
— Starts an HTTP server..requestHandler(...)
— Handles incoming HTTP requests..listen(8888)
— Listens on port 8888..onComplete(...)
— Asynchronous callback to check success or failure.
Output
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!
Understanding Vertx Instance in a Verticle
Each Verticle that you deploy using Vert.x automatically gets access to the same Vertx instance. This instance is inherited from the AbstractVerticle
base class.
You can explore this by pressing F3
(Go to Definition) in IntelliJ. You’ll see that the vertx
field is protected and available to every Verticle.

Best Practices
- Avoid creating multiple
Vert.x
objects unless you have a specific need. - Only one
Vertx
instance is typically needed per application. - Reuse this instance across Verticles for better resource management.
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.
Summary
The Vert.x
object is the foundation of every Vert.x application. It helps manage Verticles, initiate servers, and facilitate communication across your app. Understanding how to create and use it is the first step toward mastering Vert.x.
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..!!