Basil Battikhi
Basil Battikhi

Reputation: 2668

Vertx is not binding Routers

Well, i felt myself really lost with Vertx structure due to everything is a lambda expression. i followed this tutorial exactly in order to well structure my application, unfortunately it doesn't register any router i have no idea why. pleasefind bellow what i did

serviceEndPoint same with the above tutorial

import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;

public interface ServiceEndPoint {
    String mountPoint();

    Router router(Vertx vertx);
}

and here is the subscriptionService

import com.poc.poc.repositories.SubscriptionRepository;
import com.poc.poc.services.ServiceEndPoint;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;

public class SubscriptionService implements ServiceEndPoint {
    private SubscriptionRepository subscriptionRepository;

    public SubscriptionService() {
        subscriptionRepository = new SubscriptionRepository();

    }

    @Override
    public String mountPoint() {
        return "/test";
    }

    @Override
    public Router router(Vertx vertx) {
        Router router = Router.router(vertx);
        router.get("/test").handler(rc -> rc.response().end(subscriptionRepository.getSubscriptionInfo(rc, vertx).toString()));
        return router;
    }
}

And finally here is the server vertical

import com.poc.poc.services.ServiceEndPoint;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;

import java.util.ServiceLoader;
import java.util.stream.StreamSupport;

import io.vertx.ext.web.Router;

public class ServerVertical extends AbstractVerticle {

    @Override
    public void start(final Future<Void> startFuture) throws Exception {

        ServiceLoader<ServiceEndPoint> loader = ServiceLoader.load(ServiceEndPoint.class);

        Router main = StreamSupport.stream(loader.spliterator(), false)
            .collect(() -> Router.router(vertx), //the main router
                (r, s) -> r.mountSubRouter(s.mountPoint(), s.router(vertx)),
                (r1, r2) -> {
                });

        vertx.createHttpServer().requestHandler(main::accept).listen(8080, res -> {
            if (res.succeeded()) {
                startFuture.complete();
            } else {
                startFuture.fail(res.cause());
            }
        });
    }
}

Please be noted once i run the application, im getting those warnings

Jun 12, 2018 6:16:45 PM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 2486 ms, time limit is 2000
Jun 12, 2018 6:16:46 PM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 3485 ms, time limit is 2000

by the way Router main = StreamSupport.stream(loader.spliterator(), false) size is 0.

any help ?

Upvotes: 1

Views: 433

Answers (2)

tmarwen
tmarwen

Reputation: 16354

Once you have created you service interface (com.poc.poc.services.ServiceEndPoint) declaration and the concrete implementation (SubscriptionService), you should add the service provider binding.

As per the ServiceLocator documentation, the binding should be inserted under a file named after you interface FQN, i.e. under META-INF/services/com.poc.poc.services.ServiceEndPoint (The whole directory structure goes under the project / module resources directory).

The file will contain the actual interface implementation:

com.poc.poc.services.SubscriptionService

Upvotes: 1

Alexey Soshin
Alexey Soshin

Reputation: 17701

First, not everything in Vert.x is a lambda expression. That's just quite a weird tutorial you've found. As you can see, it uses java.util.ServiceLoader which is not a Vert.x class. Nor I'm familiar with anyone else recommending to use this class with Vert.x applications.
What it tries to do is to load your classes dynamically. What you probably miss is putting the correct file in META-INF directory, as described here: https://docs.oracle.com/javase/tutorial/ext/basics/spi.html#register-service-providers

Anyway, that's not the way I would recommend to use VertX. Instead, go with the regular VertX tutorial, which is excellent: https://vertx.io/docs/vertx-web/java/#_handling_requests_and_calling_the_next_handler

Upvotes: 1

Related Questions