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
  • 相关阅读:
    如果int x=20, y=5,则语句System.out.println(x+y +""+(x+y)+y); 的输出结果是()
    子父类存在同名成员时super的使用条件
    7mysql高级查询
    1udp编程
    6mysql外键
    4mysql数据表增删改查
    5mysql数据类型
    3mysql数据库操作
    2mysql基本使用
    1mysql安装
  • 原文地址:https://www.cnblogs.com/shine_cn/p/6187739.html
Copyright © 2011-2022 走看看