提交 | 用户 | 时间
|
0cceb6
|
1 |
package com.jcdm.quartz.util; |
Y |
2 |
|
|
3 |
import org.quartz.CronScheduleBuilder; |
|
4 |
import org.quartz.CronTrigger; |
|
5 |
import org.quartz.Job; |
|
6 |
import org.quartz.JobBuilder; |
|
7 |
import org.quartz.JobDetail; |
|
8 |
import org.quartz.JobKey; |
|
9 |
import org.quartz.Scheduler; |
|
10 |
import org.quartz.SchedulerException; |
|
11 |
import org.quartz.TriggerBuilder; |
|
12 |
import org.quartz.TriggerKey; |
|
13 |
import com.jcdm.common.constant.Constants; |
|
14 |
import com.jcdm.common.constant.ScheduleConstants; |
|
15 |
import com.jcdm.common.exception.job.TaskException; |
|
16 |
import com.jcdm.common.exception.job.TaskException.Code; |
|
17 |
import com.jcdm.common.utils.StringUtils; |
|
18 |
import com.jcdm.common.utils.spring.SpringUtils; |
|
19 |
import com.jcdm.quartz.domain.SysJob; |
|
20 |
|
|
21 |
/** |
|
22 |
* 定时任务工具类 |
|
23 |
* |
|
24 |
* @author jc |
|
25 |
* |
|
26 |
*/ |
|
27 |
public class ScheduleUtils |
|
28 |
{ |
|
29 |
/** |
|
30 |
* 得到quartz任务类 |
|
31 |
* |
|
32 |
* @param sysJob 执行计划 |
|
33 |
* @return 具体执行任务类 |
|
34 |
*/ |
|
35 |
private static Class<? extends Job> getQuartzJobClass(SysJob sysJob) |
|
36 |
{ |
|
37 |
boolean isConcurrent = "0".equals(sysJob.getConcurrent()); |
|
38 |
return isConcurrent ? QuartzJobExecution.class : QuartzDisallowConcurrentExecution.class; |
|
39 |
} |
|
40 |
|
|
41 |
/** |
|
42 |
* 构建任务触发对象 |
|
43 |
*/ |
|
44 |
public static TriggerKey getTriggerKey(Long jobId, String jobGroup) |
|
45 |
{ |
|
46 |
return TriggerKey.triggerKey(ScheduleConstants.TASK_CLASS_NAME + jobId, jobGroup); |
|
47 |
} |
|
48 |
|
|
49 |
/** |
|
50 |
* 构建任务键对象 |
|
51 |
*/ |
|
52 |
public static JobKey getJobKey(Long jobId, String jobGroup) |
|
53 |
{ |
|
54 |
return JobKey.jobKey(ScheduleConstants.TASK_CLASS_NAME + jobId, jobGroup); |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* 创建定时任务 |
|
59 |
*/ |
|
60 |
public static void createScheduleJob(Scheduler scheduler, SysJob job) throws SchedulerException, TaskException |
|
61 |
{ |
|
62 |
Class<? extends Job> jobClass = getQuartzJobClass(job); |
|
63 |
// 构建job信息 |
|
64 |
Long jobId = job.getJobId(); |
|
65 |
String jobGroup = job.getJobGroup(); |
|
66 |
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(getJobKey(jobId, jobGroup)).build(); |
|
67 |
|
|
68 |
// 表达式调度构建器 |
|
69 |
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); |
|
70 |
cronScheduleBuilder = handleCronScheduleMisfirePolicy(job, cronScheduleBuilder); |
|
71 |
|
|
72 |
// 按新的cronExpression表达式构建一个新的trigger |
|
73 |
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(jobId, jobGroup)) |
|
74 |
.withSchedule(cronScheduleBuilder).build(); |
|
75 |
|
|
76 |
// 放入参数,运行时的方法可以获取 |
|
77 |
jobDetail.getJobDataMap().put(ScheduleConstants.TASK_PROPERTIES, job); |
|
78 |
|
|
79 |
// 判断是否存在 |
|
80 |
if (scheduler.checkExists(getJobKey(jobId, jobGroup))) |
|
81 |
{ |
|
82 |
// 防止创建时存在数据问题 先移除,然后在执行创建操作 |
|
83 |
scheduler.deleteJob(getJobKey(jobId, jobGroup)); |
|
84 |
} |
|
85 |
|
|
86 |
// 判断任务是否过期 |
|
87 |
if (StringUtils.isNotNull(CronUtils.getNextExecution(job.getCronExpression()))) |
|
88 |
{ |
|
89 |
// 执行调度任务 |
|
90 |
scheduler.scheduleJob(jobDetail, trigger); |
|
91 |
} |
|
92 |
|
|
93 |
// 暂停任务 |
|
94 |
if (job.getStatus().equals(ScheduleConstants.Status.PAUSE.getValue())) |
|
95 |
{ |
|
96 |
scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup)); |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* 设置定时任务策略 |
|
102 |
*/ |
|
103 |
public static CronScheduleBuilder handleCronScheduleMisfirePolicy(SysJob job, CronScheduleBuilder cb) |
|
104 |
throws TaskException |
|
105 |
{ |
|
106 |
switch (job.getMisfirePolicy()) |
|
107 |
{ |
|
108 |
case ScheduleConstants.MISFIRE_DEFAULT: |
|
109 |
return cb; |
|
110 |
case ScheduleConstants.MISFIRE_IGNORE_MISFIRES: |
|
111 |
return cb.withMisfireHandlingInstructionIgnoreMisfires(); |
|
112 |
case ScheduleConstants.MISFIRE_FIRE_AND_PROCEED: |
|
113 |
return cb.withMisfireHandlingInstructionFireAndProceed(); |
|
114 |
case ScheduleConstants.MISFIRE_DO_NOTHING: |
|
115 |
return cb.withMisfireHandlingInstructionDoNothing(); |
|
116 |
default: |
|
117 |
throw new TaskException("The task misfire policy '" + job.getMisfirePolicy() |
|
118 |
+ "' cannot be used in cron schedule tasks", Code.CONFIG_ERROR); |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* 检查包名是否为白名单配置 |
|
124 |
* |
|
125 |
* @param invokeTarget 目标字符串 |
|
126 |
* @return 结果 |
|
127 |
*/ |
|
128 |
public static boolean whiteList(String invokeTarget) |
|
129 |
{ |
|
130 |
String packageName = StringUtils.substringBefore(invokeTarget, "("); |
|
131 |
int count = StringUtils.countMatches(packageName, "."); |
|
132 |
if (count > 1) |
|
133 |
{ |
|
134 |
return StringUtils.containsAnyIgnoreCase(invokeTarget, Constants.JOB_WHITELIST_STR); |
|
135 |
} |
|
136 |
Object obj = SpringUtils.getBean(StringUtils.split(invokeTarget, ".")[0]); |
|
137 |
String beanPackageName = obj.getClass().getPackage().getName(); |
|
138 |
return StringUtils.containsAnyIgnoreCase(beanPackageName, Constants.JOB_WHITELIST_STR) |
|
139 |
&& !StringUtils.containsAnyIgnoreCase(beanPackageName, Constants.JOB_ERROR_STR); |
|
140 |
} |
|
141 |
} |