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());
}

@Override
public void start() throws Exception {

// Set the vertx router with the serving context
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());

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

                // Start the http server
vertx.createHttpServer().requestHandler(router::accept).listen(8080);

}
}

// 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() {
}

@Override
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")
                                 .setStatusCode(200)
                                 .end(response);

} catch ( Exception e ) {
e.printStackTrace();
}
}
}



Thursday, May 18, 2017

Loading Postgres data into Spark



When loading data from postgres in Spark 2.11, I got the below exception. 
java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/scala/DefaultScalaModule$

All you need to fix is to add the following dependency.

   com.fasterxml.jackson.module
   jackson-module-scala_2.11
   2.4.5-1

Hope it helps.