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

时间:2016-10-28 10:31:45

标签: java vert.x

我仍然对vertx假设您将部署/启动应用程序的方式有一些误解。仍然有两个问题,但它们对我来说几乎没有关系。

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

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

另一方面,要在IDEA中隐藏您的应用,您需要手动创建主方法。此外,some features like configurations file仅针对命令行方式进行了描述。即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");
    }


}

1 个答案:

答案 0 :(得分:1)

为了从IntelliJ运行您的应用程序,您需要做的就是在以下位置添加运行配置:

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

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