Java 如何关闭 Spring Boot 命令行应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26329300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to shut down a Spring Boot command-line application
提问by ESala
I am building a Command Line java application using Spring Boot to get it working quickly.
我正在使用 Spring Boot 构建命令行 java 应用程序以使其快速运行。
The application loads different types of files (for example CSV) and loads them into a Cassandra Database. It does NOT use any web components, it is not a web application.
该应用程序加载不同类型的文件(例如 CSV)并将它们加载到 Cassandra 数据库中。它不使用任何 Web 组件,也不是 Web 应用程序。
The problem I am having is to stop the application when the work is done. I am using the Spring CommandLineRunner interface with a @Component
to run the tasks, as shown below, but when the work is completed the application does not stop, it keeps running for some reason and I can't find a way to stop it.
我遇到的问题是在工作完成后停止应用程序。我正在使用带有 a 的 Spring CommandLineRunner 接口@Component
来运行任务,如下所示,但是当工作完成时,应用程序并没有停止,它由于某种原因一直在运行,我找不到停止它的方法。
@Component
public class OneTimeRunner implements CommandLineRunner {
@Autowired
private CassandraOperations cassandra;
@Autowired
private ConfigurableApplicationContext context;
@Override
public void run(String... args) throws Exception {
// do some work here and then quit
context.close();
}
}
UPDATE: the problem seems to be spring-cassandra
, since there is nothing else in the project. Does anyone know why it keeps threads running in the background that prevent the application from stopping?
更新:问题似乎是spring-cassandra
,因为项目中没有其他内容。有谁知道为什么它让线程在后台运行以防止应用程序停止?
UPDATE: the problem disappeared by updating to the latest spring boot version.
更新:通过更新到最新的 Spring Boot 版本,问题消失了。
采纳答案by Dave Syer
The answer depends on what it is that is still doing work. You can probably find out with a thread dump (eg using jstack). But if it is anything that was started by Spring you should be able to use ConfigurableApplicationContext.close()
to stop the app in your main() method (or in the CommandLineRunner
).
答案取决于仍在起作用的是什么。您可能可以通过线程转储(例如使用 jstack)找到答案。但是,如果它是由 Spring 启动的任何内容,您应该能够使用ConfigurableApplicationContext.close()
它在您的 main() 方法(或 )中停止应用程序CommandLineRunner
。
回答by ACV
I found a solution. You can use this:
我找到了解决方案。你可以使用这个:
public static void main(String[] args) {
SpringApplication.run(RsscollectorApplication.class, args).close();
System.out.println("done");
}
Just use .close on run.
只需在运行时使用 .close 即可。
回答by Quan Vo
I also met this issue in my current project (spring boot application). My solution is:
我在当前项目(spring boot 应用程序)中也遇到了这个问题。我的解决办法是:
// releasing all resources
((ConfigurableApplicationContext) ctx).close();
// Close application
System.exit(0);
context.close()
don't stop our console application, it is just releasing the resources.
context.close()
不要停止我们的控制台应用程序,它只是释放资源。
回答by EliuX
Use org.springframework.boot.SpringApplication#exit
. E.g.
使用org.springframework.boot.SpringApplication#exit
. 例如
@Component
public class OneTimeRunner implements CommandLineRunner {
@Autowired
private ConfigurableApplicationContext context;
@Override
public void run(String... args) throws Exception {
SpringApplication.exit(context);
}
}
回答by Abel ANEIROS
This is a combination of @EliuXanswer with @Quan Voone. Thank you both!
The main different is I pass the SpringApplication.exit(context) response code as a parameter to the System.exit() so if there is an error closing the Spring context you will notice.
主要的不同是我将 SpringApplication.exit(context) 响应代码作为参数传递给 System.exit(),因此如果关闭 Spring 上下文时出现错误,您会注意到。
The SpringApplication.exit() will close the Spring context.
SpringApplication.exit() 将关闭 Spring 上下文。
The System.exit() will close the application.
System.exit() 将关闭应用程序。
@Component
public class OneTimeRunner implements CommandLineRunner {
@Autowired
private ConfigurableApplicationContext context;
@Override
public void run(String... args) throws Exception {
System.exit(SpringApplication.exit(context));
}
}