navigation

Vertx and Fiber, perfect synergy!

Vertx and Fiber, perfect synergy!

by
June 27, 2017
frontpage, Java
No Comment

Presumably you already have heard for Vert.x  project, a tool-kit for building reactive applications on the JVM. Vert.x is completely asynchronous, lightweight, increadibly fast, event driven and definitely very promising project.  Some of you may have heard about Fibers, otherwise here and here some information can be found. Though the coolest is that in addition to Vertx non blocking and event driven toolkit, we can take advantage of Vertx-Sync as a component through which we can easy escape from well known callback hell while using non blocking APIs. Vertx-Sync allows you to perform asynchronous operations and receive events in a synchronous way, but without blocking kernel threads as a result of that even if  it’s synchronous is negligibly less performant. Vertx-Sync uses Quasar for providing its fibers.

 

In this article I’ll show you how to:

  1. create a simple Vertx project
  2. write simple and concise code via Vertx-Sync  
  3. create simple REST API via Vertx-Web
  4. get some web content using HTTP client  
  5. use MongoDB as an example for using of database from Vertx.

You can find a link for Github here. For the sake of this blog we need :

  1. JDK 1.8
  2. Maven
  3. MongoDB

Install and set up Mongo

Download the latest version of MongoDB  and unzip it to your local directory. You may download it from here. Then, make the following directory  /data/db inside in directory where MongoDB is unzipped.

To start server go to {mongo_directory}/bin and start mongod.

Now it is time to run mongo client. Go to {mongo_directory}/bin and start mongo .

Create testDB database and Entities collection

Create Vertx simple project.

It’s time now to create our Vertx project. Let’s do it! Our start point of the project is:

  • And our Vertical :

  1. Launcher class is just for starting of our program and deploying of our MainVerticle.
  2. It has to be considered that our MainVerticle extends io.vertx.ext.sync.SyncVerticle. If it is not deployed as SyncVerticle instance fibers will not work.
  3. Create HttpServer and router for handling of our REST API ( following the next 3 steps):
    1. GET http://localhost:8089/getWebContent // request to “https://www.google.com” just for testing of async Http client.
    2. GET http://localhost:8089/entities
    3. GET http://localhost:8089/entities/:id  //:id is path parameter for finding of some entity
    4. PUT http://localhost:8089/entities // Create a new entity record. As request body Json as payload should be applied for example : {“name”: “Charlie Harper”}
  4. Create WebClient and MongoClient and use their async APIs synchronously via fibers.
  5. It is very important to use Sync.fiberHandler() when we need to use any handlers. So for usage of fibers in a normal handler, e.g. in the request handler of an Http server we must first convert the normal handler to a fiber handler.
  6. To get response from async APIs that return AsyncResult<T>  as callback is very easy with Sync.awaitResul() . Below is some copied documentation from Vertx about it:

The documentation says the following : Many async operations in Vert.x-land take a Handler<AsyncResult<T>> as the last argument. An example would be executing a find using the Vert.x Mongo client or sending an event bus message and getting a reply.
Vertx-sync allows you to get the result of a one-shot asynchronous operation in a synchronous way.
This is done by using the Sync.awaitResult method.
The method is executed specifying the asynchronous operation that you want to execute in the form of a Consumer, the consumer is passed the handler at run-time. Using Lambda expression is even easier:

7. The most important thing is to instrument our bytecode. Vertx use Quasar and their fiber implementation use bytecode instrumentation. We don’t need to do anything special but just have to start JVM with javaagent as VM option. For example: -javaagent:/path/to/quasar/core/quasar-core.jar. For that case we use “exec-maven-plugin” . That is content of pom.xml:

8. Finally to start our program just type : mvn clean package exec:java
And that is all :-)

Do you want more great blogs like this?

Subscribe for Dreamix Blog now!