Vertx and Spring Powerful couple for the Bigdata masses

The story of those two start this way:

My requirement was to have an app that will be able to handle thousands of concurrent connections requests for the first phase.

Now as an experienced approach the first things come on mind is:

Let’s bring up couple of Tomcat’s instances backed up by Spring having load-balancer before them and let the party begin.

But we know thats not a free lunch.  The costs, the maintenance, High availability and got damn it we do need lots of instances.. Next Idea.. let’s go the Reactor way.

Today we have NodeJS(and some others) as a very known impl for the reactor pattern. It’s ability to maintain thousands of concurrent connections simultaneously on one node is amazing!!

I love the JVM (athough I had great experience with NodeJS) and I wanted to try Vert.x.

Vertx is the Java’s “answer” to NodeJS backup by real cool features like “Workers” which easily offload time-consuming work from the main Event-looper (if you got no idea what I am talking about please read abit on Reactor pattern).

Ok enough talking.. some Hands-on:

Let’s run Vertx:

Import via gradle the Vertx libs:

compile  'io.vertx:vertx-core:3.0.0'

Run the server easily:

public class VertxHttpServerVerticle extends AbstractVerticle {

    private HttpServer httpServer = null;

    @Override
    public void start() throws Exception {
        httpServer = vertx.createHttpServer();

        httpServer.listen(8080);
    }
}

We got a rocking HTTP server listening on 8080 ready to receive thousands of requests.

Now what? Let’s have our busniess logic inside.. The “hard” part. And here it begins.. Configurations, DAO’S, Data accessors, Dependencies.. Am I going now to event the wheel?

In vertx you have the Eventbus which assisting you to pass messages between Verticles. But if you go there you need to keep all your classes concrete. Even if we go there (Instead of having Dependency Injection) we still got other matters to think of.

My experience involved lots of Spring container apps. I am used to Spring because it stitching all-around for us. From Dependency injection to Configurations and lots of other production ready integration libs.

Wouldn’t be good idea to match those two?

That way we can benefit the power of Vertx by handling million of concurrent connections behind a simple Vert.x Cluster and and in addition avoiding the pain of a boilerplate code inside our lovely app which might include configurations, integrations or any other common stuff.

Let’s bring Spring inside:

Since all we need is spring-starter lib (Having Spring boot packed with us):

Importing:

compile "org.springframework.boot:spring-boot-starter:${ver.spring}"

Now init vertx with Spring:

ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);

//Get property:

int VertxHttpServerPort = Integer.valueOf(context.getEnvironment().getProperty("http.port"));

Now that we hold Spring context we can pass it thru our Verticles(The “beans” of Vertx)

Want to have Spring beans ready to be used in our Verticles? No prob:

Configure new Spring-Bean

@Bean
public MyService myServieImpl() {
    return new MyServiceImpl();
}

All is left to pass the Context into our Verticle and retrieve the spring-bean inside:

vertx.deployVerticle(new VertxHttpServerVerticle(context))

There is abit more but I am sure you getting the idea!

Cheers,

Idan.

Related Articles:

(3636)

Be Sociable, Share!

Leave a Reply