xuhy
2025-05-06 4a0aaa7dc9bd20c787acb649f5fb33dcc8f122a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.ruoyi.jianguan.governmentCloud;
 
 
import com.ruoyi.jianguan.mongodb.service.*;
import io.netty.util.concurrent.DefaultThreadFactory;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.*;
 
/**
 * 定时上传政务云数据
 */
@Slf4j
@Component
public class UploadDataTaskUtil {
 
    @Autowired
    private AcquisitionBillingModeService acquisitionBillingModeService;
    @Autowired
    private BillingModeVerifyService billingModeVerifyService;
    @Autowired
    private BmsAbortService bmsAbortService;
    @Autowired
    private BmsDemandAndChargerExportationService bmsDemandAndChargerExportationService;
    @Autowired
    private OnlineService onlineService;
    @Autowired
    private EndChargeService endChargeService;
    @Autowired
    private ErrorMessageMessageService errorMessageMessageService;
    @Autowired
    private UploadRealTimeMonitoringDataService uploadRealTimeMonitoringDataService;
    @Autowired
    private ChargingHandshakeService chargingHandshakeService;
    @Autowired
    private ParameterSettingService parameterSettingService;
    @Autowired
    private MotorAbortService motorAbortService;
    @Autowired
    private BmsInformationService bmsInformationService;
    @Autowired
    private ChargingPileStartsChargingService chargingPileStartsChargingService;
    @Autowired
    private PlatformStartChargingReplyService platformStartChargingReplyService;
    @Autowired
    private PlatformStopChargingReplyService platformStopChargingReplyService;
    @Autowired
    private TransactionRecordService transactionRecordService;
    @Autowired
    private UpdateBalanceReplyService updateBalanceReplyService;
    @Autowired
    private SynchronizeOfflineCardReplyService synchronizeOfflineCardReplyService;
    @Autowired
    private ClearOfflineCardReplyService clearOfflineCardReplyService;
    @Autowired
    private WorkingParameterSettingReplyService workingParameterSettingReplyService;
    @Autowired
    private TimingSettingService timingSettingService;
    @Autowired
    private SetupBillingModelReplyService setupBillingModelReplyService;
    @Autowired
    private GroundLockRealTimeDataService groundLockRealTimeDataService;
    @Autowired
    private ChargingPileReturnsGroundLockDataService chargingPileReturnsGroundLockDataService;
    @Autowired
    private PlatformRestartReplyService platformRestartReplyService;
    @Autowired
    private PlatformRemoteUpdateReplyService platformRemoteUpdateReplyService;
    @Autowired
    private QrCodeDeliveryReplyService qrCodeDeliveryReplyService;
    @Autowired
    private SecurityDetectionService securityDetectionService;
 
    /**
     * 每天的9点执行的任务
     */
    @Scheduled(cron = "0 0 9 * * *")
    public void taskDay(){
        try {
            // 传输mongodb的硬件数据
            createCustomThreadPool();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
 
    /**
     * 创建自定义线程池
     * 特点:
     * 1. 支持定时及周期性任务
     * 2. 核心线程数固定,但可以不断创建新线程执行后续任务
     * 3. 适用于需要定时执行或周期性执行的场景
     */
    @SneakyThrows
    public static void createCustomThreadPool() {
        /*
          创建自定义线程池
          字段:
          1. corePoolSize:核心线程池数量
          2. maximumPoolSize: 最大线程池数量
          3. keepAliveTime: 线程空闲时间
          4. unit: 时间单位
          5. workQueue: 阻塞队列
          5. threadFactory: 线程工厂
          5. handler: 拒绝策略
         */
        ThreadPoolExecutor customthreadPoolExecutor = new ThreadPoolExecutor(
                5,                                              // 根据CPU核心数设置
                10,                                                         // 最大应急线程数
                30, TimeUnit.SECONDS,                                       // 空闲线程存活时间
                new ArrayBlockingQueue<>(100),                      // 有界队列防止内存溢出
                new DefaultThreadFactory("custom-thread-pool"),   // 自定义线程命名
                new ThreadPoolExecutor.CallerRunsPolicy()                   // 拒绝策略
        );
 
        try {
            customthreadPoolExecutor.execute(() -> {
 
 
 
            });
 
            TimeUnit.MILLISECONDS.sleep(1);
 
//            Future<String> future = customthreadPoolExecutor.submit(() -> {
//                log.info("线程:{},办理业务", Thread.currentThread().getName());
//                return "业务办理完成";
//            });
//            log.info(future.get());
        } finally {
            gracefulShutdown(customthreadPoolExecutor);
        }
    }
 
 
    /**
     * 优雅关闭线程池通用方法
     *
     * @param pool 需要关闭的线程池
     */
    private static void gracefulShutdown(ExecutorService pool) {
        pool.shutdown(); // 拒绝新任务提交
        try {
            // 等待现有任务完成
            if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
                pool.shutdownNow(); // 取消等待中的任务 只等待运行中的任务
                // 再次等待任务响应中断
                if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
                    log.error("线程池未完全关闭");
                }
            }
        } catch (InterruptedException e) {
            // 重新尝试关闭
            pool.shutdownNow();
            Thread.currentThread().interrupt();
        } finally {
            log.info("线程池是否执行完成:{}", pool.isTerminated());
        }
    }
 
}