Create your first Vertx application in Java and a basic web server

In the last post, I wrote about what Vertx is and how it can speed up development of API Backends and much more.

This post deals with creating of a basic Maven Project using Vertx, with a simple REST API Backend.

We use Maven to install required dependencies for a project and build it. ( More at https://maven.apache.org/ )

Requirements:

After installation of JDK (setting up Environment variables and checking from Command Line that java command works), Install eclipse and open it.

Here is the complete video:

Steps to create a Maven Project:

  1. Create New Project by clicking on File -> New -> Project
  2. Once project is created, create a New Class named ‘VertxIntro’ by clicking File -> New -> Class
  3. Copy the following code and replace VertxIntro Class.

    Code Explanation:

    * We derieve our class from AbstractVerticle, this tells the application that this is a Vertx file and to run the start method first when the application is started
    * public void start is the first function that gets called when you run the Vertx Project.
    * vertx is the object which you can use to create Server, Client, router, etc.
    * createHttpServer – creates a HttpServer
    * requestHandler – what happens when server gets a request (any URL – we will change this in next post)
    * .listen on vertx object – this will bind the server to listen on the port specified, here 8080
    * the code in listen block – a result object is passed in the handler {} – if the server creation is a success, fut.complete() tells the application that everything is completed without issues.
    If it fails, we tell the application that it failed using fut.fail().

    Future Class: – Since Vertx programming in asyncronous, to keep track of results or anything, we can pass a Future object and call methods on that object. We will see in next post how we will use Future class in detail

  4. Edit pom.xml to add dependencies of vertx libraries and maven dependencies. Paste the following code just before the last line </project> tag. And save the file using Ctrl + S. This will build the project and download the dependencies / modules which are defined in pom.xml

  5. Before you can run the project, you need to set Run Configuration. Click on Run -> Run Configurations, click on ‘New ‘ Button just below “Run a Java Application” and select Project and Main Class like this:
  6. Go to arguments tab and add “run art.VertxIntro” ( yourPackageName.ClassName), Click Apply and Run
  7.  To run next time, right click on Project and select Run as -> Java Application

    In the next post,

    1. I will write how to create and handle different url structures. Like localhost:8080/register , localhost:8080/login etc.
    2. We will also learn how to add separate methods and call them when particular URL is called.
    3. We will also have a look a Future class and see its importance in this asynchronous architecture of Vertx.

Introduction to Vertx – a new way to develop applications in many languages

By definition Vertx ( http://vertx.io/ ) is is a tool-kit for building reactive applications on the JVM by Eclipse.

Just to give you an idea, check this link: Benchmarks and see where Vertx stands when compared to its competitors.

This is a new way to code in Java (and many languages). Vertx simplifies any application development so much. It offers scalability, asynchronous methods & calls (no more hassle of creating threads and stuff!).

From the website (copy pasted as it is already best described!):

  • Eclipse Vert.x is event driven and non blocking. This means your app can handle a lot of concurrency using a small number of kernel threads. Vert.x lets your app scale with minimal hardware
  • You can use Vert.x with multiple languages including Java, JavaScript, Groovy, Ruby, and Ceylon.

 

  • Vert.x is incredibly flexible – whether it’s simple network utilities, sophisticated modern web applications, HTTP/REST microservices, high volume event processing or a full blown back-end message-bus application, Vert.x is a great fit

 

  • Vert.x is lightweight – Vert.x core is around 650kB in size.
  • Vert.x is fast. Here are some independent numbers.
  • Vert.x is not an application server. There’s no monolithic Vert.x instance into which you deploy applications. You just run your apps wherever you want to.
  • Vert.x is modular – when you need more bits just add the bits you need and nothing more.
  • Vert.x is simple but not simplistic. Vert.x allows you to create powerful apps, simply.
  • Vert.x is an ideal choice for creating light-weight, high-performance, microservices.
  • Vert.x offers event bus – using this you can very easily create messaging backend as well.

This is an amazing toolkit. You can code a simple (can develop full scale app too) REST API service and host it without a web server in few minutes. Its that simple.

Go through the documentation ( http://vertx.io/ ) and introductory posts here ( Vertx Tutorials ) to get a very basic idea and in the next post, i will write how to create your first Vertx application using Maven.