Sunday, October 22, 2017

The Vertx of it all

What is Vert.X

In a single sentence, Vert.X is an event based application framework running on JVM.

Why Vert.X
1. Support for multiple languages
2. Simple asynchronous model
3. Event Bus
4. No multithreading nightmares
5. No Http overhead

Simple Example ( Java )

// The Server Initialization

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.CookieHandler;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.sstore.LocalSessionStore;
import io.vertx.ext.web.sstore.SessionStore;

public final class VertxExample extends AbstractVerticle {

public static void main(String[] args) throws Exception {
                // Set the server start parameters VertxOptions options = new VertxOptions().setClustered(false);

                // Create instance of vertx with the set options
    Vertx vertx = Vertx.vertx(options);
                // Deploy the verticle
    vertx.deployVerticle(new VertxExample());

public void start() throws Exception {

// Set the vertx router with the serving context
Router router = Router.router(vertx);

                // Acknowledge end points with the handlers for the same"/myEndPoint").handler(new MyEndPointHandler());

                // Start the http server


// The Handler

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import com.softcede.quizwe.controller.UserManager;
import com.softcede.quizwe.util.Constants;
import com.softcede.quizwe.vo.UserVO;
import com.softcede.quizwe.worker.user.Authenticator;

import io.vertx.core.Handler;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;

public final class MyEndPointHandler implements Handler {

public MyEndPointHandler() {

public void handle(RoutingContext routingContext) {

HttpServerResponse response = routingContext.response();
HttpServerRequest request = routingContext.request();

try {

String reqUrl = request.uri();

// Get the request parameters
String requestJsonStr = request.getParam("requestJson");

// Do something with the request
// ...

                        // Send the response
response.putHeader("content-type", "application/json")

} catch ( Exception e ) {

No comments :