2021-02-20 10:07:25 +08:00
|
|
|
|
package io.dataease.job.sechedule;
|
|
|
|
|
|
|
|
|
|
import io.dataease.commons.utils.LogUtil;
|
|
|
|
|
import org.quartz.*;
|
2021-06-02 16:16:20 +08:00
|
|
|
|
import org.quartz.impl.triggers.CronTriggerImpl;
|
2021-02-20 10:07:25 +08:00
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
2021-06-02 16:16:20 +08:00
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.Calendar;
|
2021-06-02 17:08:12 +08:00
|
|
|
|
import java.util.*;
|
2021-02-20 10:07:25 +08:00
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
*/
|
2021-03-05 14:19:23 +08:00
|
|
|
|
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);
|
|
|
|
|
|
2021-06-09 13:53:10 +08:00
|
|
|
|
Date nTimeByCron = getNTimeByCron(cron, startTime);
|
2021-06-11 14:23:53 +08:00
|
|
|
|
// if (startTime.before(new Date())) {
|
|
|
|
|
triggerBuilder.startAt(nTimeByCron);
|
|
|
|
|
// }
|
2021-03-05 14:19:23 +08:00
|
|
|
|
|
|
|
|
|
if (endTime != null) {
|
2021-06-03 12:04:27 +08:00
|
|
|
|
if (endTime.before(nTimeByCron)) {
|
|
|
|
|
triggerBuilder.endAt(nTimeByCron);
|
|
|
|
|
} else {
|
|
|
|
|
triggerBuilder.endAt(endTime);
|
|
|
|
|
}
|
2021-03-05 14:19:23 +08:00
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 14:19:23 +08:00
|
|
|
|
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
|
|
|
|
|
*/
|
2021-03-05 14:19:23 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-03-05 14:19:23 +08:00
|
|
|
|
/** 方式一 :调用 rescheduleJob 开始 */
|
|
|
|
|
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();// 触发器
|
2021-02-20 10:07:25 +08:00
|
|
|
|
|
2021-03-05 14:19:23 +08:00
|
|
|
|
triggerBuilder.withIdentity(triggerKey);// 触发器名,触发器组
|
2021-02-20 10:07:25 +08:00
|
|
|
|
|
2021-06-09 13:53:10 +08:00
|
|
|
|
Date nTimeByCron = getNTimeByCron(cron, startTime);
|
2021-06-11 14:23:53 +08:00
|
|
|
|
// if (startTime.before(new Date())) {
|
|
|
|
|
triggerBuilder.startAt(nTimeByCron);
|
|
|
|
|
// }
|
2021-02-20 10:07:25 +08:00
|
|
|
|
|
2021-03-05 14:19:23 +08:00
|
|
|
|
if (endTime != null) {
|
2021-06-03 12:04:27 +08:00
|
|
|
|
if (endTime.before(nTimeByCron)) {
|
|
|
|
|
triggerBuilder.endAt(nTimeByCron);
|
|
|
|
|
} else {
|
|
|
|
|
triggerBuilder.endAt(endTime);
|
|
|
|
|
}
|
2021-03-05 14:19:23 +08:00
|
|
|
|
}
|
2021-02-20 10:07:25 +08:00
|
|
|
|
|
2021-03-05 14:19:23 +08:00
|
|
|
|
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));// 触发器时间设定
|
2021-02-20 10:07:25 +08:00
|
|
|
|
|
2021-03-05 14:19:23 +08:00
|
|
|
|
trigger = (CronTrigger) triggerBuilder.build();// 创建Trigger对象
|
2021-02-20 10:07:25 +08:00
|
|
|
|
|
2021-03-05 14:19:23 +08:00
|
|
|
|
scheduler.rescheduleJob(triggerKey, trigger);// 修改一个任务的触发时间
|
|
|
|
|
/** 方式一 :调用 rescheduleJob 结束 */
|
2021-02-20 10:07:25 +08:00
|
|
|
|
|
2021-03-05 14:19:23 +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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 14:19:23 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 14:19:23 +08:00
|
|
|
|
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
|
|
|
|
|
*/
|
2021-03-05 14:19:23 +08:00
|
|
|
|
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)) {
|
2021-03-05 14:19:23 +08:00
|
|
|
|
modifyCronJobTime(triggerKey, cron, startTime, endTime);
|
2021-02-20 10:07:25 +08:00
|
|
|
|
} else {
|
2021-03-05 14:19:23 +08:00
|
|
|
|
addCronJob(jobKey, triggerKey, jobClass, cron, startTime, endTime, jobDataMap);
|
2021-02-20 10:07:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 14:19:23 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 14:19:23 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
2021-03-05 14:19:23 +08:00
|
|
|
|
} catch (Exception e) {
|
2021-02-20 10:07:25 +08:00
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnMap;
|
|
|
|
|
}
|
2021-06-02 16:16:20 +08:00
|
|
|
|
|
2021-06-09 13:53:10 +08:00
|
|
|
|
// public static Date getNTimeByCron(String cron) {
|
|
|
|
|
// try {
|
|
|
|
|
// CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
|
|
|
|
|
// cronTriggerImpl.setCronExpression(cron);
|
|
|
|
|
// Calendar calendar = Calendar.getInstance();
|
|
|
|
|
// Date now = calendar.getTime();
|
|
|
|
|
//// calendar.add(java.util.Calendar.YEAR, 1);
|
|
|
|
|
// calendar.add(Calendar.MONTH, 2);
|
|
|
|
|
//
|
|
|
|
|
// List<Date> dates = TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, now, calendar.getTime());
|
|
|
|
|
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
// String nextTime = dateFormat.format(dates.get(0));
|
|
|
|
|
// Date date = dateFormat.parse(nextTime);
|
|
|
|
|
// return date;
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
// throw new RuntimeException(e);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
public static CronTrigger getCronTrigger(String cron) {
|
|
|
|
|
if (!CronExpression.isValidExpression(cron)) {
|
|
|
|
|
throw new RuntimeException("cron :" + cron + " error");
|
|
|
|
|
}
|
|
|
|
|
return TriggerBuilder.newTrigger().withIdentity("Calculate Date").withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Date getNTimeByCron(String cron, Date start) {
|
|
|
|
|
CronTrigger trigger = getCronTrigger(cron);
|
|
|
|
|
if (start == null) {
|
|
|
|
|
start = trigger.getStartTime();
|
2021-06-02 16:16:20 +08:00
|
|
|
|
}
|
2021-06-09 13:53:10 +08:00
|
|
|
|
return trigger.getFireTimeAfter(start);
|
2021-06-02 16:16:20 +08:00
|
|
|
|
}
|
2021-02-20 10:07:25 +08:00
|
|
|
|
}
|