There are many design patterns and development strategies available nowadays, and they range from traditional to modern and flexible. These allow architects and developers to analyze and test the best approach for each project. Like design patterns and development strategies, frameworks are also being created to improve quality and productivity. Recently, one framework that caught my attention was Vert.x.
Vert.x was an official Eclipse Foundation project started by Tim Fox in 2011/2012 and empowered by Red Hat. It contains many of the benefits presented by Node.js, plus the benefits of running in a JVM.
Vert.x is a Java framework that allows you to write event-driven applications by developing components in the language that you think most appropriate for the task, such as Java itself, Kotlin, Scala, Ruby, and JavaScript. Like Node.js, Vert.x operates a single event loop, but it also has the ability to keep a thread pool to match the number of available cores. With greater concurrency support, Vert.x is suitable for not only IO but also CPU-heavy processes that require parallel computing.
The concept of loosely coupled components in Vert.x is called Verticle. It has some of the same concepts as actor model, but not all. As mentioned before, Verticles can be implemented in different supported languages by using two methods: start() and stop().
Another important concept in Vert.x is the event bus. The communication between the Verticles occurs through messages that are managed and delivered by the event bus, which accepts two types of implementation: point-to-point and publish/subscribe, though Vert.x doesn't provide a guaranteed delivery of the messages.
Image courtesy of javaworld.com
One interesting thing about this architecture is that Verticles stay idle until receiving a message to execute an action.
The concept seems good, so why don't we try it out with some code?
To try out Vert.x, let's create a Java project. You can choose to work with Maven or Gradle. After creating the project, import the following dependencies:
- Maven: (in your pom.xml):
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.8.1</version>
</dependency>
- Gradle: (in your build.gradle file)
dependencies {
compile 'io.vertx:vertx-core:3.8.1'
}
Since the building blocks of Vert.x are Verticles, we'll create our first Verticle:
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
public class MyFirstVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> startFuture) throws Exception {
vertx
.createHttpServer()
.requestHandler(r -> {
r.response().end("<h1>Hello from my first " +
"Vert.x 3 application</h1>");
})
.listen(8080, result -> {
if (result.succeeded()) {
startFuture.complete();
} else {
startFuture.fail(result.cause());
}
});
}
}
So far so good. Now we just have to deploy the Verticle and start the application:
import io.vertx.core.Vertx;
public class VertxVerticleMain {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new MyFirstVerticle(), stringAsyncResult -> {
System.out.println("Verticle deployment complete");
});
}
}
To sum it up, Vert.x is a great framework when you want a non-blocking strategy for an event-driven application. You may think that you can develop the application, but you will have problems with the concurrency when querying the database. But don't worry, because there are some non-blocking connectors. On the order hand, working with this framework will be a bit of a challenge due to lack of documentation and examples. All in all, however, for an application that has to keep its state (context, memory content, and so on) consistent and preserved, Vert.x is a good option due to the fact that each Verticle provides a thread-safe environment by isolating its state.
Author
João Moráveis
João Moráveis is a Java Engineer at Avenue Code. He loves reading and studying, and he is enthusiastic about computer vision, image processing, and artificial intelligence.