Vert.x is an application platform to write high performance, massively scalable, async applications that run on the JVM using the language of your choice (Java, JavaScript, CoffeeScript, Ruby, Python, Clojure or Groovy). Vert.x's ability to scale to handle huge number of concurrent connections is a result of using non blocking I/O and message passing architecture. All communications between the different application components go through the distributed Event Bus, even if they reside on different nodes.

Vert.x provides a simple programming model to write applications: Verticles. An application consists of one or more verticles talking to each other using messages passed through the event bus. There is one simple rule that you must not violate: DON'T BLOCK. Vert.x internally uses event loops to do the work. An event loop is a thread that does the following:

  1. it looks for new events(example: HTTP response received)
  2. find handlers that asked to handle that event
  3. execute the handler passing this event to it
  4. Go to 1

If you do any blocking operation in the verticle, then it will block the event loop. This will result in events piling up and other verticles waiting, and the whole thing will eventually collapse. So there is one "Golden Rule - Don't block the event loop!".

There is one common pattern you will always use: callbacks. You ask for something and register a handler that will be "called back" when the result of your request is ready. For a server, for example, you ask to create an HTTP server, and if there is a new request, you provide a handler to serve it.

HttpServer server = vertx.createHttpServer();

server.requestHandler(new Handler<HttpServerRequest>() {  
    public void handle(HttpServerRequest request) {"A request has arrived on the server!");

server.listen(8080, "localhost");  

For a client, you provide the request(it is also an event that will be handled by the event loop, not sent synchronously in the same thread), and a handler that will be "called back" when the response is ready.

HttpClient client = vertx.createHttpClient().setHost("");

HttpClientRequest request ="/some-path/", new Handler<HttpClientResponse>() {  
    public void handle(HttpClientResponse resp) {"Got a response: " + resp.statusCode());


Another example is when you send a message using the event bus and you need a reply. Again, you give the message and specify a handler that will be called back when the reply is received.

eventbus.send("test.address", "This is a message", new Handler<Message<String>>() {  
    public void handle(Message<String> message) {
        System.out.println("I received a reply " + message.body);

Vert.x provides TCP/SSL, HTTP/HTTPS and WebSockets servers and clients out of the box.

If you must write blocking code, Vert.x gives you the option: Worker Verticles. Worker verticles are not executed by an event loop thread. Intead, worker verticles are executed by threads from worker thread pool. However, using worker verticles that block does not scale and you should take this into consideration when designing your application if you expect a large number of conncurent connections.

All the code snippets above are from Vert.x documentation.

Abdulaziz AlMalki

Read more posts by this author.