Reactive application platform for Micro-services-Vert.x

P. Madanasekaran

Introduction:

Vert.x is “a platform on JVM for writing polyglot reactive applications. It is framework in Java similar to JEE or Spring, but reactive.  What is meant by reactive?  Reactive manifesto lays down the following criteria: 1) Responsiveness 2) Resilience and Fault-tolerance 3) Scalability 4) Event-driven-asynchronous and non-blocking. Tim Fox, the creator of Vertx, gives importance to the last two in saying “Event driven for scalability is a big part of reactive". What is an Event? What is Event-driven? Event is just an object. It can be message, a HTTP request, a socket connection an error or result. Just register a handler for each event and the Event loop thread will deliver the events after they arrive to the handlers in the order they were received. A handler is just a lambda or an anonymous function. Like Node.js, Vertx has been designed with “Event-Loop”. The idea is “to never block an OS thread”. Then the event-loop thread can deliver huge amount of available events in quick succession to their registered handlers within a short time. For example a single event loop can handle many thousands of HTTP requests very quickly. This capacity to service huge number of connections with a single thread or minimal number of threads is scalability .In Browser environment it was used earlier. Node used it in server side. Tim Fox was impressed by Node’s simplified, lightweight approach to application development- but not satisfied with its “only JavaScript approach”. So he decided to build Vertx on JVM, a more mature platform and make it polyglot. He built Vert.x on top of Netty.

Netty builds utility classes and functions on top of Java NIO packages. As an asynchronous event-driven network application framework, it makes it much easier to work with that programming model. Building on to top of the components provided by Netty, Vertx reduces the efforts required to build applications even further. A number of other asynchronous frameworks like Play, Akka and Twitter Finagle were built on Netty. Netty got the Duke’s choice award for 2011 and Vertx and Akka got the JAX award for most innovative Open/Java Technology in 2014 and 2014.Spring has just brought out “Project Reactor” which uses non- blocking and event driven approach. This shows that “reactive systems” are the trend now and this was developed to manage the  explosion in the number of mobile clients for the newer web apps ; Node.js is one of the first reactive frameworks. While Node uses call-back mechanism, Akka uses high-level abstractions like Actors, Streams and Futures. Vertx also uses actor-like Verticles but it also uses callbacks and provides Rx-ified (usingRxJava) versions for most of its APIs for use, if you prefer it. As now reactive systems have become main-stream, every framework claims it is “reactive” and provides “micro-services”. But a few asynchronous APIs cannot make them “reactive” as their other APIs will be “blocking”. To provide micro-services you have to write self-contained code and there should be no need to deploy it on some “server” which sits somewhere on the network. Akka, Vert.x and the Project Reactor from VMWare are the reactive frameworks that provide micro-services for developing Java applications.

Architecture:

The basic unit of deployment in Vertx is the Verticle, which resembles actors in Erlang and Akka.  Tim Fox had great experience in Messaging systems –he worked in JBoss Messaging systems and created HornetQ and also worked in RabbitMQ. This influenced his architectural choice of Event Bus as the communication medium for the Vertices. He calls Event-bus as the nervous system of Vert.x. They communicate through Event Bus, though they are coded in different JVM languages. Tim Fox sums up that the three greatest influences on Vertx as Node.js, Erlang/OTP and asynchronous messaging. As shown in the Figure-1 below, one or more Verticles run inside the Vertx instance, the run-time that runs inside a JVM instance.

Installation
You can download Vertx-3; for windows it is available as a zip file. You can unzip it to any directory and add the “../bin” to the system variable “path”. You may have to re-start the system. Go to command prompt and

execute
vertx –version

If you see the version number you can proceed further.

Sample   Have a look at the code below. Though we can embed Vertx in a regular Java code, we will see its use as an application platform. As a Vertx application mostly consists of one or more Verticles, we will be looking at how to code and deploy the Verticles. At first we use the command prompt to run the Vertx instance, as shown above. Later on we will use Maven to manage the dependency and package the app as a jar file like any other Java application.

MyVerticle.java

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
public class MyFirstVerticle extends AbstractVerticle {
@Override
public void start() {
vertx.createHttpServer().requestHandler(req -> req.response().end("Hello  world!")).listen(8080);
}
}

In the command prompt go the folder in which you saved the file and execute

vertx run MyVerticle.java

When you get the message “succeeded in Deploying Verticle”, go to your Browser and navigate to “localhost:8080” and you will see the “Hello World”

What happens?

  • “vertx” is a Vert.x instance and the run time within JVM. With it, you are able to run your Java file without any pre-compilation.
  • Vertx class has methods to provide core services, like creating HTTPServers, NetServers, Eventbus, Verticles and access to File System etc. The Vertx instance “vertx” can invoke them. The instance “vertx” is declared as a protected variable in AbstractVerticle class and “MyVerticle.java”, being its sub-class, can use the protected variable “vertx”.  Other services like database access, tools for writing sophisticated web apps and accessing Event- bus- Bridge etc are provided through separate asynchronous modules built on top of core.
  • As mentioned above, a Verticle is the unit of deployment in Vert.x. Every Verticle should implement start() declared in AbstractVerticle. An application may be a single Verticle or may consist of multiple Verticles that communicate with one another via the event bus.
  • As mentioned in point-1 and shown in Figure-1, Verticles run inside of a Vert.x instance. Each Vert.x instance runs in its own JVM instance and can run multiple Verticles concurrently. A host may run a single Vert.x instance or multiple ones.
  • Vert.x uses relatively few threads to create an event loop and execute Verticles. But in some cases a Verticle needs to do something either computationally expensive, or that might block, such as connecting to a database or using legacy code. When this happens Vert.x allows you to mark a Verticle instance as a worker verticle, in which case it will be executed by a background thread pool. Vert.x ensures that the worker verticles will not be executed concurrently, so you want to keep them to a minimum, but they are there to help you when you need them.
  • Vert.x is toolkit, not a container or "framework". That means we can use it within our existing application to give it the Vert.x  powers. For example we can use it within our Spring application, as many users do.
  • Vert.x uses a variation on the reactor pattern called "multi-reactor". So instead of having just one event loop it has multiple event loops(The platform just creates as many event loop threads as the number of CPU cores) , but it is  guaranteed that any specific handler will always be invoked by the same event loop. This means we can write our code as single threaded (not having to worry about synchronized, volatile etc) but still have it scale easily. So we get the benefits of the reactor model, but unlike pure reactor implementations (like Node.js) Vertx scales more easily over the cores of our server without having to deploy many server instances

Please have a look at Figure-2.

As mentioned earlier, Tim Fox calls Eventbus as the nervous system of Vert.x. As shown in Figure-2, only Verticles need not use event-bus. Similar to Event-Bus API in Vertx, there is JavaScript library “vertx-eventbus.js” for the use of clients. Using that, the clients can access event-bus. Vert.x uses SockJS servers to handle messages to and from the clients for event-bus. We will see more about this in the article on “Real time Web apps with Vertx-Web”.

Now just bear in mind 1) Verticle is the primary unit of deployment in a Vert.x application. 2) In order to use Vert.x, you need to extend the AbstractVerticle class and override the start() method, which is the entry-point to your Verticle. 3)  the AbstractVerticle class defines Vertx instance vertx as a protected member variable (that your Verticle inherits), which provides access to the Vert.x runtime. 4) The vertx object invokes the methods defined in the Vertx class and here the method createHttpPServer() was invoked.

Support for Java-8 in Vertx-3

Vertx-3 supports Java-8 and we get the advantage of using of lambdas or anonymous functions. The functional style of Lambdas makes programming against event style APIs much nicer. Vertx-3 is in tune with the Java World in changing the deployment model from module in Vertx2 to verticle and using Maven or Gradle to build and to manage dependencies; the blogs available as a part of Introduction to Vert.x series on the official web site “Vertx.io” and the samples on github are very handy to learn and use this framework. To the users who have not used any “lambda”, the lambda passed to createHttpServer() and the symbol “ -> “ in a java program may appear strange. But as we have earlier seen functional languages including Java-8 you should be familiar with this

We have not explicitly given the type of argument, as compiler can infer it. If you want, you can explicitly state it. As there is only one argument, no bracket “()” is used. More than one argument or no argument requires” ()” .As the body has only a single expression-chained method calls- no curly braces{} have been used. If it contains more than one statement separated by “;”, “{}” is essential.

Vert.x-web

Vert.x core provides a fairly low level set of functionality for handling HTTP, and for some applications that will be sufficient. Vert.x-Web builds on Vert.x core to provide a richer set of functionality for building real web applications, more easily. It’s the successor to Yoke in Vert.x 2.x, and takes inspiration from projects such as Express in the Node.js world and Sinatra in the Ruby world. We will see it in a separate article

Project  structure 
As shown above dependency management / build-tools are not a must but you cannot do without them when your dependencies grow and in tune with the Java world, Maven or Gradle can be used. We will use Maven.   We have to create a project folder and files and folders as shown below. ( if you have not installed Maven-3, go to Maven home page and do that first and set the environment variable MAVEN_HOME and edit “path” to add %MAVEN _HOME%\bin before proceeding further)

Pom.xml
Our project folder is named “firstVerticle ”. The code for “pom.xml”, required to use Maven, is given below. Don’t be overawed by it. You can use a file from the blogs and modify it for your needs.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.vertx.sample</groupId>
<artifactId>my-ang-app</artifactId>
<version>1.0-SNAPSHOT</version>
//dependencies
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>3.0.0</version>
</dependency> 
</dependencies>
<build>
<plugins>
<plugin>
<!-- maven compiler for Java-8 -- >
<artifactId>maven-compiler-plugin</artifactId>  
<version>3.3</version>
<configuration>
<source>1.8</source> 
<target>1.8</target>
</configuration>
</plugin>
<! We want to create fat.jar with dependencies included -- >
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>  
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<!—starter file -- >
<Main-Class>io.vertx.core.Starter</Main-Class>
<Main-Verticle>io.vertx.sample.ang.MyVerticle</Main-Verticle>
                  </manifestEntries>
</transformer>
</transformers>
<artifactSet/>
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
            </configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

We can use the verticle example given above now; we will gradually use the bigger examples in later articles.
Ensure that you package tree and verticle name matches with value in <Main-Verticle> element above. In the command prompt change to project directory and execute

mvn clean package   //( it will download the dependencies and create a fat.jar file)
and
java –jar target\ my-ang-app-1.0-SNAPSHOT.fat.jar (“created  fat jar file name from artifact-id and version no ”)

When you see the message “Succeeded in deploying the verticle” you can go to the Browser and navigate to localhost:8080 and see the hello world message.

Conclusion

Apart from Java-8, in 2014 and 2015, the recipients of JAX-INNOVATION awards were Vert.x, Hazelcast(Clustering tool in Vert.x),Akka and NetFlix –all connected to Reactive programming. Tim Fox was impressed by the event-loop model of Node.js. He thought “we could do something similar but better using the JVM – a much better virtual machine”. He built Vert.x using tools like Netty, Hazelcast and Jackson. Unlike Akka or Finagle, the other frameworks that are built on Netty, he did not want to use Scala, which in his opinion is too difficult to become main-stream like Java.  The existing frameworks in Java are not reactive; Akka though can be used in Java, all the material for it is for Scala; so Java needed a reactive framework. He said “Many modern apps are crunching a lot of data and processing a lot of messages and events, or handling a lot of connections, you just can't do this effectively with thread pools and blocking (OS threads) implementations. Event driven for scalability is a big part of "reactive". Though Vert.x can be embedded in Java code (some users embed in a Spring app), we saw its use as an application platform, an alternative to multi-threaded blocking model frameworks,-JEE or Spring - many of their APIs are blocking(JDBC,JTA and even many parts of JMS). When compared to Node.js, Vertx is more suitable for bigger back-end apps  because of  1) its modular approach –each vertical is a module 2) message-passing through distributed event-bus 3) use of more than one event-loop(thread) like Netty.-Vertx creates event-loops equal to the number of cores in the server 4) can get Java programmers experienced in such type of applications. Even for Real time apps, it is a better choice due to its multi-reactor pattern that automatically makes use of the available resources better.

We can sum up Vert.x as a light-weight, high performance application platform for the JVM based on polyglot programming, asynchronous messaging and reactive programming principles. Vert.x is a very general purpose platform which you can use for many types of applications – including web applications, in particular modern ‘real-time’ web applications, but also traditional ‘enterprise’ back end apps.








}