|
@@ -8,10 +8,8 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
-import java.util.concurrent.Executors;
|
|
|
-import java.util.concurrent.Future;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.*;
|
|
|
|
|
|
/**
|
|
|
* @author ZhangXianyu
|
|
@@ -33,13 +31,11 @@ public class OrderCostMonitorAlarmTask {
|
|
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 任务每10分钟运行一次
|
|
|
- */
|
|
|
- @Scheduled(cron = "0 0/10 * * * ? ")
|
|
|
+
|
|
|
+ @Deprecated
|
|
|
public void run() {
|
|
|
+ log.info("订单与消耗监控告警定时任务开始.");
|
|
|
Future<?> future = taskScheduler.submit(() -> {
|
|
|
- log.info("订单与消耗监控告警定时任务开始.");
|
|
|
//本地不运行
|
|
|
if (!run) {
|
|
|
return;
|
|
@@ -71,4 +67,50 @@ public class OrderCostMonitorAlarmTask {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 任务每10分钟运行一次
|
|
|
+ */
|
|
|
+ @Scheduled(cron = "0 0/10 * * * ? ")
|
|
|
+ public void runTask(){
|
|
|
+ log.info("订单与消耗监控告警定时任务开始.");
|
|
|
+ ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
|
|
+ Runnable task = ()->{
|
|
|
+ try {
|
|
|
+ //0点0分到0点30分不执行
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ if (now.getHour() == 0 && now.getMinute() <= 30) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //监控订单表
|
|
|
+ orderCostMonitorAlarmService.monitorDataStatus();
|
|
|
+ //监控头条广告消耗表
|
|
|
+ orderCostMonitorAlarmService.monitorHeadCostStatus();
|
|
|
+ //监控腾讯广告消耗表
|
|
|
+ orderCostMonitorAlarmService.monitorTencentCostStatus();
|
|
|
+ log.info("订单与消耗监控告警定时任务结束.");
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("定时任务订单与消耗监控告警超时", e);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ scheduler.submit(task);
|
|
|
+ // 使用shutdown()优雅关闭
|
|
|
+ scheduler.shutdown();
|
|
|
+ try {
|
|
|
+ // 等待所有任务完成,最多等待120秒
|
|
|
+ if (!scheduler.awaitTermination(120, TimeUnit.SECONDS)) {
|
|
|
+ log.info("线程池关闭超时,可能有任务未完成");
|
|
|
+ }
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ // 当前线程被中断,通常应重新中断自己
|
|
|
+ log.info("线程池关闭时当前线程被中断");
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 或使用shutdownNow()强制关闭
|
|
|
+ List<Runnable> notStartedTasks = scheduler.shutdownNow();
|
|
|
+ log.info("已取消 " + notStartedTasks.size() + " 个任务");
|
|
|
+ }
|
|
|
+
|
|
|
}
|