Picocli与长时间运行的Javalin线程

huangapple go评论56阅读模式
英文:

Picocli with long running Javalin Thread

问题

我想构建一个简单的Javalin API,并使用Picocli配置HTTP端口作为命令行参数。它看起来有点像这样:

public class Main implements Runnable {

    @CommandLine.Option(names = {"-p", "--port"})
    int httpPort = 9080;

    public static void main(String... args) {
        int exitCode = new CommandLine(new Main()).execute(args);
        System.exit(exitCode);
    }

    @Override
    public void run() {
        Javalin.create()
            .get("/some/endpoint", new EndpointHandler())
            .start("0.0.0.0", httpPort);
    }
}

问题是,Picocli似乎启动了Javalin线程并立即返回或退出。我应该如何让Picocli等待Javalin,或者让Javalin以不立即退出的方式阻塞,以便我的应用程序不会立即退出?

英文:

I want to build a simple Javalin API and configure the HTTP port with a command line arg with Picocli. It looks a bit like this:

public class Main implements Runnable {

    @CommandLine.Option(names = {"-p", "--port"})
    int httpPort = 9080;

    public static void main(String... args) {
        int exitCode = new CommandLine(new Main()).execute(args);
        System.exit(exitCode);
    }

    @Override
    public void run() {
        Javalin.create()
            .get("/some/endpoint", new EndpointHandler())
            .start("0.0.0.0", httpPort);
    }
}

The problem is, that Picocli seems to start the Javalin thread and returns or exits immediatly. How can I either get Picocli to wait for Javalin or have Javalin block in such a way that my app does not exit instantly?

答案1

得分: 1

我认为我找到了一个解决办法。我只需要在启动Runnable/Callable后不退出应用程序。像这样:

public static void main(String... args) {
    new CommandLine(new Main()).execute(args);
}
英文:

I think I found a solution. I just had to not exit the app after starting the Runnable/Callable. Like so:

public static void main(String... args) {
    new CommandLine(new Main()).execute(args);
}

huangapple
  • 本文由 发表于 2023年6月13日 13:11:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定