First Vert.x Application
In this tutorial, we are going to discuss about the First Vert.x application. It’s finally time for us to write a First Vert.x application! Before we look into the details of Vert.x, we are going to bootstrap our First Vert.x Application.
Vert.x is a toolkit for building reactive applications on the JVM, and it’s polyglot, meaning you can use it with multiple languages (Java, Kotlin, JavaScript, etc.). For this example, I’ll guide you through creating a basic Vert.x application in Java. To build your first Vert.x application, follow this step-by-step guide.
For this we are going to use the website start.vertx.io. With this website we can easily create our first vert.x application. I am going to use the latest version of vert.x 4.x for this tutorial. If you are still on vert.x 3.x, you should definitely upgrade. I recommend to use version 4.x. Then we can choose the language. We have Java and Kotlin. I am going to use Java and next we need to choose build tool we can decide between Maven and Gradle. I am going to use Gradle. Next I’m using my groupid. You can choose your own. And for the artifact id I am using vertx-starter
. Finally On the bottom we see a dependency section, so it’s possible to define multiple dependencies as vert.x is very modular. For this section we don’t need any dependency as we focus on the vertex core functionality.
In the advanced options field we can specify another JDK version. I am using the latest version, Java 17. After that, we can generate a project and this will download a vertex application as zip file.
So let’s open this in our file Explorer and then we can unpack the zip file and open this project in our ID of our choice. Depends what you prefer for developing. I’m going to use IntelliJ, which is one of the most popular Java ides. In the menu file, we can press open and then IntelliJ asks us for a path where the project is located. So I’m navigating to the project. Click on the folder and press open on Project Import. The gradle build will start automatically, download all dependencies and then the project structure should look like.
Here we have our src/main/java folder and src/test/java folder where we have one main verticle and one test. The main verticle is the entry point of a vertex application and the test main verticle shows if everything works.
package com.ashok.vertx.vertx_starter;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
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 from Vert.x!");
}).listen(8888).onComplete(http -> {
if (http.succeeded()) {
startPromise.complete();
System.out.println("HTTP server started on port 8888");
} else {
startPromise.fail(http.cause());
}
});
}
}
package com.ashok.vertx.vertx_starter;
import io.vertx.core.Vertx;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ExtendWith(VertxExtension.class)
public class TestMainVerticle {
@BeforeEach
void deploy_verticle(Vertx vertx, VertxTestContext testContext) {
vertx.deployVerticle(new MainVerticle()).onComplete(testContext.succeeding(id -> testContext.completeNow()));
}
@Test
void verticle_deployed(Vertx vertx, VertxTestContext testContext) throws Throwable {
testContext.completeNow();
}
}
So let’s try to start this and see if our setup is correct.
We see after some moments the test is green with the message Http server started on port, so by default on an empty application vertex starts an Http server on port 8888. As we have a project set up, let’s dive into the features of Vertex.
That’s all about the First Vert.x application with simple example. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Vert.x tutorials..!!