zoukankan      html  css  js  c++  java
  • Spring webapp

    显示使用线程池Executors,必须执行 pool.shutdown() 否则会存在线程池泄露;

    http://stackoverflow.com/questions/22650569/spring-webapp-shutting-down-threads-on-application-stop

    I am instantiating a ScheduledExecutorService using Spring's ApplicationListener interface as follows:

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

    At the moment, Tomcat won't shut down cleanly when I run, ./shutdown.sh, with message:

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

    and this seems to be because I have not yet written code to stop the ScheduledExecutorService.

    My question is: how should this be done properly in this environment?

    I noticed that there exists a ContextStoppedEvent, so, I implemented a listener for it:

    @Component
    public class ExecutorsStop implements ApplicationListener<ContextStoppedEvent> {
    
    @Autowired
    ExecutorsStart executorsStart;
    
    @Override
    public void onApplicationEvent(final ContextStoppedEvent event) {
        executorsStart.executor.shutdownNow();
    }

    But it seems that this event handler doesn't get called when Tomcat is shutdown.

    Have I implemented this incorrectly, or am I going about this completely the wong way?

    shareimprove this question
     

    You're looking for ContextClosedEvent.

    @Component
    public class ExecutorsStop implements ApplicationListener<ContextClosedEvent> {
    
        @Autowired
        ExecutorsStart executorsStart;
    
        @Override
        public void onApplicationEvent(final ContextClosedEvent event) {
            System.out.println("Stopped: " + event);
        }
    }

    When the Servlet container shuts down, it calls contextDestroyed(..) on its various ServletContextListener and destroy() on its Servlet instances. The ContextLoaderListener and DispatcherServlet each call close() on their ApplicationContext.

    shareimprove this answer
     
    1  
    Is the PreDestroy annotation you mention in stackoverflow.com/a/22544982/55070 not enough for doing so?– leo Aug 27 '14 at 12:13
        
    @lep Sure, preDestroy could work here too. – Sotirios Delimanolis Aug 27 '14 at 14:24
  • 相关阅读:
    vim 使用总结
    C语言中的条件编译
    getopt函数
    Linux 命令使用总结
    vim ctags cscope
    shell 定时处理
    shell 脚本中的标准输出
    ubuntu mail 命令发送邮件
    shell 编程中的特殊字符的处理
    system v 消息队列
  • 原文地址:https://www.cnblogs.com/shine_cn/p/6187739.html
Copyright © 2011-2022 走看看