JobBuilder.java
5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.yoho.quartz.job;
import com.yoho.quartz.contants.Contants;
import com.yoho.quartz.domain.JobDetail;
import com.yoho.quartz.job.inner.YohoInternalJob;
import com.yoho.quartz.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.quartz.*;
import org.quartz.spi.OperableTrigger;
import java.util.*;
import java.util.Calendar;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
/**
* Author:yanzhang.fu
* Date:2018/1/9
* Description:
* Modified By:
**/
public class JobBuilder {
public OperableTrigger buildTrigger(JobDetail jobDetail) {
Trigger trigger = null;
if (jobDetail.getMisfiredPolicy() == null) {
jobDetail.setMisfiredPolicy(0);
}
if (jobDetail.getPriority() == null) {
jobDetail.setPriority(5);
}
if (jobDetail.getJobType() == Contants.JobType.CRON.getType()) {
CronScheduleBuilder builder = cronSchedule(jobDetail.getCron());
switch (jobDetail.getMisfiredPolicy()) {
case CronTrigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY:
builder = builder.withMisfireHandlingInstructionIgnoreMisfires();
break;
case CronTrigger.MISFIRE_INSTRUCTION_SMART_POLICY:
break;
case CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING:
builder = builder.withMisfireHandlingInstructionDoNothing();
break;
case CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW:
builder = builder.withMisfireHandlingInstructionFireAndProceed();
break;
default:
break;
}
Date startDate = new Date();
if (StringUtils.isNotEmpty(jobDetail.getEndDate())) {
if (DateUtils.string2Date(jobDetail.getEndDate(), DateUtils.DATE_TIME_FORMAT).before(new Date())) {
startDate = DateUtils.getBeforeTime(DateUtils.string2Date(jobDetail.getEndDate(), DateUtils.DATE_TIME_FORMAT), 1);
}
}
trigger = newTrigger().withIdentity(jobDetail.getId(), jobDetail.getJobGroup()).startAt(startDate).endAt(DateUtils.string2Date(jobDetail.getEndDate(), DateUtils.DATE_TIME_FORMAT)).withSchedule(builder).withPriority(jobDetail.getPriority()).build();
} else {
SimpleScheduleBuilder builder = simpleSchedule().simpleSchedule().withIntervalInSeconds(jobDetail.getInterval())
.withRepeatCount(jobDetail.getRepeatCount());
switch (jobDetail.getMisfiredPolicy()) {
case Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY:
builder.withMisfireHandlingInstructionIgnoreMisfires();
break;
case SimpleTrigger.MISFIRE_INSTRUCTION_SMART_POLICY:
break;
case SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW:
builder.withMisfireHandlingInstructionFireNow();
break;
case SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT:
builder.withMisfireHandlingInstructionNowWithExistingCount();
break;
case SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT:
builder.withMisfireHandlingInstructionNowWithRemainingCount();
break;
case SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT:
builder.withMisfireHandlingInstructionNextWithRemainingCount();
break;
case SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT:
builder.withMisfireHandlingInstructionNextWithExistingCount();
break;
default:
break;
}
trigger = newTrigger().withIdentity(jobDetail.getId(), jobDetail.getJobGroup())
.startAt(DateUtils.string2Date(jobDetail.getStartDate(), DateUtils.DATE_TIME_FORMAT)).withSchedule(builder).withPriority(jobDetail.getPriority()).build();
}
OperableTrigger trig = (OperableTrigger) trigger;
if (null == trigger.getJobKey()) {
trig.setJobKey(new JobKey(jobDetail.getId(), jobDetail.getJobGroup()));
}
trig.computeFirstFireTime(null);
return trig;
}
public org.quartz.JobDetail buildJobDetail(JobDetail jobDetail) {
org.quartz.JobDetail job = null;
job = newJob(YohoInternalJob.class)
.withIdentity(jobDetail.getId(), jobDetail.getJobGroup()).withDescription(jobDetail.getDesc()).storeDurably()
.build();
job.getJobDataMap().put(Contants.JobDataMapKey.PROCESSOR, jobDetail.getJobClass());
job.getJobDataMap().put(Contants.JobDataMapKey.JOB_CONTEXT, jobDetail.getJobContext());
job.getJobDataMap().put(Contants.JobDataMapKey.MODULE_KEY, jobDetail.getModule());
return job;
}
}