Vert.x - First steps and impressions

06.02.2020Stefan Welsch
Cloud Vert.X Java Kotlin Open source Hands-on

Vert.x is an open source framework developed by the Eclipse Foundation. Vert.x is an event-driven framework for the Java Virtual Machine.

What is vert.x?

First, let’s take a look at the definition of vert.x.

Vert.x is a polyglot, event driven and non blocking framework, which enables you to write reactive applications.

But what does polyglot, event driven, non blocking or reactive mean?

polyglot

Polyglot says that you can use vert.x in different languages. This is possible because the most common APIs for the supported languages are provided. So you can decide for yourself based on your requirements in which language you would like to implement them. Assuming that the selected language is one of the following languages: “Java, Javascript, Groovy, Ruby, Ceylon, Scala or Kotlin”. ;-))

Event driven

Don’t call us, we’ll call you.

As soon as an event of your choice occurs, Vert.x will notify you by sending events for this case. This means you can perform tasks asynchronously and still be notified when the task is done.

Non blocking

If software gets stuck during an essential calculation or some other computationally intensive operation, in most cases it is a worst-case scenario. The consequence of this is a long waiting time for the user before he receives his answer. Vert.x does not block threads. This makes it possible to take advantage of concurrency with a smaller number of threads.

Reactive

Reactive is the consequence of event driven and non blocking. This means that the program is built with asynchronous data streams. You can learn more about reactive programming in Andre Staltz’s Reactive tutorial .

Properties of vert.x

  • lightweight
    • Core Vert.x is 650kb
  • fast
    • You can find many benchmarks that confirm this.
  • no application server
    • no deployment necessary. Run your code whenever you want.
  • modular
    • There are many components available, so you can decide what you need.
  • simple, but not too simple
  • ideal choice for building lightweight, high-performance microservices
  • polyglot

Our first verticle

Before we dive into the wonderful world of vert.x, let’s clarify the term Verticle. A Verticle is any component that has the following properties:

  • single-threaded
  • event-driven
  • non-blocking

First, this is all you need to know about it. The following example shows you how easy it is to write a full web application.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
//1.
public class BNovaVerticle extends AbstractVerticle {

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

Now let’s take a closer look at the code.

  1. First the class BNovaVerticle is created and extended by the class AbstractVerticle.
  2. To implement our web server, we must first override the start() method of the AbstractVerticle class.
  3. Then we create the HttpServer and register the RequestHandler. As soon as a request arrives, a response with “Hello from b-nova” is sent.
  4. Then the port of the web server is set to 8080. As far as the server can be started on this port, we made it.

EventBusReader

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class EventBusReaderVerticle extends AbstractVerticle {

    @Override
    public void start(Promise<Void> startPromise) throws Exception {
//1.
        vertx.eventBus().consumer("myCustomAddress", message -> {
        System.out.println("message received = " + message.body());
        });
    }
}
  1. First, the Consumer is registered on the Event Bus. The consumer then listens to the specified address. (myCustomAddress ist just a String which you can freely choose)

Publisher

Now let’s take a look at the Publisher page. We modify our BNovaVerticle so that it publishes a message every time a request is processed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class BNovaVerticle extends AbstractVerticle {

    @Override
    public void start(Promise<Void> startPromise) throws Exception {
    
    vertx.createHttpServer().requestHandler(req -> {
    
    vertx.eventBus().publish("myCustomAddress", "Hello b-nova");
    
    }).listen(8888, http -> {
        if (http.succeeded()) {
        startPromise.complete();
        System.out.println("HTTP server started on port 8888");
        } else {
        startPromise.fail(http.cause());
        }
    });
    }
}

Every time a request is received, a message is sent over the event bus. If you register several recipients at the same address, the message will be sent to all recipients.

Conclusion

Note that this is just a small sample of what the vert.x framework offers. If you are building a lightweight and high-performance application, first take a look at all the practical modules that Vert.x provides .


This text was automatically translated with our golang markdown translator.

Stefan Welsch

Stefan Welsch – Pionier, Stuntman, Mentor. Als Gründer von b-nova ist Stefan immer auf der Suche nach neuen und vielversprechenden Entwicklungsfeldern. Er ist durch und durch Pragmatiker und schreibt daher auch am liebsten Beiträge die sich möglichst nahe an 'real-world' Szenarien anlehnen.