|
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.bosshand.virgo.api.model.JobInfo;
|
|
import com.bosshand.virgo.api.model.JobInfo;
|
|
import com.bosshand.virgo.api.model.Project;
|
|
import com.bosshand.virgo.api.model.Project;
|
|
|
|
+import com.bosshand.virgo.api.model.Reminder;
|
|
import org.quartz.*;
|
|
import org.quartz.*;
|
|
import org.quartz.impl.matchers.GroupMatcher;
|
|
import org.quartz.impl.matchers.GroupMatcher;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
@@ -11,10 +12,9 @@ import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import java.time.Duration;
|
|
|
|
+import java.time.Instant;
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
public class QuartzService {
|
|
public class QuartzService {
|
|
@@ -88,6 +88,24 @@ public class QuartzService {
|
|
deleteCronJob(project.getId() + "_" + project.getName(), DEFAULT_JOB_GROUP, TRIGGER_PRE + project.getId() + "_" + project.getName(), DEFAULT_TRIGGER_GROUP);
|
|
deleteCronJob(project.getId() + "_" + project.getName(), DEFAULT_JOB_GROUP, TRIGGER_PRE + project.getId() + "_" + project.getName(), DEFAULT_TRIGGER_GROUP);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 提醒任务
|
|
|
|
+ */
|
|
|
|
+ public void executeJobReminderQuartz(Reminder reminder){
|
|
|
|
+
|
|
|
|
+ String jobName = reminder.getId() + "_" + reminder.getName();
|
|
|
|
+ String jobClassName = "com.bosshand.virgo.api.job.JobReminderQuartz";
|
|
|
|
+
|
|
|
|
+ // 示例:2023年10月1日12点(UTC时间)
|
|
|
|
+ Instant futureTime = Instant.parse(reminder.getDate());
|
|
|
|
+
|
|
|
|
+ // 计算延迟时间(毫秒)
|
|
|
|
+ long delay = Duration.between(Instant.now(), futureTime).toMillis();
|
|
|
|
+
|
|
|
|
+ // 执行任务
|
|
|
|
+ executeOnce(delay, jobName, jobClassName, JSONObject.toJSONString(reminder));
|
|
|
|
+ }
|
|
|
|
+
|
|
public String addCronJob(String jobName, String cron, String jobClassName, JSONObject data) {
|
|
public String addCronJob(String jobName, String cron, String jobClassName, JSONObject data) {
|
|
try {
|
|
try {
|
|
// 当前任务不存在才进行添加
|
|
// 当前任务不存在才进行添加
|
|
@@ -179,6 +197,29 @@ public class QuartzService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public String executeOnce(long delay, String jobName, String jobClassName, String data) {
|
|
|
|
+ try {
|
|
|
|
+ JobKey jobKey = JobKey.jobKey(jobName, DEFAULT_JOB_GROUP);
|
|
|
|
+ JobDetail job = JobBuilder.newJob(getClass(jobClassName).getClass())
|
|
|
|
+ .withIdentity(jobKey).build();
|
|
|
|
+
|
|
|
|
+ job.getJobDataMap().put("data", data);
|
|
|
|
+
|
|
|
|
+ Trigger trigger = TriggerBuilder.newTrigger()
|
|
|
|
+ .startAt(new Date(System.currentTimeMillis() + delay))
|
|
|
|
+ .withIdentity(TriggerKey.triggerKey(TRIGGER_PRE + jobName, DEFAULT_TRIGGER_GROUP))
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ // 启动调度器
|
|
|
|
+ scheduler.scheduleJob(job, trigger);
|
|
|
|
+ scheduler.start();
|
|
|
|
+ return "SUCCESS";
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("[指定时间点执行一次任务]失败,报错:", e);
|
|
|
|
+ return "FAIL";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
/**
|
|
/**
|
|
* 查询定时任务列表
|
|
* 查询定时任务列表
|