Vertx, compose with Future !

DI DIO Maximilien
Aug 12, 2018 · 3 min read
Image for post
Image for post

At the beginning…

I’m working with Vertx for my actual project in my company. Vertx is more a toolkit than a bloated framework. Vertx provide many little dependancies which help you to build anything you want ! With Vertx, you strictly install what you need. But, the core concept is to make asynchronous programming like Node JS, using an event loop system. But it’s not exactly like Node. Due to Javascript, Node JS execution is Singlethread. (Okay, you can launch several instances of Node to scale your application using process manager like pm2 which provide load balancing system. But a Node instance is Singlethread)

Vertx introduce the concept of verticle. Each statements in a verticle use an event loop. You can define many verticles to interact with the same event loop. But, a verticle can run in another thread and executed concurrently or not from the worker pool.

Thread pool management is the biggest difference between Vert.x and Node.js, except for polyglot. Vert.x creates Run Loops (Event Loops) equaling the number of cores and provides thread pool-related function to handle tasks using server resources requiring long periods for event handling.


What did he mean by “except for polyglot” ? Vertx is available for many programming language, ruby, groovy, scala, ceylon, kotlin and even javascript (Node JS are you scared ?) A verticle can be write in ruby and work with an another verticle write in Java… pretty cool isn’t it ? :)

So, let’s talk about our Future !

Vertx is inspired by Node JS and use callback to make asynchronous functions.(you can use RxJava programming style if you don’t like callbacks) Vertx provide interfaces to handle asynchronous result from callbacks and define callbacks. But, it also provide a smart system to prevent callback hell situations !

The result from callback is an instance of object which implement AsyncResult<T> interface. A future object can handle this type of result. If you have some javascript skills, you can considered Futures like javascript Promises. The main interest is to chain Future calls using compose method from Future<T> interface (like then for a Promise object) if you need to have the result from the first callback to use the second callback.

Considere this fictional exampel. I want to save a specific data in my database before data updating. So i have a class (which extends AbstractVerticle) with an attribute called databaseClient which provide a database connection. (using an object which implements SQLConnection interface) Methods queryWithParams and updateWithParams are provided by SQLCommon library of Vertx.

So, in this example, updateUserData is called after saveDataToHistoryTable execution. We don’t need to call updateUserData in saveDataToHistoryTable function. (and create callback hell situation !)

Pretty simple ? :)

Vertx has a big potential and probably, a great rival for Node JS. I’ll probably write some drafts about this complete toolkit.

See you soon for another asynchronous programming technologies !


stupid dev blog

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store