【发布时间】:2016-09-07 12:30:41
【问题描述】:
我目前正在尝试将 Spring Boot 应用程序部署到外部 Tomcat 实例中,并且遇到了一些关于如何最好地管理某些事物的实例化的问题。
按照目前的结构,我有一些类似
的东西public class MyClass extends SpringBootServletInitializer{
@Bean
public ThreadPool pool(){
return new ThreadPool();
}
@Bean
public BackgroundThread setupInbox() {
BackgroundThread inbox = new BackgroundThread(pool());
inbox.start();
return inbox;
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyClass.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(MyClass.class, args);
}
}
其中 BackgroundThread 是一个线程,它正在侦听 AMQP 类型的消息队列以获取新作业。我知道 Spring 提供了一些 RabbitMQ 方法来做到这一点,但我们没有为此使用 Rabbit,所以它没有帮助。
正在部署的这个 *.war 文件的全部目的是通过消息传递向线路公开一些功能,所以我的问题是在 Spring 的生命周期中实例化、启动然后销毁 BackgroundThread 的最佳方法是什么? XML 配置?
【问题讨论】:
-
你想监听一些 Spring 上下文生命周期事件并在它们上管理你的线程吗?