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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| import java.util.Objects;
import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.TriggerBuilder; import org.quartz.TriggerKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
import com.common.exception.ExceptionUtil; import com.common.exception.QuartzErrorCode;
@Component public class QuartzService { private static final Logger LOGGER = LoggerFactory.getLogger(QuartzService.class);
@Autowired private Scheduler scheduler;
public void startJobs() { try { if (scheduler.isStarted()) { LOGGER.warn("该 scheduler 已经被启动了."); return; } scheduler.start(); } catch (SchedulerException e) { handleScheExp(null, e); } }
public void shutdownJobs() { try { if (scheduler.isShutdown()) { LOGGER.warn("该 scheduler 已经被停止了."); return; } scheduler.shutdown(); } catch (SchedulerException e) { handleScheExp(null, e); } }
public void addJob(QuartzJobDomain job) { ExceptionUtil .checkArgument(job.verify(), QuartzErrorCode.Q_PARAM_NEC_NULL, String.format("作业 [%s] 必填参数为空.", job)); JobDetail jobDetail = createJobDetail(job);
CronTrigger trigger = createCronTrigger(job); try { scheduler.scheduleJob(jobDetail, trigger); this.startJobs(); } catch (Exception e) { handleScheExp(job, e); } }
public void modifyJobTime(QuartzJobDomain job) { ExceptionUtil.checkArgument( job.verifyTrigger(), QuartzErrorCode.Q_PARAM_NEC_NULL, String.format("作业 [%s] trigger 信息必填参数为空.", job)); String cron = ExceptionUtil .checkNonBlank(job.getCron(), QuartzErrorCode.Q_PARAM_NEC_NULL, String.format("作业 [%s] cron 信息不能为空.", job));
TriggerKey tk = getTriggerKey(job); try { CronTrigger oldTrigger = (CronTrigger)scheduler.getTrigger(tk); if (oldTrigger == null) { return; } String oldCron = oldTrigger.getCronExpression(); if (!oldCron.equalsIgnoreCase(cron)) { CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron); CronTrigger trigger = oldTrigger.getTriggerBuilder().withSchedule(cronScheduleBuilder).build(); scheduler.rescheduleJob(tk, trigger); } } catch (Exception e) { handleScheExp(job, e); } }
public void removeJob(QuartzJobDomain job) { ExceptionUtil .checkArgument(job.verifyJAndT(), QuartzErrorCode.Q_PARAM_NEC_NULL, String.format("作业 [%s] 必填参数为空.", job));
JobKey jk = getJobKey(job); TriggerKey tk = getTriggerKey(job); try { scheduler.pauseTrigger(tk); scheduler.unscheduleJob(tk); scheduler.deleteJob(jk); } catch (Exception e) { handleScheExp(job, e); } }
public void resumeJob(QuartzJobDomain job) { ExceptionUtil .checkArgument(job.verifyJob(), QuartzErrorCode.Q_PARAM_NEC_NULL, String.format("作业 [%s] 必填参数为空.", job));
JobKey jk = getJobKey(job); try { scheduler.resumeJob(jk); } catch (Exception e) { handleScheExp(job, e); } }
public void execOnce(QuartzJobDomain job) { ExceptionUtil .checkArgument(job.verifyJob(), QuartzErrorCode.Q_PARAM_NEC_NULL, String.format("作业 [%s] 必填参数为空.", job)); JobKey jk = getJobKey(job); try { scheduler.triggerJob(jk); } catch (Exception e) { handleScheExp(job, e); } }
private void handleScheExp(QuartzJobDomain job, Exception e) { String msg = "scheduler 执行异常"; if (Objects.nonNull(job)) { msg = String.format("scheduler 执行异常, 作业信息为 [jobName=%s].", job.getJobName()); } LOGGER.error(msg, e); throw ExceptionUtil.asExceptionUtil(QuartzErrorCode.SCHEDULER_EXCP, msg); }
private static JobDetail createJobDetail(QuartzJobDomain job) { return JobBuilder.newJob(job.getJobClass()) .usingJobData(job.getJobDataMap()) .withIdentity(getJobKey(job)) .build(); }
public static JobKey getJobKey(QuartzJobDomain job) { return JobKey.jobKey(job.getJobName(), job.getJobGroupName()); }
private static CronTrigger createCronTrigger(QuartzJobDomain job) { CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCron()); CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(job)).withSchedule(cronScheduleBuilder).build(); return trigger; }
public static TriggerKey getTriggerKey(QuartzJobDomain job) { return TriggerKey.triggerKey(job.getTriggerName(), job.getTriggerGroupName()); } }
|