Skip to content

[Doc] QBit and Vertx3 : Best of both worlds for Microservices

Richard Hightower edited this page Dec 3, 2015 · 14 revisions

This feature allows you to create a service which can also serve up web pages and web resources for an app. Prior QBit has been more focused on just being a REST microservices, i.e., routing HTTP calls and WebSocket messages to Java methods. Rather then reinvent the world. QBit now supports Vertx 3. There is a full example at the bottom of this page on how to combine QBit and Vertx.

The QBit support for Vertx 3 exceeds the support for Vertx 2.

QBit allows REST style support via annotations.

Example of QBit REST style support via annotations.

    @RequestMapping(value = "/todo", method = RequestMethod.DELETE)
    public void remove(final Callback<Boolean> callback, 
                       final @RequestParam("id") String id) {

        Todo remove = todoMap.remove(id);
        callback.accept(remove!=null);

    }

Note this is a simple example. QBit can do much more than this. To get a bit of an idea, please check out: QBit microservice tutorials. And be sure to check out QBit Reactive Programming. Also check out the wiki and/or the links at the bottom of this guide. The focus of this page is to discuss how to embed QBit inside of Vertx 3 so you can use both, not to cover all of the bits that make QBit cool in detail.

QBit, microservices lib, also provides integration with Consul, a typed event bus (which can be clustered), and really simplifies complex reactive async callback coordination between services, and a lot more. Please read through the QBit overview.

History: QBit at first only ran inside of Vertx2 . Then we decided to (client driven decision) to make it stand alone and we lost the ability to run it embedded (we did not need it for any project on the road map).

Now you can use QBit features and Vertx 3 features via a mix and match model. You do this by setting up routers and/or a route in Vertx 3 to route to an HttpServer in QBit, and this takes about 1 line of code.

This means we can use Vertx 3's chunking, streaming etc. for complex Http Support. As well as use Vertx 3 as a normal HttpServer, but when we want to use REST style, async callbacks we can use QBit for routing REST calls to Java methods. We can access all of the features of Vertx 3. (QBit was originally a Vertx 2 add-on lib, but then we had clients that wanted to run in standalone and clients who wanted to use it with Servlets / Embedded Jetty. This is more coming back home versus a new thing).

You can run QBit standalone and if you do, it uses Vertx 3 like a network lib, or you can run QBit inside of Vertx 3.

We moved this up for two reasons. We were going to start using Vertx support for DNS to read DNS entries for service discovery in a Heroku like environment. It made no sense to invest a lot of time using Vertx 2 API when we were switching to Vertx 3 in the short time. We also had some services that needed to deliver up an SPA (Single Page App), so we had to extend the support for Vertx anyway or add these features to QBit (which it sort of has but not really its focus so we would rather just delegate that to Vertx 3), and it made no sense to do that with Vertx 2.

Also the Vertx 3 environment is a very vibrant one with many shared philosophies to QBit.

Let's cover where the Vertx3 integration and QBit come in.

We added a new class called a VertxHttpServerBuilder (extends HttpServerBuilder), which allows one to build a QBit HTTP server from a vertx object, a vertx HttpServer and optionally from a Vertx router or a Vertx route.

Note that you can pass QBit HttpServerBuilder or a QBit HttpServer to a QBit EndpointServerBuilder to use that builder instead or HttpServer instead of the default. VertxHttpServerBuilder is a QBit HttpServerBuilder so you construct it, associate it with vertx, and then inject it into EndpointServerBuilder. This is how we integrate with the QBit REST/WebSocket support. If you are using QBit REST with Vertx, that is one integration point.

Also note that you can pass HttpServerBuilder or a HttpServer to a ManagedServiceBuilder to use that builder instead or HttpServer instead of the default. If you wanted to use QBit REST and QBit Swagger support with Vertx then you would want to use ManagedServiceBuilder with this class.

Read Vertx 3 guide on HTTP routing for more details.

Here are some docs taken from our JavaDocs for QBit VertxHttpServerBuilder. VertxHttpServerBuilder also allows one to pass a shared Vertx object if running inside of the Vertx world. It also allows one to pass a shared vertx HttpServer if you want to use more than just QBit routing. If you are using Vertx routing or you want to limit this QBit HttpServer to one route then you can pass a route.

Note: QBits Vertx 2 support is EOL. We will be phasing it out shortly.

Here are some code examples on how to mix and match QBit and Vertx3.

Usage

Creating a QBit HttpServer that is tied to a single vertx route

    HttpServer httpServer = VertxHttpServerBuilder.vertxHttpServerBuilder()
                    .setVertx(vertx).setHttpServer(vertxHttpServer).setRoute(route).build();
    httpServer.start();

Creating a QBit HttpServer server and passing a router so it can register itself as the default route

    Router router = Router.router(vertx); //Vertx router
    Route route1 = router.route("/some/path/").handler(routingContext -> {
    HttpServerResponse response = routingContext.response();
         // enable chunked responses because we will be adding data as
         // we execute over other handlers. This is only required once and
         // only if several handlers do output.
         response.setChunked(true);
         response.write("route1\n");

         // Call the next matching route after a 5 second delay
        routingContext.vertx().setTimer(5000, tid -> routingContext.next());
    });

    //Now install our QBit Server to handle REST calls.
    vertxHttpServerBuilder = VertxHttpServerBuilder.vertxHttpServerBuilder()
                    .setVertx(vertx).setHttpServer(httpServer).setRouter(router);

    HttpServer httpServer = vertxHttpServerBuilder.build();
    httpServer.start();

Note that you can pass HttpServerBuilder or a HttpServer to EndpointServerBuilder to use that builder instead or HttpServer instead of the default. If you are using QBit REST with Vertx, that is one integration point.

EndpointServerBuilder integration

    //Like before
    vertxHttpServerBuilder = VertxHttpServerBuilder.vertxHttpServerBuilder()
                    .setVertx(vertx).setHttpServer(vertxHttpServer).setRouter(router);

    //Now just inject it into the vertxHttpServerBuilder before you call build
    HttpServer httpServer = vertxHttpServerBuilder.build();
    endpointServerBuilder.setHttpServer(httpServer);

Also note that you can pass HttpServerBuilder or a HttpServer to a ManagedServiceBuilder to use that builder instead or HttpServer instead of the default.

If you wanted to use QBit REST and QBit Swagger support with Vertx then you would want to use ManagedServiceBuilder with this class.

ManagedServiceBuilder integration

    //Like before
    vertxHttpServerBuilder = VertxHttpServerBuilder.vertxHttpServerBuilder()
                    .setVertx(vertx).setHttpServer(vertxHttpServer).setRouter(router);

    //Now just inject it into the vertxHttpServerBuilder before you call build
    HttpServer httpServer = vertxHttpServerBuilder.build();
    managedServiceBuilder.setHttpServer(httpServer);

Read Vertx guide on routing for more details Vertx Http Ext Manual.

##Example of combining QBit and Vertx

Let's say we have a service like this:

Sample QBit Service

    @RequestMapping("/hello")
    public static class MyRestService {

        @RequestMapping(value = "/world", method = RequestMethod.POST)
        public String hello(String body) {
            return body;
        }
    }

We want to use a lot of Vertx features, and we decide to embed QBit support inside of a verticle.

Our vertx MyVerticle might look like this:

Vertx Verticle

    public class MyVerticle extends AbstractVerticle {

        private final int port;

        public MyVerticle(int port) {
            this.port = port;
        }

        public void start() {

            try {


                /* Route one call to a vertx handler. */
                final Router router = Router.router(vertx); //Vertx router
                router.route("/svr/rout1/").handler(routingContext -> {
                    HttpServerResponse response = routingContext.response();
                    response.setStatusCode(202);
                    response.end("route1");
                });

                /* Route everything under /hello to QBit http server. */
                final Route qbitRoute = router.route().path("/hello/*");


                /* Vertx HTTP Server. */
                final io.vertx.core.http.HttpServer vertxHttpServer =
                        this.getVertx().createHttpServer();

                /*
                 * Use the VertxHttpServerBuilder which is a special builder for Vertx/Qbit integration.
                 */
                final HttpServer httpServer = VertxHttpServerBuilder.vertxHttpServerBuilder()
                        .setRoute(qbitRoute)
                        .setHttpServer(vertxHttpServer)
                        .setVertx(getVertx())
                        .build();


                /*
                 * Create a new service endpointServer and add MyRestService to it.
                 *  ( You could add a lot more services than just one. )
                 */
                final MyRestService myRestService = new MyRestService();
                final ServiceEndpointServer endpointServer = endpointServerBuilder().setUri("/")
                        .addService(myRestService).setHttpServer(httpServer).build();

                endpointServer.startServer();



                /*
                 * Associate the router as a request handler for the vertxHttpServer.
                 */
                vertxHttpServer.requestHandler(router::accept).listen(port);
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        public void stop() {
        }

    }

Read the comments to see what is going on. It should make sense.

Next we start up the vertx Verticle (perhaps in a main method).

Starting up the Vertx verticle

        myVerticle = new MyVerticle(port);
        vertx = Vertx.vertx();
        vertx.deployVerticle(myVerticle, res -> {
            if (res.succeeded()) {
                System.out.println("Deployment id is: " + res.result());
            } else {
                System.out.println("Deployment failed!");
                res.cause().printStackTrace();
            }
            latch.countDown();
        });

Now do some QBit curl commands :)

        final HttpClient client = HttpClientBuilder.httpClientBuilder()
                     .setHost("localhost").setPort(port).buildAndStart();

        final HttpTextResponse response = client.postJson("/svr/rout1/", "\"hi\"");
        assertEquals(202, response.code());
        assertEquals("route1", response.body());


        final HttpTextResponse response2 = client.postJson("/hello/world", "\"hi\"");
        assertEquals(200, response2.code());
        assertEquals("\"hi\"", response2.body());

The full example is actually one of the integration tests that is part of QBit.

Full example

package io.advantageous.qbit.vertx;

import io.advantageous.qbit.annotation.RequestMapping;
import io.advantageous.qbit.annotation.RequestMethod;
import io.advantageous.qbit.http.client.HttpClient;
import io.advantageous.qbit.http.client.HttpClientBuilder;
import io.advantageous.qbit.http.request.HttpTextResponse;
import io.advantageous.qbit.http.server.HttpServer;
import io.advantageous.qbit.server.ServiceEndpointServer;
import io.advantageous.qbit.util.PortUtils;
import io.advantageous.qbit.vertx.http.VertxHttpServerBuilder;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static io.advantageous.qbit.server.EndpointServerBuilder.endpointServerBuilder;
import static org.junit.Assert.assertEquals;

/**
 * Created by rick on 9/29/15.
 */
public class VertxRESTIntegrationTest {

    private Vertx vertx;
    private TestVerticle testVerticle;
    private int port;

    @RequestMapping("/hello")
    public static class TestRestService {

        @RequestMapping(value = "/world", method = RequestMethod.POST)
        public String hello(String body) {
            return body;
        }
    }

    public static class TestVerticle extends AbstractVerticle {

        private final int port;

        public TestVerticle(int port) {
            this.port = port;
        }

        public void start() {

            try {


                /* Route one call to a vertx handler. */
                final Router router = Router.router(vertx); //Vertx router
                router.route("/svr/rout1/").handler(routingContext -> {
                    HttpServerResponse response = routingContext.response();
                    response.setStatusCode(202);
                    response.end("route1");
                });

                /* Route everything under /hello to QBit http server. */
                final Route qbitRoute = router.route().path("/hello/*");


                /* Vertx HTTP Server. */
                final io.vertx.core.http.HttpServer vertxHttpServer =
                        this.getVertx().createHttpServer();

                /*
                 * Use the VertxHttpServerBuilder which is a special builder for Vertx/Qbit integration.
                 */
                final HttpServer httpServer = VertxHttpServerBuilder.vertxHttpServerBuilder()
                        .setRoute(qbitRoute)
                        .setHttpServer(vertxHttpServer)
                        .setVertx(getVertx())
                        .build();


                /*
                 * Create a new service endpointServer.
                 */
                final ServiceEndpointServer endpointServer = endpointServerBuilder().setUri("/")
                        .addService(new TestRestService()).setHttpServer(httpServer).build();

                endpointServer.startServer();



                /*
                 * Associate the router as a request handler for the vertxHttpServer.
                 */
                vertxHttpServer.requestHandler(router::accept).listen(port);
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        public void stop() {
        }

    }

    @Before
    public void setup() throws Exception{


        final CountDownLatch latch = new CountDownLatch(1);
        port = PortUtils.findOpenPortStartAt(9000);
        testVerticle = new TestVerticle(port);
        vertx = Vertx.vertx(new VertxOptions().setWorkerPoolSize(5));
        vertx.deployVerticle(testVerticle, res -> {
            if (res.succeeded()) {
                System.out.println("Deployment id is: " + res.result());
            } else {
                System.out.println("Deployment failed!");
                res.cause().printStackTrace();
            }
            latch.countDown();
        });


        latch.await(5, TimeUnit.SECONDS);
    }

    @Test
    public void test() {

        final HttpClient client = HttpClientBuilder.httpClientBuilder().setHost("localhost").setPort(port).buildAndStart();
        final HttpTextResponse response = client.postJson("/svr/rout1/", "\"hi\"");
        assertEquals(202, response.code());
        assertEquals("route1", response.body());


        final HttpTextResponse response2 = client.postJson("/hello/world", "\"hi\"");
        assertEquals(200, response2.code());
        assertEquals("\"hi\"", response2.body());

    }


    @After
    public void tearDown() throws Exception {

        final CountDownLatch latch = new CountDownLatch(1);
        vertx.close(res -> {
            if (res.succeeded()) {
                System.out.println("Vertx is closed? " + res.result());
            } else {
                System.out.println("Vertx failed closing");
            }
            latch.countDown();
        });


        latch.await(5, TimeUnit.SECONDS);
        vertx = null;
        testVerticle = null;

    }
}

You can bind direct to the vertxHttpServer, or you can use a router.

Bind qbit to a vertx router


    public static class MyVerticle extends AbstractVerticle {

        private final int port;

        public MyVerticle(int port) {
            this.port = port;
        }

        public void start() {

            try {

                HttpServerOptions options = new HttpServerOptions().setMaxWebsocketFrameSize(1000000);
                options.setPort(port);

                Router router = Router.router(vertx); //Vertx router
                router.route("/svr/rout1/").handler(routingContext -> {
                    HttpServerResponse response = routingContext.response();
                    response.setStatusCode(202);
                    response.end("route1");
                });



                io.vertx.core.http.HttpServer vertxHttpServer =
                        this.getVertx().createHttpServer(options);

                HttpServer httpServer = VertxHttpServerBuilder.vertxHttpServerBuilder()
                        .setRouter(router)//BIND TO THE ROUTER!
                        .setHttpServer(vertxHttpServer)
                        .setVertx(getVertx())
                        .build();
               ...

Bind qbit to a vertx httpServer


    public static class MyVerticle extends AbstractVerticle {

        private final int port;

        public MyVerticle(int port) {
            this.port = port;
        }

        public void start() {

            try {


                HttpServerOptions options = new HttpServerOptions().setMaxWebsocketFrameSize(1000000);
                options.setPort(port);


                io.vertx.core.http.HttpServer vertxHttpServer =
                        this.getVertx().createHttpServer(options);

                HttpServer httpServer = VertxHttpServerBuilder.vertxHttpServerBuilder()
                        .setVertx(getVertx()) 
                        .setHttpServer(vertxHttpServer) //BIND TO VERTX HTTP SERVER DIRECT
                        .build();

           ...

##Where do we go from here

QBit has a health system, and a microservices stats collections system. Vertx 3 provided similar support. QBit has an event bus. Vertx has an event bus. There is no reason why QBit can't provide Vertx implementations of its event bus (this is how the QBit event bus started), or for that matter integrate with Vertx's health system or its stats collection system. QBit has its own service discovery system with implementations that talk to DNS, Consul, or just monitor JSON files to be updated (for Chef Push, or Consul, etcd pull model). There is no reason QBit could not provide an implementation of its Service Discovery that worked with Vertx's clustering support. All of the major internal services that QBit provides are extensible with plugins via interfaces. There is plenty of opportunity for more integration of QBit and Vertx.

QBit and Vertx have both evolved to provide more and more support for microservices and there is a lot of synergy between the two libs.

QBit can also play well with Servlets, Spring MVC, Spring Boot, and other lightweight HTTP libs. QBit comes batteries included.

ManagedServiceBuilder vs. EndpointServerBuilder

ManagedServiceBuilder is in QBit admin. EndpointServerBuilder is in QBit core. ManagedServiceBuilder provides integration with statsD, consul, swagger. ManagedServiceBuilder is the glue to work well in Heroku-like environments, Swagger support, StatsD support, local stats support, health end point support, health system support, admin endpoint support, etc. EndpointServerBuilder builds a single endpoint. ManagedServiceBuilder builds a standard microservice app with health checks, metrics, and more to provide a batteries-included microservices architecture. There is some overlap with Vertx. But the plan is to build bridges from QBit health system over to Vertx health system, and from QBit metrics, stats, KPI over to Vertx stats system. (You just have to implement an interface and delegate some method calls to vertx for both QBit health system and QBit stats system).

If you want to use QBit without statsD, consul, health checks, admin, managed shutdown and swagger support, then you just use EndpointServerBuilder. If you want statsD, consul, health checks, admin, or swagger then you use QBit Spring support or the ManagedServiceBuilder. The Spring support is for another document page. ManagedServiceBuilder allows you to inject a custom health system, a custom service discovery, a custom stats system. It is an nice integration point to delegate to Vertx services.

Let's cover this in example that is like the REST example above but uses ManagedServiceBuilder instead of EndpointServerBuilder.

You have a service like before

    @RequestMapping("/hello")
    public class MyRestService {

        @RequestMapping(value = "/world", method = RequestMethod.POST)
        public String hello(String body) {
            return body;
        }
    }

Note this is a simple example. QBit can do much more than this. To get a bit of an idea, please check out: QBit microservice tutorials. And be sure to check out QBit Reactive Programming.

The verticle now uses ManagedServiceBuilder instead of EndpointServerBuilder direct

    public class MyVerticle extends AbstractVerticle {

        private final int port;

        /** The systemManager can cleanly shut down anything started by the 
         * QBit ManagedServiceBuilder.
         */
        private  QBitSystemManager systemManager;

        public MyVerticle(int port) {
            this.port = port;
        }

        public void start() {

            try {


                /* Route one call to a vertx handler. */
                final Router router = Router.router(vertx); //Vertx router
                router.route("/svr/rout1/").handler(routingContext -> {
                    HttpServerResponse response = routingContext.response();
                    response.setStatusCode(202);
                    response.end("route1");
                });

                /* Route everything under /hello to QBit http server. */
                final Route qbitRoute = router.route().path("/hello/*");


                /* Vertx HTTP Server. */
                final io.vertx.core.http.HttpServer vertxHttpServer =
                        this.getVertx().createHttpServer();

                /*
                 * Use the VertxHttpServerBuilder which is a special builder for Vertx/Qbit integration.
                 */
                final HttpServer httpServer = VertxHttpServerBuilder.vertxHttpServerBuilder()
                        .setRoute(qbitRoute)
                        .setHttpServer(vertxHttpServer)
                        .setVertx(getVertx())
                        .build();


                /** Use a managed service builder. */
                final ManagedServiceBuilder managedServiceBuilder = 
                                ManagedServiceBuilder.managedServiceBuilder();

                systemManager = managedServiceBuilder.getSystemManager();

                /*
                 * Create a new service endpointServer.
                 */
                final ServiceEndpointServer endpointServer = managedServiceBuilder
                        .getEndpointServerBuilder().setUri("/")
                        .addService(new MyRestService())
                        .setHttpServer(httpServer).build();



                endpointServer.startServer();



                /*
                 * Associate the router as a request handler for the vertxHttpServer.
                 */
                vertxHttpServer.requestHandler(router::accept).listen(port);
            }catch (Exception ex) {
                ex.printStackTrace();
                throw new IllegalStateException(ex);
            }
        }

        public void stop() {

            if (systemManager!=null) {
                systemManager.shutDown();
            }
        }

    }

The important bits to see are that we are now using the ManagedServiceBuilder in the verticle start method

ManagedServiceBuilder

                /** Use a managed service builder. */
                final ManagedServiceBuilder managedServiceBuilder = 
                                ManagedServiceBuilder.managedServiceBuilder();

                systemManager = managedServiceBuilder.getSystemManager();

And that we are now using the EndpointServerBuilder that is managed by the ManagedServiceBuilder (managedServiceBuilder.getEndpointServerBuilder).

ManagedServiceBuilder.getEndpointServerBuilder

                /*
                 * Create a new service endpointServer.
                 */
                final ServiceEndpointServer endpointServer = managedServiceBuilder
                        .getEndpointServerBuilder().setUri("/")
                        .addService(new MyRestService())
                        .setHttpServer(httpServer).build();

Note that the QBit systemManager ensures that all services that QBit started will get properly shutdown.

Proper shutdown

        public void stop() {

            if (systemManager!=null) {
                systemManager.shutDown();
            }
        }

This example is one of the unit tests for the admin package.

Complete example showing how to use ManagedServiceBuilder with Vertx to build microservices

package io.advantageous.qbit.vertx;

import io.advantageous.qbit.admin.ManagedServiceBuilder;
import io.advantageous.qbit.annotation.RequestMapping;
import io.advantageous.qbit.annotation.RequestMethod;
import io.advantageous.qbit.http.client.HttpClient;
import io.advantageous.qbit.http.client.HttpClientBuilder;
import io.advantageous.qbit.http.request.HttpTextResponse;
import io.advantageous.qbit.http.server.HttpServer;
import io.advantageous.qbit.server.ServiceEndpointServer;
import io.advantageous.qbit.system.QBitSystemManager;
import io.advantageous.qbit.util.PortUtils;
import io.advantageous.qbit.vertx.http.VertxHttpServerBuilder;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;

public class VertxManagedServiceBuilderIntegrationTest {

    private Vertx vertx;
    private TestVerticle testVerticle;
    private int port;

    @RequestMapping("/hello")
    public static class TestRestService {

        @RequestMapping(value = "/world", method = RequestMethod.POST)
        public String hello(String body) {
            return body;
        }
    }

    public static class TestVerticle extends AbstractVerticle {

        private final int port;

        private  QBitSystemManager systemManager;

        public TestVerticle(int port) {
            this.port = port;
        }

        public void start() {

            try {


                /* Route one call to a vertx handler. */
                final Router router = Router.router(vertx); //Vertx router
                router.route("/svr/rout1/").handler(routingContext -> {
                    HttpServerResponse response = routingContext.response();
                    response.setStatusCode(202);
                    response.end("route1");
                });

                /* Route everything under /hello to QBit http server. */
                final Route qbitRoute = router.route().path("/hello/*");


                /* Vertx HTTP Server. */
                final io.vertx.core.http.HttpServer vertxHttpServer =
                        this.getVertx().createHttpServer();

                /*
                 * Use the VertxHttpServerBuilder which is a special builder for Vertx/Qbit integration.
                 */
                final HttpServer httpServer = VertxHttpServerBuilder.vertxHttpServerBuilder()
                        .setRoute(qbitRoute)
                        .setHttpServer(vertxHttpServer)
                        .setVertx(getVertx())
                        .build();


                /** Use a managed service builder. */
                final ManagedServiceBuilder managedServiceBuilder = ManagedServiceBuilder.managedServiceBuilder();

                systemManager = managedServiceBuilder.getSystemManager();

                /*
                 * Create a new service endpointServer.
                 */
                final ServiceEndpointServer endpointServer = managedServiceBuilder
                        .getEndpointServerBuilder().setUri("/")
                        .addService(new TestRestService())
                        .setHttpServer(httpServer).build();



                endpointServer.startServer();



                /*
                 * Associate the router as a request handler for the vertxHttpServer.
                 */
                vertxHttpServer.requestHandler(router::accept).listen(port);
            }catch (Exception ex) {
                ex.printStackTrace();
                throw new IllegalStateException(ex);
            }
        }

        public void stop() {

            if (systemManager!=null) {
                systemManager.shutDown();
            }
        }

    }

    @Before
    public void setup() throws Exception{


        final CountDownLatch latch = new CountDownLatch(1);
        port = PortUtils.findOpenPortStartAt(9000);
        testVerticle = new TestVerticle(port);
        vertx = Vertx.vertx(new VertxOptions().setWorkerPoolSize(5));
        vertx.deployVerticle(testVerticle, res -> {
            if (res.succeeded()) {
                System.out.println("Deployment id is: " + res.result());
            } else {
                System.out.println("Deployment failed!");
                res.cause().printStackTrace();
            }
            latch.countDown();
        });


        latch.await(5, TimeUnit.SECONDS);
    }

    @Test
    public void test() {

        final HttpClient client = HttpClientBuilder.httpClientBuilder().setHost("localhost").setPort(port).buildAndStart();
        final HttpTextResponse response = client.postJson("/svr/rout1/", "\"hi\"");
        assertEquals(202, response.code());
        assertEquals("route1", response.body());


        final HttpTextResponse response2 = client.postJson("/hello/world", "\"hi\"");
        assertEquals(200, response2.code());
        assertEquals("\"hi\"", response2.body());

    }


    @After
    public void tearDown() throws Exception {

        final CountDownLatch latch = new CountDownLatch(1);
        vertx.close(res -> {
            if (res.succeeded()) {
                System.out.println("Vertx is closed? " + res.result());
            } else {
                System.out.println("Vertx failed closing");
            }
            latch.countDown();
        });


        latch.await(5, TimeUnit.SECONDS);
        vertx = null;
        testVerticle = null;

    }
}

The best example of this is here:

https://github.com/MammatusTech/qbit-microservices-examples/blob/master/todo/src/main/java/com/mammatustech/todo/VertxTodoServiceVerticle.java

TODO explain the above and/or create a vertx / qbit tutorial at https://github.com/MammatusTech/qbit-microservices-examples/.

Serving up static resources (*.js, *.html, *.css) from Vertx.

// Use a Verticle from Vertx land.
// The Verticle is configured with StaticHandler to handle file serving requests. 
// For REST calls, we use QBIT.


// final Vertx vertx = Vertx.vertx();
// vertx.deployVerticle( Instance of Class<? extends AbstractVerticle>)


// In the Start method of the class inherited by AbstractVerticle

/* Vertx HTTP Server. */
		final io.vertx.core.http.HttpServer vertxHttpServer =
				this.getVertx().createHttpServer();

		/* Route  call to a vertx handler’s static 
                   handler for examples : 
                     http://www.programcreek.com/java-api-examples/index.php?api=io.vertx.ext.web.handler.StaticHandler 
                */
		final Router router = createStaticResourcesRouter();

		/* Route everything under /v1 to QBit http server. */
		final Route qbitRoute = router.route().path("/v1/*");

		/*
		 * Use the VertxHttpServerBuilder which is a special builder for Vertx/Qbit integration.
		 */
		VertxHttpServerBuilder vertxHttpServerBuilder =  VertxHttpServerBuilder.vertxHttpServerBuilder();
		...
                //Configure your QBit REST services	
		/*
		 * Create and start new service endpointServer.
		 */
		managedServiceBuilder.getEndpointServerBuilder()
		.setHttpServer(httpServer)
		.build()
		.startServer();


		/*
		 * Associate the router as a request handler for the vertxHttpServer.
		 */
		vertxHttpServer.requestHandler(router::accept).listen(
				managedServiceBuilder.getPort());

	}

Tutorials

__

Docs

Getting Started

Basics

Concepts

REST

Callbacks and Reactor

Event Bus

Advanced

Integration

QBit case studies

QBit 2 Roadmap

-- Related Projects

Kafka training, Kafka consulting, Cassandra training, Cassandra consulting, Spark training, Spark consulting

Clone this wiki locally
You can’t perform that action at this time.