|
|
|
@ -13,6 +13,8 @@ import org.quartz.TriggerKey;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import com.ruoyi.common.constant.ScheduleConstants;
|
|
|
|
|
import com.ruoyi.common.exception.job.TaskException;
|
|
|
|
|
import com.ruoyi.common.exception.job.TaskException.Code;
|
|
|
|
|
import com.ruoyi.project.monitor.job.domain.Job;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -25,14 +27,12 @@ public class ScheduleUtils
|
|
|
|
|
{
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ScheduleUtils.class);
|
|
|
|
|
|
|
|
|
|
private final static String JOB_NAME = "TASK_";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取触发器key
|
|
|
|
|
*/
|
|
|
|
|
public static TriggerKey getTriggerKey(Long jobId)
|
|
|
|
|
{
|
|
|
|
|
return TriggerKey.triggerKey(JOB_NAME + jobId);
|
|
|
|
|
return TriggerKey.triggerKey(ScheduleConstants.TASK_CLASS_NAME + jobId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -40,7 +40,7 @@ public class ScheduleUtils
|
|
|
|
|
*/
|
|
|
|
|
public static JobKey getJobKey(Long jobId)
|
|
|
|
|
{
|
|
|
|
|
return JobKey.jobKey(JOB_NAME + jobId);
|
|
|
|
|
return JobKey.jobKey(ScheduleConstants.TASK_CLASS_NAME + jobId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -70,14 +70,14 @@ public class ScheduleUtils
|
|
|
|
|
JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(getJobKey(job.getJobId())).build();
|
|
|
|
|
|
|
|
|
|
// 表达式调度构建器
|
|
|
|
|
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
|
|
|
|
|
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
|
|
|
|
|
cronScheduleBuilder = handleCronScheduleMisfirePolicy(job, cronScheduleBuilder);
|
|
|
|
|
|
|
|
|
|
// 按新的cronExpression表达式构建一个新的trigger
|
|
|
|
|
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(job.getJobId()))
|
|
|
|
|
.withSchedule(scheduleBuilder).build();
|
|
|
|
|
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(job.getJobId())).withSchedule(cronScheduleBuilder).build();
|
|
|
|
|
|
|
|
|
|
// 放入参数,运行时的方法可以获取
|
|
|
|
|
jobDetail.getJobDataMap().put(ScheduleConstants.JOB_PARAM_KEY, job);
|
|
|
|
|
jobDetail.getJobDataMap().put(ScheduleConstants.TASK_PROPERTIES, job);
|
|
|
|
|
|
|
|
|
|
scheduler.scheduleJob(jobDetail, trigger);
|
|
|
|
|
|
|
|
|
@ -91,6 +91,10 @@ public class ScheduleUtils
|
|
|
|
|
{
|
|
|
|
|
log.error(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
catch (TaskException e)
|
|
|
|
|
{
|
|
|
|
|
log.error(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -103,15 +107,16 @@ public class ScheduleUtils
|
|
|
|
|
TriggerKey triggerKey = getTriggerKey(job.getJobId());
|
|
|
|
|
|
|
|
|
|
// 表达式调度构建器
|
|
|
|
|
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
|
|
|
|
|
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
|
|
|
|
|
cronScheduleBuilder = handleCronScheduleMisfirePolicy(job, cronScheduleBuilder);
|
|
|
|
|
|
|
|
|
|
CronTrigger trigger = getCronTrigger(scheduler, job.getJobId());
|
|
|
|
|
|
|
|
|
|
// 按新的cronExpression表达式重新构建trigger
|
|
|
|
|
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
|
|
|
|
|
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build();
|
|
|
|
|
|
|
|
|
|
// 参数
|
|
|
|
|
trigger.getJobDataMap().put(ScheduleConstants.JOB_PARAM_KEY, job);
|
|
|
|
|
trigger.getJobDataMap().put(ScheduleConstants.TASK_PROPERTIES, job);
|
|
|
|
|
|
|
|
|
|
scheduler.rescheduleJob(triggerKey, trigger);
|
|
|
|
|
|
|
|
|
@ -126,6 +131,10 @@ public class ScheduleUtils
|
|
|
|
|
{
|
|
|
|
|
log.error(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
catch (TaskException e)
|
|
|
|
|
{
|
|
|
|
|
log.error(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -138,7 +147,7 @@ public class ScheduleUtils
|
|
|
|
|
{
|
|
|
|
|
// 参数
|
|
|
|
|
JobDataMap dataMap = new JobDataMap();
|
|
|
|
|
dataMap.put(ScheduleConstants.JOB_PARAM_KEY, job);
|
|
|
|
|
dataMap.put(ScheduleConstants.TASK_PROPERTIES, job);
|
|
|
|
|
|
|
|
|
|
scheduler.triggerJob(getJobKey(job.getJobId()), dataMap);
|
|
|
|
|
rows = 1;
|
|
|
|
@ -194,4 +203,23 @@ public class ScheduleUtils
|
|
|
|
|
log.error(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CronScheduleBuilder handleCronScheduleMisfirePolicy(Job job, CronScheduleBuilder cb)
|
|
|
|
|
throws TaskException
|
|
|
|
|
{
|
|
|
|
|
switch (job.getMisfirePolicy())
|
|
|
|
|
{
|
|
|
|
|
case ScheduleConstants.MISFIRE_DEFAULT:
|
|
|
|
|
return cb;
|
|
|
|
|
case ScheduleConstants.MISFIRE_IGNORE_MISFIRES:
|
|
|
|
|
return cb.withMisfireHandlingInstructionIgnoreMisfires();
|
|
|
|
|
case ScheduleConstants.MISFIRE_FIRE_AND_PROCEED:
|
|
|
|
|
return cb.withMisfireHandlingInstructionFireAndProceed();
|
|
|
|
|
case ScheduleConstants.MISFIRE_DO_NOTHING:
|
|
|
|
|
return cb.withMisfireHandlingInstructionDoNothing();
|
|
|
|
|
default:
|
|
|
|
|
throw new TaskException("The task misfire policy '" + job.getMisfirePolicy()
|
|
|
|
|
+ "' cannot be used in cron schedule tasks", Code.CONFIG_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|