From 0a4d548b6bf9cfef252400fb6d6612e45cf5b602 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期二, 27 十月 2026 13:21:23 +0800
Subject: [PATCH] fix(scheduler): 修复定时任务调度问题并增强系统稳定性
---
src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskScheduler.java | 141 +++++++++++++++++++++++++++++++++--------------
1 files changed, 99 insertions(+), 42 deletions(-)
diff --git a/src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskScheduler.java b/src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskScheduler.java
index cb63d96..3614a39 100644
--- a/src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskScheduler.java
+++ b/src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskScheduler.java
@@ -1,8 +1,9 @@
package com.ruoyi.inspectiontask.service.impl;
import com.ruoyi.inspectiontask.pojo.TimingTask;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalTime;
@@ -13,10 +14,11 @@
import java.util.stream.Collectors;
@Service
+@Slf4j
+@RequiredArgsConstructor
public class TimingTaskScheduler {
- @Autowired
- private Scheduler scheduler;
+ private final Scheduler scheduler;
/**
* 娣诲姞鏂颁换鍔″埌璋冨害鍣�
@@ -36,19 +38,52 @@
// 鑾峰彇鐜版湁瑙﹀彂鍣ㄥ苟杞崲涓� CronTrigger
Trigger oldTrigger = scheduler.getTrigger(triggerKey);
if (!(oldTrigger instanceof CronTrigger)) {
- throw new SchedulerException("Existing trigger is not a CronTrigger");
+ // 瑙﹀彂鍣ㄤ笉瀛樺湪锛堥娆¢儴缃插悗浠庢湭娉ㄥ唽锛屾垨璋冨害璁板綍琚竻鐞嗭級锛屾敼涓洪噸鏂板垱寤�
+ JobKey jobKey = new JobKey("timingTask_" + task.getId());
+ if (scheduler.checkExists(jobKey)) {
+ scheduler.deleteJob(jobKey);
+ }
+ scheduleTimingTask(task);
+ return;
}
- // 鏋勫缓鏂拌Е鍙戝櫒
- Trigger newTrigger = TriggerBuilder.newTrigger()
- .withIdentity(triggerKey)
- .withDescription(task.getTaskName())
- .withSchedule(CronScheduleBuilder.cronSchedule(convertToCronExpression(task)))
- .startAt(Date.from(task.getNextExecutionTime().atZone(ZoneId.systemDefault()).toInstant()))
- .forJob(oldTrigger.getJobKey())
+ // 3. 鏋勫缓CronTrigger锛岀‘淇濇寔涔呭寲閰嶇疆
+ CronTrigger newTrigger = TriggerBuilder.newTrigger()
+ .withIdentity(triggerKey) // 鍞竴鏍囪瘑锛岀敤浜庢寔涔呭寲瀛樺偍
+ .withDescription(task.getTaskName() + "_TRIGGER") // 瑙﹀彂鍣ㄦ弿杩�
+ .forJob(oldTrigger.getJobKey()) // 鍏宠仈瀵瑰簲鐨凧ob
+ .withSchedule(CronScheduleBuilder
+ .cronSchedule(convertToCronExpression(task))
+ .withMisfireHandlingInstructionDoNothing()
+ )
+ // 4. 璁剧疆寮�濮嬫椂闂达紙鑻ヤ负null鍒欑珛鍗崇敓鏁堬級
+ .startAt(task.getNextExecutionTime() != null
+ ? Date.from(task.getNextExecutionTime().atZone(ZoneId.systemDefault()).toInstant())
+ : new Date())
.build();
+ // 鏋勫缓鏂拌Е鍙戝櫒
+// Trigger newTrigger = TriggerBuilder.newTrigger()
+// .withIdentity(triggerKey)
+// .withDescription(task.getTaskName())
+// .withSchedule(CronScheduleBuilder.cronSchedule(convertToCronExpression(task)))
+// .startAt(Date.from(task.getNextExecutionTime().atZone(ZoneId.systemDefault()).toInstant()))
+// .forJob(oldTrigger.getJobKey())
+// .build();
+
scheduler.rescheduleJob(triggerKey, newTrigger);
+ }
+
+ /**
+ * 纭繚浠诲姟宸插湪璋冨害鍣ㄤ腑锛屾湭娉ㄥ唽鏃惰ˉ娉ㄥ唽锛堝簲鐢ㄥ惎鍔ㄦ椂鎭㈠浣跨敤锛�
+ */
+ public void ensureScheduled(TimingTask task) throws SchedulerException {
+ JobKey jobKey = new JobKey("timingTask_" + task.getId());
+ if (scheduler.checkExists(jobKey)) {
+ return;
+ }
+ scheduleTimingTask(task);
+ log.info("琛ユ敞鍐屽贰妫�瀹氭椂浠诲姟: id={}, name={}", task.getId(), task.getTaskName());
}
/**
@@ -70,42 +105,58 @@
/**
* 鍒犻櫎浠诲姟
*/
- public void unscheduleTimingTask(Long taskId) throws SchedulerException {
- JobKey jobKey = new JobKey("timingTask_" + taskId);
- scheduler.deleteJob(jobKey);
+ public void unscheduleTimingTask(Long taskId){
+ try {
+ JobKey jobKey = new JobKey("timingTask_" + taskId);
+ scheduler.deleteJob(jobKey);
+ }catch (SchedulerException e){
+ throw new RuntimeException(e);
+ }
}
private JobDetail buildJobDetail(TimingTask task) {
- JobDataMap jobDataMap = new JobDataMap();
- jobDataMap.put("taskId", task.getId());
+ // 1. 鏋勫缓鍞竴JobKey锛堝熀浜庝换鍔D锛岀‘淇濋噸鍚悗鑳借瘑鍒級
+ JobKey jobKey = new JobKey("timingTask_" + task.getId());
+ // 2. 灏佽浠诲姟鏁版嵁锛堜粎浣跨敤鍩烘湰绫诲瀷锛岀‘淇濆彲搴忓垪鍖栵級
+ JobDataMap jobDataMap = new JobDataMap();
+ jobDataMap.put("taskId", task.getId()); // 浠诲姟ID锛圠ong锛屽彲搴忓垪鍖栵級
+ jobDataMap.put("taskName", task.getTaskName()); // 浠诲姟鍚嶇О锛圫tring锛屽彲搴忓垪鍖栵級
+ jobDataMap.put("taskType", task.getFrequencyType()); // 浠诲姟绫诲瀷锛圫tring锛�
+ // 鎸夐渶娣诲姞鍏朵粬蹇呰鐨勫熀鏈被鍨嬪弬鏁�
+
+ // 3. 鏋勫缓JobDetail锛岃缃寔涔呭寲鐩稿叧灞炴��
return JobBuilder.newJob(TimingTaskJob.class)
- .withIdentity("timingTask_" + task.getId())
- .withDescription(task.getTaskName())
- .usingJobData(jobDataMap)
- .storeDurably()
+ .withIdentity(jobKey) // 鍞竴鏍囪瘑锛岀敤浜庢寔涔呭寲瀛樺偍
+ .withDescription(task.getTaskName()) // 浠诲姟鎻忚堪锛屽瓨鍏ユ暟鎹簱
+ .usingJobData(jobDataMap) // 缁戝畾浠诲姟鏁版嵁
+ .storeDurably(true) // 鍗充娇娌℃湁瑙﹀彂鍣ㄥ叧鑱斾篃鎸佷箙鍖栦繚瀛�
+ .requestRecovery(true) // 褰撹皟搴﹀櫒宕╂簝鍚庢仮澶嶆椂锛岄噸鏂版墽琛屾湭瀹屾垚鐨勪换鍔�
.build();
}
private Trigger buildJobTrigger(TimingTask task, JobDetail jobDetail) {
+ // 1. 鏋勫缓鍞竴TriggerKey锛堝熀浜庝换鍔D锛�
+ TriggerKey triggerKey = new TriggerKey("trigger_" + task.getId());
+
+ // 2. 鐢熸垚Cron琛ㄨ揪寮忥紙鍘熼�昏緫涓嶅彉锛�
String cronExpression = convertToCronExpression(task);
- TriggerBuilder<CronTrigger> triggerBuilder = TriggerBuilder.newTrigger()
- .withIdentity("trigger_" + task.getId())
- .withDescription(task.getTaskName())
- .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression));
-
- if (jobDetail != null) {
- triggerBuilder.forJob(jobDetail);
- }
-
- if (task.getNextExecutionTime() != null) {
- triggerBuilder.startAt(Date.from(task.getNextExecutionTime().atZone(ZoneId.systemDefault()).toInstant()));
- }
-
- return triggerBuilder.build();
+ // 3. 鏋勫缓CronTrigger锛岀‘淇濇寔涔呭寲閰嶇疆
+ return TriggerBuilder.newTrigger()
+ .withIdentity(triggerKey) // 鍞竴鏍囪瘑锛岀敤浜庢寔涔呭寲瀛樺偍
+ .withDescription(task.getTaskName() + "_TRIGGER") // 瑙﹀彂鍣ㄦ弿杩�
+ .forJob(jobDetail) // 鍏宠仈瀵瑰簲鐨凧ob
+ .withSchedule(CronScheduleBuilder
+ .cronSchedule(cronExpression)
+ .withMisfireHandlingInstructionDoNothing() // 閿欒繃鎵ц鏃剁殑绛栫暐锛堟牴鎹笟鍔¤皟鏁达級
+ )
+ // 4. 璁剧疆寮�濮嬫椂闂达紙鑻ヤ负null鍒欑珛鍗崇敓鏁堬級
+ .startAt(task.getNextExecutionTime() != null
+ ? Date.from(task.getNextExecutionTime().atZone(ZoneId.systemDefault()).toInstant())
+ : new Date())
+ .build();
}
-
private String convertToCronExpression(TimingTask task) {
// 鍙傛暟鏍¢獙
if (task == null || task.getFrequencyType() == null || task.getFrequencyDetail() == null) {
@@ -113,13 +164,19 @@
}
// 浣跨敤switch纭繚鏉′欢浜掓枼
- return switch (task.getFrequencyType().toUpperCase()) { // 缁熶竴杞负澶у啓姣旇緝
- case "DAILY" -> convertDailyToCron(task.getFrequencyDetail());
- case "WEEKLY" -> convertWeeklyToCron(task.getFrequencyDetail());
- case "MONTHLY" -> convertMonthlyToCron(task.getFrequencyDetail());
- case "QUARTERLY" -> convertQuarterlyToCron(task.getFrequencyDetail());
- default -> throw new IllegalArgumentException("涓嶆敮鎸佺殑棰戠巼绫诲瀷: " + task.getFrequencyType());
- };
+ String frequencyType = task.getFrequencyType().toUpperCase(); // 缁熶竴杞负澶у啓姣旇緝
+ switch (frequencyType) {
+ case "DAILY":
+ return convertDailyToCron(task.getFrequencyDetail());
+ case "WEEKLY":
+ return convertWeeklyToCron(task.getFrequencyDetail());
+ case "MONTHLY":
+ return convertMonthlyToCron(task.getFrequencyDetail());
+ case "QUARTERLY":
+ return convertQuarterlyToCron(task.getFrequencyDetail());
+ default:
+ throw new IllegalArgumentException("涓嶆敮鎸佺殑棰戠巼绫诲瀷: " + task.getFrequencyType());
+ }
}
// 姣忔棩浠诲姟杞崲
--
Gitblit v1.9.3