java - Spring webapp - 在应用程序停止时关闭线程

标签 java spring tomcat

我正在使用 Spring 的 ApplicationListener 接口(interface)实例化一个 ScheduledExecutorService,如下所示:

@Component
public class ExecutorsStart implements ApplicationListener<ContextRefreshedEvent> {

    private ScheduledExecutorService executor;

@Autowired
Scheduler scheduler;

@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {
    executor = Executors.newSingleThreadScheduledExecutor();
    scheduler.init();
    int delay = 10;
    int period = 60;// repeat every 1 minutes.
    executor.scheduleAtFixedRate(scheduler, delay, period, TimeUnit.SECONDS);
}

目前,当我运行 ./shutdown.sh 时,Tomcat 不会干净地关闭,并显示消息:

The web application [/foo] appears to have started a thread named [pool-1-thread-1] but has failed to stop it

这似乎是因为我还没有编写代码来停止 ScheduledExecutorService。

我的问题是:在这种环境下应该如何正确完成?

我注意到存在一个 ContextStoppedEvent ,所以,我为它实现了一个监听器:

@Component
public class ExecutorsStop implements ApplicationListener<ContextStoppedEvent> {

@Autowired
ExecutorsStart executorsStart;

@Override
public void onApplicationEvent(final ContextStoppedEvent event) {
    executorsStart.executor.shutdownNow();
}

但似乎在 Tomcat 关闭时不会调用此事件处理程序。

我是否错误地实现了这一点,还是我完全按照 wong 的方式进行了?

最佳答案

您正在寻找 ContextClosedEvent

@Component
public class ExecutorsStop implements ApplicationListener<ContextClosedEvent> {

    @Autowired
    ExecutorsStart executorsStart;

    @Override
    public void onApplicationEvent(final ContextClosedEvent event) {
        System.out.println("Stopped: " + event);
    }
}

当 Servlet 容器关闭时,它会在其各种 ServletContextListener 上调用 contextDestroyed(..) 并在其 上调用 destroy() >Servlet 实例。 ContextLoaderListenerDispatcherServlet 分别调用 close()在他们的 ApplicationContext 上。

关于java - Spring webapp - 在应用程序停止时关闭线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22650569/

相关文章:

java - switch 结构可以嵌套在 (if-else ) 结构中,反之亦然吗?

java - Spring MVC Controller 继承和路由

java - 使用BlockingQueue在Java中实现生产者-消费者似乎在消费后丢失数据

java - 无法推断基本网址。这在使用动态 servlet 注册或 API 位于 API 网关 swagger 2.10.5 之后时很常见

tomcat - xwiki 占用 100 个 cpu

java - 重命名、删除或更改文件路径的成本

java - 读取 excel 文件时出错

javascript - tomcat中只使用jsp和javascript/ajax/jQuery不使用php如何实现跨域访问?

tomcat - AWS - 在 EC2 上安装 Tomcat 8

java - 使用与JBOSS兼容的ANT创建WAR