What is Vert.x?
In my humble opinion, Vert.x is a poorly documented platform for creating server applications intended to be scalable by using an event-driven, non-blocking I/O in the JVM.
The first definition for Vert.x I heard was “it’s like Node.js but in the JVM”. (Yeah!, part of my first definition has been copied from the description that can be found at Node.js)
OK! Let’s take a look to the Vert.x site:
“Vert.x is a lightweight, high performance application platform for the JVM that’s designed for modern mobile, web, and enterprise applications.”
Terrific! Furthermore:
- It’s polyglot: you can write your application components in Java, JavaScript, CoffeeScript, Ruby, Python or Groovy. And you can even mix all these languages. Sincerely, I don’t find this feature exciting.
- It has a simple APIs for writing non-blocking network enabled applications.
- It’s scalable because it uses non blocking I/O to serve many connections with minimal threads plus passing messages to handle the logic of the application.
- It provides a simple actor-like concurrency model, so that you don’t have to worry about multi-threaded programming anymore.
Well! I still miss something here.
Taking a look to the Key Features you’ll learn that Vert.x has an Event Bus, a kind of Queue that will use the Vert.x components, called Verticles, to communicate between them regardless their programming language. It uses WebSockets and SockJS to achieve the JavaScript penetration that they claim.
Vert.x is a platform that you invoke from the command line, vert.x run
for single Verticles or vert.x runmod
for the encapsulation system that Vert.x provides: the module system (that can be shared via Maven repository or Bintray and can be registered in the module registry). But it also can be embedded in a Java application.
Install Vert.x
Installing Vert.x is very easy:
- You need a Linux, OS X or Windows with JDK 1.7.0 or later installed (try
javac -version
in order to ensure that you have the JDK bin directory on yourPATH
). - Download the latest release of Vert.x, 2.1.1 at the time of this writing.
- Decompress the download file. I like to have an Applications directory in my home directory, so:
tar -zxf ~/Applications/vert.x-2.1.1.tar.gz
will work right. - Add the Vert.x bin directory to your PATH environment variable.
Now you can check the version:
1 2 |
$ vertx version 2.1.1 (built 2014-06-18 14:11:03) |
The first example
Testing the install is as easy as write a simple web server. This example will show the main features of Vert.x: simplicity, scalability, concurrency. With a few more examples, the polyglot would be showed as well.
Copy the following into a text editor and save it as Server.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Main { import org.vertx.java.core.Handler; import org.vertx.java.core.http.HttpServerRequest; import org.vertx.java.platform.Verticle; public class Server extends Verticle { public void start() { vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() { public void handle(HttpServerRequest req) { //String file = req.path().equals("/") ? "index.html" : req.path(); //req.response().sendFile("webroot/" + file); req.response().end("Hello World!"); } }).listen(8080); } } |
Now run this Verticle (more on the Vert.x concepts later) by opening a console in the directory where you saved the file, and typing:
1 2 |
~/Documents/vert.x$ vertx run Server.java Succeeded in deploying verticle |
To ensure that you have really succeeded with this verticle, open a web browser and go to http://localhost:8080 (see above the highlighted line 15, if you want to change the port number)
You have to see “Hello World!” (without the quotes)
Now you can stop the server by using Ctrl+C (Command-C in OS X)
1 2 |
^C ~/Documents/vert.x$ |
Regarding polyglot, you can find more or less the same sample at in JavaScript, Ruby, Groovy, Python and Clojure
You can see with this example another feature of Vert.x: you don’t need to compile Java code. I suppose that the authors wanted to give the same facilities that the other languages have, but they are script languages. I don’t find interesting this option, and it’s totally useless for big projects, that is better to be distributed as modules.
Next steps
We have seen almost nothing of Vert.x but the installation and a dumb test. In later posts, I will explain the Core Concepts and I will develop a Java Vert.x application using maven.