我有一个无限循环的过程:
public class MyProcess {
public void start() {
while (true) {
//process
}
}
}
当我开始使用Spring Boot时,我的第一步是在应用程序启动后从上下文中获取bean并手动启动流程:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(MyApplication.class, args);
MyProcess myProcess = applicationContext.getBean(MyProcess.class);
myProcess.start();
}
}
这个方法可以工作,但似乎不是正确的做法,所以我实现了一个CommandLineRunner
:
@Component
public class MyRunner implements CommandLineRunner {
@Autowired
private MyProcess myProcess;
@Override
public void run(String... args) {
this.myProcess.start();
}
}
这几乎与以前相同,但这里的
SpringApplication#run
调用从未真正结束,这似乎也是错误的。我想这样想是正确的吗?然后我尝试使用
Executor
自己管理,但为了捕获任何 Exception
,我必须调用 Future#get
,这导致了相同的阻塞状态。相反,我现在在
CommandLineRunner#run
方法上使用 @Async
注释。这似乎是最好的解决方案,因为任务启动后应用程序完成启动。任何
Exception
都将被捕获并记录,但由Spring创建的支持Executor
会阻止应用程序关闭。我假设有一种方法可以要求Spring终止
Executor
,以便整个应用程序可以退出,因为我正在使用Supervisor外部管理它。这是正确的方法还是我缺少了一步?应用程序是否应该独立并在失败后重新启动进程?
CommandLineRunner
的目的是什么? - DjonCommandLineRunner
接口来创建命令行应用程序。 - ruX