xm
2024-06-14 722af26bc6fec32bb289b1df51a9016a4935610f
提交 | 用户 | 时间
722af2 1 package com.dl.framework.manager;
X 2
3 import com.dl.common.utils.Threads;
4 import lombok.extern.slf4j.Slf4j;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.beans.factory.annotation.Qualifier;
7 import org.springframework.stereotype.Component;
8
9 import javax.annotation.PreDestroy;
10 import java.util.concurrent.ScheduledExecutorService;
11
12 /**
13  * 确保应用退出时能关闭后台线程
14  *
15  * @author Lion Li
16  */
17 @Slf4j
18 @Component
19 public class ShutdownManager {
20
21     @Autowired
22     @Qualifier("scheduledExecutorService")
23     private ScheduledExecutorService scheduledExecutorService;
24
25     @PreDestroy
26     public void destroy() {
27         shutdownAsyncManager();
28     }
29
30     /**
31      * 停止异步执行任务
32      */
33     private void shutdownAsyncManager() {
34         try {
35             log.info("====关闭后台任务任务线程池====");
36             Threads.shutdownAndAwaitTermination(scheduledExecutorService);
37         } catch (Exception e) {
38             log.error(e.getMessage(), e);
39         }
40     }
41 }