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 Server.java

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.

http://localhost:8500/?name=yourname



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. 

Polyglot


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. 

Reactive


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


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. 

Linux

  • 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

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

     eg:
                          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

Windows

  • 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'
          where
          path = absolute path to the bin directory of your vert.x extract

           eg:
                          ;C:\Users\User\Vertx\bin

  • For Windows 10 click 'New' and add the path with no semicolon. 
             eg:
                          C:\Users\User\Vertx\bin


Running Vert.x programs


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

eg: 
        vertx run myprogram.java
        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 hackerrank.com or codeforces.com 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. 

   

Optimized quick sort

In the previous article  we discussed about quick sort algorithm. In this article we are going to discuss about further optimizing quick sor...