我正在使用Spring Boot构建一个命令行Java应用程序,以使其快速运行。
该应用程序加载不同类型的文件(例如CSV)并将其加载到Cassandra数据库中。它不使用任何Web组件,也不是Web应用程序。
我遇到的问题是在工作完成后停止应用程序。我正在将Spring CommandLineRunner接口(interface)与@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();
}
}
更新:问题似乎是
spring-cassandra
,因为项目中没有其他内容。有谁知道为什么它会使线程在后台运行,从而阻止应用程序停止运行?UPDATE :通过更新到最新的spring boot版本,问题消失了。
最佳答案
答案取决于仍然在做什么。您可能可以通过线程转储(例如使用jstack)来查找。但是,如果它是由Spring启动的,则应该可以使用ConfigurableApplicationContext.close()
在main()方法(或CommandLineRunner
)中停止该应用程序。