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 {
|
log.info("\n================ 普通任务执行 ================");
|
customthreadPoolExecutor.execute(() -> {
|
|
|
|
});
|
|
TimeUnit.MILLISECONDS.sleep(1);
|
|
log.info("\n================ 带返回值的任务执行 ================");
|
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());
|
}
|
}
|
|
}
|