Saturday, July 16, 2016

Vert.x example - Http Server

Vert.x is a toolkit for building reactive applications on the JVM. 
For more information about vert.x please read the previous article from here

Following is a simple http Server implemented using vert.x to understand the nature of vert.x applications.

The code is self explanatory. 

To run the above http server, you need vert.x installed and setup properly. 

To start the server, open a terminal or a command promt and run the following command. 

vertx run

After you run the above command you will get a message saying 'Succeeded in deploying verticle'. 

If you get any errors, please check your vert.x installation. 

Now your server is running and waiting for requests to come.  

Note that the above server is implemented to handle GET requests only. It listens to the port 8500.

As line 28 needs a parameter called "name", our GET request should include a name parameter. 

Now open your browser, paste the below url and hit enter. For 'yourname', you can give anything.


Now you've used successfully used vert.x to build a server. 

This server can handle multiple clients at the same time although in the code we never do anything about it. And we don't have to worry about synchronization or deadlocks. 
This is the speciality of vert.x. 

This same server can be built using javaScript, ruby Groovy or Python to run on JVM. 
More examples can be found in the official github repository of vert.x.

Getting Started with Vert.x

What is Vert.x ?

Vert.x is a toolkit for writing scalable, polyglot, reactive applications which run on the JVM. Vert.x is similar to Node.js as both provide an asynchronous API and as it is event-driven. 


Vert.x is written in Java, but we can use it with many languages like Java, Groovy, Ruby, Python, and even JavaScript. You can even mix and match languages. 


Vert.x is asynchronous. It can be used to create highly concurrent applications. Vert.x allows to create multiple threads based on the number of CPU cores while only one process is executed. It handles the multi-threading so users can focus on implementing business logic. Developer won't have to worry about synchronization, deadlocks or data races.


Verticles are building blocks of vert.x application. They are units which operate independently. It can simply said that running a vert.x application means running one or more verticles. Verticles communicate with each other using event bus. Vert.x has some command line tool which enable the developer to simply run the program without compiling it. 

How to install vert.x ?

Vert.x can be installed linux, Windows or OSX. Before installing, you need to have JDK 1.7.0 or later installed and JAVA_HOME added to the PATH variable. 


  • Download the latest version of vert.x from here.
  • Extract it to a place where it will stay permanent. (Extracted directory shouldn't be moved since path will be set accordingly).
  • Add vert.x/bin to PATH variable. Open a terminal and run the following command 
                          export PATH=$PATH:/path

          path = absolute path to the bin directory of your vert.x extract

                          export PATH=$PATH:/home/user/Vertx/bin

      If you are having trouble changing the PATH variable, see this.

  • Verify your installation by running the command 
                         vertx version


  • Download the latest version of vert.x from here.
  • Extract it to a place where it will stay permanent. (Extracted directory shouldn't be moved since path will be set accordingly).
  • Search for 'View Advanced System Settings' and select 'Environment Variables'
  • From 'System Variables' section, highlight 'Path' and click on 'Edit'
  • For windows 7 and 8 
          append ;path to the existing 'Variable Value'
          path = absolute path to the bin directory of your vert.x extract


  • For Windows 10 click 'New' and add the path with no semicolon. 

Running Vert.x programs

Vert.x programs can be simply run using vert.x command line tools. 

        vertx run
        vertx run myprogram.js

Java - Standard input and output in competitive programming

Java provides several classes for taking standard input and printing to standard output.

For input
  • Scanner class
  • BufferedReader and InputStreamReader classes
  • DataInputStream class
  • Console class
For output
  • PrintStream class (System.out.print)
  • BufferedWriter and OutputStreamWriter classes
  • PrintWriter class
We can use any of the above classes to interact with std in/out. But the performance of the above classes differ with the way they are implemented.

Sometimes the time taken to get user input or the time taken to print back to the user becomes important. One such occasion is competitive programming.

Competitive programming websites like or sets timouts to run the solutions. These timeouts are generally set to check whether the competitor has solved the problem using an efficient algorithm.

But sometimes although the competitor has used an efficient algorithm, timeouts may occur. This is due to inefficiency in taking input or printing the output. Below is an implementation of a java class which can be easily used take input and print output efficiently.