Vert.x 启动应用程序和配置文件的方式

Vert.x way of launching an app and config file

我对 vertx 假设您 deploy/start 您的应用程序的方式仍有一些误解。还有两个问题,但对我来说几乎没有关系。

问题 1: 我有办法让您的应用程序可以从命令行和 IDEA 启动吗?

一方面有一个 Starter class(由 Vert.x 提供)从命令行启动我们的应用程序。它为您提供了 main 方法。但是,如果我从 shell 启动它,我将无法调试它,对吧?

另一方面,要在 IDEA 中启动您的应用程序,您需要手动创建主要方法。另外 some features like configurations file 仅针对命令行方式进行了描述。 IE。 java -jar target/my-first-app-1.0-SNAPSHOT-fat.jar -conf src/main/conf/my-application-conf.json

问题2: 如何以编程方式设置配置文件?

我有一个ServerVerticle,那里有所有问题:

public class ServerVerticle extends AbstractVerticle {

    //QUESTION 1: I need this method for IDEA debugging, but don't need when launch app from command line. How to be??
    public static void main(String[] args) {
        Consumer<Vertx> runner = vertx -> {
            try {
                vertx.deployVerticle("server.ServerVerticle", new DeploymentOptions());
            } catch (Throwable t) {
                t.printStackTrace();
            }
        };
        Vertx vertx = Vertx.vertx(new VertxOptions());
        runner.accept(vertx);
    }

    @Override
    public void start() {
        vertx.deployVerticle(...); //deploy another verticles
        //QUESTION 2: how to pass configuration file programmaticaly in vertx? I can parse json file with java I/O, but if some configs are used accross the whole app?
        vertx.createNetServer()
             .connectHandler(this::handleMessage)
             .listen(8080, "localhost");
    }
   

}

为了从 IntelliJ 运行 您的应用程序,您需要做的就是添加一个 运行 配置,其中:

  • 主要 class: io.vertx.core.Launcher
  • 参数:运行 ServerVerticle -conf /path/to/your/config.json

为了以编程方式设置配置,您需要将 Json 对象传递给您的部署选项,如 javadocs 中所述。