dataease-dm/backend/src/main/java/io/dataease/job/sechedule/ScheduleManager.java

399 lines
13 KiB
Java
Raw Normal View History

2021-02-20 10:07:25 +08:00
package io.dataease.job.sechedule;
import io.dataease.commons.utils.LogUtil;
import org.quartz.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
2021-02-20 10:07:25 +08:00
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class ScheduleManager {
@Resource
private Scheduler scheduler;
/**
* 添加 simpleJob
*
* @param jobKey
* @param triggerKey
* @param cls
* @param repeatIntervalTime
* @param jobDataMap
* @throws SchedulerException
*/
public void addSimpleJob(JobKey jobKey, TriggerKey triggerKey, Class<? extends Job> cls, int repeatIntervalTime,
JobDataMap jobDataMap) throws SchedulerException {
JobBuilder jobBuilder = JobBuilder.newJob(cls).withIdentity(jobKey);
if (jobDataMap != null) {
jobBuilder.usingJobData(jobDataMap);
}
JobDetail jd = jobBuilder.build();
SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey)
.withSchedule(
SimpleScheduleBuilder.simpleSchedule().withIntervalInHours(repeatIntervalTime).repeatForever())
.startNow().build();
scheduler.scheduleJob(jd, trigger);
}
public void addSimpleJob(JobKey jobKey, TriggerKey triggerKey, Class<? extends Job> cls, int repeatIntervalTime) throws SchedulerException {
addSimpleJob(jobKey, triggerKey, cls, repeatIntervalTime);
}
/**
* 添加 cronJob
*
* @param jobKey
* @param triggerKey
* @param jobClass
* @param cron
* @param jobDataMap
*/
public void addCronJob(JobKey jobKey, TriggerKey triggerKey, Class jobClass, String cron, Date startTime, Date endTime, JobDataMap jobDataMap) {
2021-02-20 10:07:25 +08:00
try {
LogUtil.info("addCronJob: " + triggerKey.getName() + "," + triggerKey.getGroup());
JobBuilder jobBuilder = JobBuilder.newJob(jobClass).withIdentity(jobKey);
if (jobDataMap != null) {
jobBuilder.usingJobData(jobDataMap);
}
JobDetail jobDetail = jobBuilder.build();
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
triggerBuilder.withIdentity(triggerKey);
triggerBuilder.startAt(startTime);
if (endTime != null) {
triggerBuilder.endAt(endTime);
}
2021-02-20 10:07:25 +08:00
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
CronTrigger trigger = (CronTrigger) triggerBuilder.build();
scheduler.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
public void addCronJob(JobKey jobKey, TriggerKey triggerKey, Class jobClass, String cron, Date startTime, Date endTime) {
addCronJob(jobKey, triggerKey, jobClass, cron, startTime, endTime, null);
}
public void addSingleJob(JobKey jobKey, TriggerKey triggerKey, Class jobClass, Date date, JobDataMap jobDataMap) {
try {
LogUtil.info("addSingleJob: " + triggerKey.getName() + "," + triggerKey.getGroup());
JobBuilder jobBuilder = JobBuilder.newJob(jobClass).withIdentity(jobKey);
if (jobDataMap != null) {
jobBuilder.usingJobData(jobDataMap);
}
JobDetail jobDetail = jobBuilder.build();
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
triggerBuilder.withIdentity(triggerKey);
triggerBuilder.startAt(date);
Trigger trigger = triggerBuilder.build();
scheduler.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
public void addSingleJob(JobKey jobKey, TriggerKey triggerKey, Class jobClass, Date date) {
addSingleJob(jobKey, triggerKey, jobClass, date, null);
2021-02-20 10:07:25 +08:00
}
/**
* 修改 cronTrigger
*
* @param triggerKey
* @param cron
* @throws SchedulerException
*/
public void modifyCronJobTime(TriggerKey triggerKey, String cron, Date startTime, Date endTime) throws SchedulerException {
2021-02-20 10:07:25 +08:00
LogUtil.info("modifyCronJobTime: " + triggerKey.getName() + "," + triggerKey.getGroup());
try {
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
if (trigger == null) {
return;
}
/** 方式一 :调用 rescheduleJob 开始 */
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();// 触发器
2021-02-20 10:07:25 +08:00
triggerBuilder.withIdentity(triggerKey);// 触发器名,触发器组
2021-02-20 10:07:25 +08:00
triggerBuilder.startAt(startTime);
2021-02-20 10:07:25 +08:00
if (endTime != null) {
triggerBuilder.endAt(endTime);
}
2021-02-20 10:07:25 +08:00
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));// 触发器时间设定
2021-02-20 10:07:25 +08:00
trigger = (CronTrigger) triggerBuilder.build();// 创建Trigger对象
2021-02-20 10:07:25 +08:00
scheduler.rescheduleJob(triggerKey, trigger);// 修改一个任务的触发时间
/** 方式一 :调用 rescheduleJob 结束 */
2021-02-20 10:07:25 +08:00
/** 方式二先删除然后在创建一个新的Job */
// JobDetail jobDetail = sched.getJobDetail(JobKey.jobKey(jobName, jobGroupName));
// Class<? extends Job> jobClass = jobDetail.getJobClass();
// removeJob(jobName, jobGroupName, triggerName, triggerGroupName);
// addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron);
/** 方式二 先删除然后在创建一个新的Job */
2021-02-20 10:07:25 +08:00
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 修改simpleTrigger触发器的触发时间
*
* @param triggerKey
* @param repeatIntervalTime
* @throws SchedulerException
*/
public void modifySimpleJobTime(TriggerKey triggerKey, int repeatIntervalTime) throws SchedulerException {
try {
LogUtil.info("modifySimpleJobTime: " + triggerKey.getName() + "," + triggerKey.getGroup());
SimpleTrigger trigger = (SimpleTrigger) scheduler.getTrigger(triggerKey);
if (trigger == null) {
return;
}
long oldTime = trigger.getRepeatInterval();
if (oldTime != repeatIntervalTime) {
/** 方式一 :调用 rescheduleJob 开始 */
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();// 触发器builder
triggerBuilder.withIdentity(triggerKey);// 触发器名,触发器组
triggerBuilder.withSchedule(SimpleScheduleBuilder.repeatHourlyForever(repeatIntervalTime));// 更新触发器的重复间隔时间
triggerBuilder.startNow();// 立即执行
trigger = (SimpleTrigger) triggerBuilder.build();// 创建Trigger对象
scheduler.rescheduleJob(triggerKey, trigger);// 修改一个任务的触发时间
/** 方式一 :调用 rescheduleJob 结束 */
/** 方式二先删除然后在创建一个新的Job */
// JobDetail jobDetail = sched.getJobDetail(JobKey.jobKey(jobName, jobGroupName));
// Class<? extends Job> jobClass = jobDetail.getJobClass();
// removeJob(jobName, jobGroupName, triggerName, triggerGroupName);
// addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron);
/** 方式二 先删除然后在创建一个新的Job */
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
public void modifySingleJobTime(TriggerKey triggerKey, Date date) throws SchedulerException {
try {
LogUtil.info("modifySingleJobTime: " + triggerKey.getName() + "," + triggerKey.getGroup());
Trigger trigger = scheduler.getTrigger(triggerKey);
if (trigger == null) {
return;
}
Date oldTime = trigger.getStartTime();
if (oldTime.getTime() != date.getTime()) {
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();// 触发器builder
triggerBuilder.withIdentity(triggerKey);// 触发器名,触发器组
triggerBuilder.startAt(date);
trigger = triggerBuilder.build();// 创建Trigger对象
scheduler.rescheduleJob(triggerKey, trigger);// 修改一个任务的触发时间
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
2021-02-20 10:07:25 +08:00
/**
* @param jobKey
* @param triggerKey
* @Title:
* @Description: 根据job和trigger删除任务
*/
public void removeJob(JobKey jobKey, TriggerKey triggerKey) {
try {
LogUtil.info("RemoveJob: " + jobKey.getName() + "," + jobKey.getGroup());
scheduler.pauseTrigger(triggerKey);
scheduler.unscheduleJob(triggerKey);
scheduler.deleteJob(jobKey);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
public static void startJobs(Scheduler sched) {
try {
sched.start();
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
public void shutdownJobs(Scheduler sched) {
try {
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
/**
* 新增或者修改 simpleJob
*
* @param jobKey
* @param triggerKey
* @param clz
* @param intervalTime
* @param jobDataMap
* @throws SchedulerException
*/
public void addOrUpdateSimpleJob(JobKey jobKey, TriggerKey triggerKey, Class clz,
int intervalTime, JobDataMap jobDataMap) throws SchedulerException {
if (scheduler.checkExists(triggerKey)) {
modifySimpleJobTime(triggerKey, intervalTime);
} else {
addSimpleJob(jobKey, triggerKey, clz, intervalTime, jobDataMap);
}
}
public void addOrUpdateSingleJob(JobKey jobKey, TriggerKey triggerKey, Class clz,
Date date, JobDataMap jobDataMap) throws SchedulerException {
if (scheduler.checkExists(triggerKey)) {
modifySingleJobTime(triggerKey, date);
} else {
addSingleJob(jobKey, triggerKey, clz, date, jobDataMap);
}
}
public void addOrUpdateSingleJob(JobKey jobKey, TriggerKey triggerKey, Class clz,
Date date) throws SchedulerException {
addOrUpdateSingleJob(jobKey, triggerKey, clz, date, null);
}
2021-02-20 10:07:25 +08:00
public void addOrUpdateSimpleJob(JobKey jobKey, TriggerKey triggerKey, Class clz, int intervalTime) throws SchedulerException {
addOrUpdateSimpleJob(jobKey, triggerKey, clz, intervalTime, null);
}
/**
* 添加或修改 cronJob
*
* @param jobKey
* @param triggerKey
* @param jobClass
* @param cron
* @param jobDataMap
* @throws SchedulerException
*/
public void addOrUpdateCronJob(JobKey jobKey, TriggerKey triggerKey, Class jobClass, String cron, Date startTime, Date endTime, JobDataMap jobDataMap) throws SchedulerException {
2021-02-20 10:07:25 +08:00
LogUtil.info("AddOrUpdateCronJob: " + jobKey.getName() + "," + triggerKey.getGroup());
if (scheduler.checkExists(triggerKey)) {
modifyCronJobTime(triggerKey, cron, startTime, endTime);
2021-02-20 10:07:25 +08:00
} else {
addCronJob(jobKey, triggerKey, jobClass, cron, startTime, endTime, jobDataMap);
2021-02-20 10:07:25 +08:00
}
}
public void addOrUpdateCronJob(JobKey jobKey, TriggerKey triggerKey, Class jobClass, String cron, Date startTime, Date endTime) throws SchedulerException {
addOrUpdateCronJob(jobKey, triggerKey, jobClass, cron, startTime, endTime, null);
2021-02-20 10:07:25 +08:00
}
2021-03-18 17:59:52 +08:00
public JobDataMap getDefaultJobDataMap(String resourceId, String expression, String taskId, String updateType) {
2021-02-20 10:07:25 +08:00
JobDataMap jobDataMap = new JobDataMap();
2021-03-10 18:13:27 +08:00
jobDataMap.put("datasetTableId", resourceId);
jobDataMap.put("taskId", taskId);
2021-02-20 10:07:25 +08:00
jobDataMap.put("expression", expression);
2021-03-18 17:59:52 +08:00
jobDataMap.put("updateType", updateType);
2021-02-20 10:07:25 +08:00
return jobDataMap;
}
public Object getCurrentlyExecutingJobs() {
2021-02-20 10:07:25 +08:00
Map<String, String> returnMap = new HashMap<>();
try {
List<JobExecutionContext> currentJobs = scheduler.getCurrentlyExecutingJobs();
for (JobExecutionContext jobCtx : currentJobs) {
String jobName = jobCtx.getJobDetail().getKey().getName();
String groupName = jobCtx.getJobDetail().getJobClass().getName();
returnMap.put("jobName", jobName);
returnMap.put("groupName", groupName);
}
} catch (Exception e) {
2021-02-20 10:07:25 +08:00
e.printStackTrace();
}
return returnMap;
}
}