Spring Boot – 在部署时启动后台线程的最佳方式

我在Tomcat 8中部署了一个Spring Boot应用程序。当应用程序启动时,我想在Spring Autowires的后台启动一个工作线程,并带有一些依赖关系。 目前我有这个:

@SpringBootApplication @EnableAutoConfiguration @ComponentScan public class MyServer extends SpringBootServletInitializer { public static void main(String[] args) { log.info("Starting application"); ApplicationContext ctx = SpringApplication.run(MyServer.class, args); Thread subscriber = new Thread(ctx.getBean(EventSubscriber.class)); log.info("Starting Subscriber Thread"); subscriber.start(); } 

在我的Docker测试环境中,这很好用 – 但是当我将它部署到Tomcat 8中的Linux(Debian Jessie,Java 8)主机时,我从未看到“Starting Subscriber Thread”消息(并且线程未启动)。

将应用程序部署到非嵌入式应用程序服务器时,不会调用main方法。 启动线程的最简单方法是从beans构造函数中执行此操作。 在上下文关闭时清理线程也是一个好主意,例如:

 @Component class EventSubscriber implements DisposableBean, Runnable { private Thread thread; private volatile boolean someCondition; EventSubscriber(){ this.thread = new Thread(this); this.thread.start(); } @Override public void run(){ while(someCondition){ doStuff(); } } @Override public void destroy(){ someCondition = false; } } 

你可以有一个bean,其中有一个onApplicationEvent ApplicationListener onApplicationEvent ApplicationListener它的onApplicationEvent将被调用,如果它还没有启动那么就开始你的线程。 我想你想要ApplicationReadyEvent。