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
  • 相关阅读:
    gateone DSM 8271
    手机
    epub- https://www.taoshudang.com/
    [outlook] [vba] Highlight text in body of incoming emails
    HDMI ARC功能详解及应用介绍
    蓝光播放机
    surfingkeys
    亿格瑞A5-hdmi故障了
    解决VS无法连接到已配置的开发web服务器或者部署在IIS上的web服务打不开的问题
    解决远程连接mysql很慢的方法
  • 原文地址:https://www.cnblogs.com/shine_cn/p/6187739.html
Copyright © 2011-2022 走看看