|
|
@@ -22,18 +22,15 @@ import com.ylx.quartz.domain.SysJob;
|
|
|
* 定时任务工具类
|
|
|
*
|
|
|
* @author ylx
|
|
|
- *
|
|
|
*/
|
|
|
-public class ScheduleUtils
|
|
|
-{
|
|
|
+public class ScheduleUtils {
|
|
|
/**
|
|
|
* 得到quartz任务类
|
|
|
*
|
|
|
* @param sysJob 执行计划
|
|
|
* @return 具体执行任务类
|
|
|
*/
|
|
|
- private static Class<? extends Job> getQuartzJobClass(SysJob sysJob)
|
|
|
- {
|
|
|
+ private static Class<? extends Job> getQuartzJobClass(SysJob sysJob) {
|
|
|
boolean isConcurrent = "0".equals(sysJob.getConcurrent());
|
|
|
return isConcurrent ? QuartzJobExecution.class : QuartzDisallowConcurrentExecution.class;
|
|
|
}
|
|
|
@@ -41,24 +38,24 @@ public class ScheduleUtils
|
|
|
/**
|
|
|
* 构建任务触发对象
|
|
|
*/
|
|
|
- public static TriggerKey getTriggerKey(Long jobId, String jobGroup)
|
|
|
- {
|
|
|
+ public static TriggerKey getTriggerKey(Long jobId, String jobGroup) {
|
|
|
return TriggerKey.triggerKey(ScheduleConstants.TASK_CLASS_NAME + jobId, jobGroup);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 构建任务键对象
|
|
|
*/
|
|
|
- public static JobKey getJobKey(Long jobId, String jobGroup)
|
|
|
- {
|
|
|
+ public static JobKey getJobKey(Long jobId, String jobGroup) {
|
|
|
return JobKey.jobKey(ScheduleConstants.TASK_CLASS_NAME + jobId, jobGroup);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建定时任务
|
|
|
+ *
|
|
|
+ * @param scheduler 调度器
|
|
|
+ * @param job 调度信息
|
|
|
*/
|
|
|
- public static void createScheduleJob(Scheduler scheduler, SysJob job) throws SchedulerException, TaskException
|
|
|
- {
|
|
|
+ public static void createScheduleJob(Scheduler scheduler, SysJob job) throws SchedulerException, TaskException {
|
|
|
Class<? extends Job> jobClass = getQuartzJobClass(job);
|
|
|
// 构建job信息
|
|
|
Long jobId = job.getJobId();
|
|
|
@@ -77,22 +74,19 @@ public class ScheduleUtils
|
|
|
jobDetail.getJobDataMap().put(ScheduleConstants.TASK_PROPERTIES, job);
|
|
|
|
|
|
// 判断是否存在
|
|
|
- if (scheduler.checkExists(getJobKey(jobId, jobGroup)))
|
|
|
- {
|
|
|
+ if (scheduler.checkExists(getJobKey(jobId, jobGroup))) {
|
|
|
// 防止创建时存在数据问题 先移除,然后在执行创建操作
|
|
|
scheduler.deleteJob(getJobKey(jobId, jobGroup));
|
|
|
}
|
|
|
|
|
|
// 判断任务是否过期
|
|
|
- if (StringUtils.isNotNull(CronUtils.getNextExecution(job.getCronExpression())))
|
|
|
- {
|
|
|
+ if (StringUtils.isNotNull(CronUtils.getNextExecution(job.getCronExpression()))) {
|
|
|
// 执行调度任务
|
|
|
scheduler.scheduleJob(jobDetail, trigger);
|
|
|
}
|
|
|
|
|
|
// 暂停任务
|
|
|
- if (job.getStatus().equals(ScheduleConstants.Status.PAUSE.getValue()))
|
|
|
- {
|
|
|
+ if (job.getStatus().equals(ScheduleConstants.Status.PAUSE.getValue())) {
|
|
|
scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
|
|
|
}
|
|
|
}
|
|
|
@@ -101,10 +95,8 @@ public class ScheduleUtils
|
|
|
* 设置定时任务策略
|
|
|
*/
|
|
|
public static CronScheduleBuilder handleCronScheduleMisfirePolicy(SysJob job, CronScheduleBuilder cb)
|
|
|
- throws TaskException
|
|
|
- {
|
|
|
- switch (job.getMisfirePolicy())
|
|
|
- {
|
|
|
+ throws TaskException {
|
|
|
+ switch (job.getMisfirePolicy()) {
|
|
|
case ScheduleConstants.MISFIRE_DEFAULT:
|
|
|
return cb;
|
|
|
case ScheduleConstants.MISFIRE_IGNORE_MISFIRES:
|
|
|
@@ -125,12 +117,10 @@ public class ScheduleUtils
|
|
|
* @param invokeTarget 目标字符串
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static boolean whiteList(String invokeTarget)
|
|
|
- {
|
|
|
+ public static boolean whiteList(String invokeTarget) {
|
|
|
String packageName = StringUtils.substringBefore(invokeTarget, "(");
|
|
|
int count = StringUtils.countMatches(packageName, ".");
|
|
|
- if (count > 1)
|
|
|
- {
|
|
|
+ if (count > 1) {
|
|
|
return StringUtils.containsAnyIgnoreCase(invokeTarget, Constants.JOB_WHITELIST_STR);
|
|
|
}
|
|
|
Object obj = SpringUtils.getBean(StringUtils.split(invokeTarget, ".")[0]);
|