Vert.x - First steps and impressions

- Stefan Welsch

Introduction

Vert.x is an open source framework and will be developed by the Eclipse Foundation.

What is vert.x

Let's start with 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 means "polyglot", "event driven", "non blocking" and "reactive"?

Polyglot

Ployglot means that you can use vert.x in different languages as it provides idiomatic APIs for supported languages. So you can choose for yourself which language to implement suits best your requirements or solves your problem at hand. (provided that "Java, Javascript, Groovy, Ruby, Ceylon, Scala or Kotlin" is one of it ;-)).

Event driven

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

When things happen in Vert.x and you are interested in, Vert.x will call you by sending events for these things. That means you can execute tasks asynchronously and you will still get notified, given the task once finished.

Non blocking

The worst scenario with software is if it hangs up during some crucial calculation or other computationally intensive operation. The result is that users have to wait a long time before getting a response from the application. Vert.x does not block threads. As such, it is possible to handle a lot of concurrency using just a small number of threads.

Reactive

Reactive is the consequence of "event driven" and "non blocking". It means that the program is build with asynchronous data streams. You can read more about "reactive programming" on Andre Staltz's Reactive tutorial

Characteristics of Vert.x

  • Lightweight - Core Vert.x is 650 KB
  • Fast - You can find a lot of benchmarks that can verify the speed :-)
  • Not an application server - No deployment necessary. Run your code wherever you want
  • Modular - Lots of components available and you can choose what you need
  • Simple but not simplistic
  • Ideal choice for creating light-weight, high-performance, microservices.
  • Polyglot

Our first verticle

Before we will run in the fantastic world of Vert.x, let me explain what a "verticle" is:

A verticle is a single-threaded, event-driven, and non blocking component that receives events and produces responses.

First off, this is everything you have to know about it.

Enough of theory now. Let's start with an easy code sample to showcase you how easy it is to write a full web application. This example is written in Java.

1) public class BNovaVerticle extends AbstractVerticle {

  @Override
  2) 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());
      }
    });
  }
}

Let's have a closer look at the code.

  1. We created a new class BNovaVerticle and we extend AbstractVerticle.
  2. To implement our web server, we have to override the start method from the AbstractVerticle class.
  3. We create the HttpServer and register an requestHandler. If there is an incoming request the response is "Hello from b-nova"
  4. Now we bind our webserver to the 8888 port. If the server can start on this port, we complete the Future.

Event Bus - How verticles communicate

So let's go one step further. In some cases, it is necessary that one verticle has to communicate with another one.

Imagine you have a verticle that reads a file from the file system and another verticle needs the very version of said file as input.

This is the time where the event bus comes into play. A verticle can send or read messages via the event bus to other verticles.

EventBusReader

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. We register the consumer at the event bus. This way, the consumer is listening on the specified address. ("myCustomAddress" is just a string and you can choose any name you want)

Publisher

Let's also have a look at the publisher side. We modify our BNovaVerticle to publish a message every time a request is being handled.

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 being received, a message will be published via the event bus. (If you register more than one receiver on the same address, the message will be pushed to all consumers)

Conclusion

Note that this is just a small excerpt of what Vert.x framework has to offer. If you want to create a lightweight and high-performance application make sure to get a closer look on all the powerful modules Vert.x provides for you.