Hello Vert.x World!

From out previous 2 posts, we had been explaining a bit of background and preparation to be able to start writing Vert.x code. In this post, we will actually start to write some Vert.x code and examine some of it's features.

Our First Vert.x Application

Get The Vert.x Runtime

Vert.x has a command-line runtime for deploying purely Vert.x applications. It can be downloaded from the Vert.x web site:

Using the Vert.x runtime, you can launch simple Vert.x applications like the ones in this chapter by simply running:

vertx run Verticle.(java|js|groovy|rb|scala|ceylon|py|kt)

Once you have downloaded the Vert.x runtime and extracted it, you will need to ensure that the bin/ directory is in your execution path.

Writing Our First Verticle

Without further delay, let’s write some code.

HelloWorld.java
import io.vertx.core.AbstractVerticle;

public class HelloWorld extends AbstractVerticle {

    @Override
    public void start() {
        int period = 100;
        vertx.setPeriodic(
                period,                                 (1)
                t -> System.out.println("Hello World")  (2)
        );
    }
}
1 The period of the repeating task in milliseconds
2 A Lambda which will be executed

This is about the simplest example of a Vert.x application that I can demonstrate. This is a single Verticle which sets up a timer which executes a Callback every period milliseconds. The callback prints out the words “Hello World” to the console. We see in the code above that the setPeriodic method takes 2 arguments: A period in milliseconds, and a callback to be executed on that period. In this case, the callback is in the form of a Lambda.

Taking It To The Web

The first example was pretty easy, so let’s make it a little more challenging. Let’s create a web server which returns “Hello World” every time we make a request.

HelloWebWorld.java
import io.vertx.core.AbstractVerticle;

class HelloWebWorld extends AbstractVerticle {

    @Override
    public void start() {
        int period = 100;
        vertx.createHttpServer().requestHandler(req -> { (1)
          req.response()
             .putHeader("Content-Type", "text/plain")
             .setStatusCode(200)
             .setStatusMessage("OK")
             .end("Hello Web World");  (2)
        }).listen(8080, "0.0.0.0");
    }
}
1 Create a Lambda which will be called each time an HTTP request is received by our server
2 The response is not actually sent UNTIL end() is called.

Something to take particular note of in this example is that all of the response methods are chained together using what is called a Fluent API. Each method returns the original object, with it’s state modified, so that more methods on that same object can be called instead of having to make separate statements for each method.

That's a great start. We're already handling web requests in a reactive, non-blocking, event-driven manner! Tune in tomorrow when we take the next step into using network sockets over TCP and UDP!

Comments

Popular Posts