簡體   English   中英

Vertx沒有綁定路由器

[英]Vertx is not binding Routers

好吧,我覺得自己真的很迷失Vertx結構,因為一切都是lambda表達。 我完全按照教程操作是為了很好地構造我的應用程序,但是不幸的是,它沒有注冊任何路由器,我也不知道為什么。 請找到下面我做了什么

serviceEndPoint與上面的教程相同

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

public interface ServiceEndPoint {
    String mountPoint();

    Router router(Vertx vertx);
}

這是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;
    }
}

最后是服務器垂直

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

一旦我運行該應用程序,請注意,我會收到這些警告

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

順便說一下, Router main = StreamSupport.stream(loader.spliterator(), false)大小為0。

有什么幫助嗎?

首先,不是Vert.x中的所有內容都是lambda表達式。 您發現的教程很奇怪。 如您所見,它使用不是Vert.x類的java.util.ServiceLoader 我也不熟悉其他建議將此類與Vert.x應用程序一起使用的人。
它試圖做的是動態加載您的類。 您可能會錯過的是將正確的文件放在META-INF目錄中,如下所述: https : //docs.oracle.com/javase/tutorial/ext/basics/spi.html#register-service-providers

無論如何,這不是我建議使用VertX的方式。 相反,請選擇出色的常規VertX教程: https : //vertx.io/docs/vertx-web/java/#_handling_requests_and_calling_the_next_handler

創建服務接口com.poc.poc.services.ServiceEndPoint )聲明和具體實現( SubscriptionService )后,應添加服務提供者綁定

根據ServiceLocator文檔,應將綁定插入以FQN接口命名的文件下,即META-INF / services / com.poc.poc.services.ServiceEndPoint下 (整個目錄結構位於project / module 資源目錄下)。

該文件將包含實際的接口實現:

com.poc.poc.services.SubscriptionService

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM