Spring Boot - Best way to start a background threa

2020-02-26 00:41发布

I have a Spring Boot application deployed in Tomcat 8. When the application starts I want to start a worker Thread in the background that Spring Autowires with some dependencies. Currently I have this :

@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();
    }

In my Docker test environment this works just fine - but when I deploy this to my Linux (Debian Jessie, Java 8) host in Tomcat 8 I never see the "Starting Subscriber Thread" message (and the thread is not started).

2条回答
够拽才男人
2楼-- · 2020-02-26 01:27

The main method is not called when deploying the application to a non-embedded application server. The simplest way to start a thread is to do it from the beans constructor. Also a good idea to clean up the thread when the context is closed, for example:

@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;
    }

}
查看更多
够拽才男人
3楼-- · 2020-02-26 01:27

You could have a bean which impelements ApplicationListener<ContextRefreshedEvent> It's onApplicationEvent will be called just start your thread there if it hasn't been started already. I think you want the ApplicationReadyEvent by the way.

Edit How to add a hook to the application context initialization event?

@Component
public class FooBar implements ApplicationListener<ContextRefreshedEvent> {

    Thread t = new Thread();

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (!t.isAlive()) {
            t.start();
        }
    }
}
查看更多
登录 后发表回答