Skip to main content

My first day playing around with Vert.x

I have been reading quite a lot about Vert.x recently and finally the day before yesterday I sat down and spent some time exploring it by writing some code. Today I would like to present the outcome.

A little bit of history

Like all Reactive frameworks/tool-kits/libraries, Vert.x is inspired by Node.js.
Back when it was released, Node.js shocked many Java developers who looked down on Javascript, on top of owning the Web client side, JS wanted to conquer also the server side!
And thanks to its non-blocking nature, Node.js performed better than typical Java application servers such as Tomcat or Glassfish.
Then the Java world reacted (pun intended) and today Vert.x is one of the most matured reactive technologies for the JVM.

First Vert.x application 

When trying to build my first super-simple reactive (not really) application using Vert.x, I started following this tutorial from the lead developers of Vert.x.
That's great stuff, but I found it a little overwhelming as the first hands-on experience. I understand that the authors are eager to highlight most of the nice features of the technology but I decided to write my own example focusing just in the very basics.

The example application uses Java 8 and Maven and no custom Vert.x configuration (just the default). The main class creates one main verticle and then that verticle creates two more verticles: 
PublisherVerticle and ConsumerVerticle
The PublisherVerticle simply publishes (broadcasts) five messages using JSON as serialization and the ConsumerVerticle receives the messages and just logs their contents.
The messages are just POJOs.

At this point, you may wonder what a verticle is. Vert.x uses its own terminology, I recommend this post in order to get familiar with it. In simple words, a verticle is a Java class whose life-cycle is managed by a Vert.x instance and it can deploy and start other verticles,  it can also communicate with other verticles using the Event Bus.

This example is useful for people who are new to vert.x since it showcases the following:
- how to start vert.x
- how to deploy and start verticles
- how to serialize/marshal a POJO to be encapsulated inside a vert.x message using JSON
- how to publish a message to the vert.x event bus
- how to receive a message from the vert.x event bus
- how to construct a POJO from the received message
- how to stop vert.x
- the asynchronous behaviour of vert.x

And here you have the code.
Looking at the pom.xml you'll see that just adding the vertx-core dependency is enough, as usually, Maven will download the required transient dependencies for us:

As you can see, Vert.x uses Netty for the network communication (I like that it does not force you to use HTTP only) and Jackson for JSON serialization.

To run the example application you just need to execute its main class class, and looking at the output helps to understand the "everything asynchronous by default" philosophy of Vert.x. 
Even the call to start Vert.x is asynchronous, that is why the main method is the first one to finish:

We can also confirm that Vert.x manages the pool of threads for us so we do not need to worry about it.

Since this is a simplistic example, all verticles run on the same JVM/process, but one of the features of Vert.x is its convenience to implement microservices, thus, I could easily refactor the application in order to place each verticle in a different (Maven) module (and run it on its own runtime environment or in different machines) but that's subject for a future post.

Vert.x is a broad topic and I would like to highlight what this post does not cover and I may explore in the future:
- using a different serialization other than JSON (Google protobuffers for example)
- communication with external services (using HTTP or simply TCP or Websockets) either directly or through the Event Bus.
- using Vert.x as the back-end for a Web app (front end could be Angular or React)
- using Vert.x as the back-end for a native mobile app (probably Android since I already have some experience)
- Vert.x cache capabilities (Shared Map) 
- using different instances of Vert.x in the same application

Thanks for reading!


Popular posts from this blog

Complete (working) code to verify an Android app user phone number through SMS

Update from Thursday September 14th 2017: 
The very same day I posted this (the day before yesterday), I realized that it looked like Google had just made it effectively obsolete

I thought that at least I could claim that I chose a very demanded functionality to blog about, since Google decided to add a new API to provide this very same service.
Even the names are quite similar, I called it "SMS Verifier" and they call it "SMS Retriever".
But after looking into this new Google Services API, I found out that it requires to use a paid third-party service such as Twilio... very disappointing!
So my original post (which follows below) is still relevant after all, since it allows you to verify the user's phone number for free.

Original post: Tuesday September 12th 2017
It was about time for me to give back to the open source community, so I have just pushed the complete (working) code to verify the user's phone number from within an Android app to Github.

When I goo…

Shared Ledger Simulator

I have been interested in the shared/distributed ledger technology (a.k.a. block chain, a.k.a. the magic behind cryptocurrencies) for more than a year already but recently I had finally put real time and effort into it.

And since I believe that the best way to understand something is to get your hands dirty, that is what I have done, after I got a grasp of the core principles (or that is what I thought back then), I decided to code my own shared ledger simulator.

Initially, I also considered to look into the main existing code bases (e.g., the source code of the main/official Bitcoin client/miner or Ethereum's) since they are open source, but seeing code like thisput me off... That file is 3229 lines big!!! Plus it is written in C++.
Do not get me wrong, I truly believe Satoshi Nakamoto (i.e., whoever hides behind that name) is a genius and also a great software developer, but he/she/they for sure did not have readability as their main priority.

I also noticed that some other people h…

You can finally DISLIKE a tweet (and make it count)

(If you don't feel like reading you can watch this video instead)

How many times have you wished there was a "dislike" button in Twitter? (and probably also in any other social network, but I will focus on Twitter in this post).

And I am not referring to this dislike button which is not what you want, since it is just a way to tell Twitter (and no one else) that you would not like to see similar tweets on your feed in the future.

So until Twitter decides to add this highly demanded feature (i.e., a real dislike button), I have come up with a way to dislike a tweet and make it count, so you can share your dislikenesswith everybody and your dislikes can be aggregated with other Twitter users' dislikes.

All you need to do in order to dislike a tweet is to use the hashtag #dislike or just #dlfor short, when replying to the tweet which you dislike. That's it, so simple!

For example, this is the image of a tweet which has two dislikes:

But it would be very annoying if you ha…