英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论