How can I shutdown Spring task executor/scheduler pools before all other beans in the web app are destroyed?

后端 未结 5 917
眼角桃花
眼角桃花 2020-11-28 18:53

In a Spring web application I have several DAO and service layer beans. One service layer bean has annotated @Async / @Scheduled methods. These methods depend on other (auto

5条回答
  •  天命终不由人
    2020-11-28 19:15

    If it is going to be a web based application, you can also use the ServletContextListener interface.

    public class SLF4JBridgeListener implements ServletContextListener {
    
       @Autowired 
       ThreadPoolTaskExecutor executor;
    
       @Autowired 
       ThreadPoolTaskScheduler scheduler;
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
    
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
             scheduler.shutdown();
             executor.shutdown();     
    
        }
    

    }

提交回复
热议问题