Spring Quartz2 動態(tài)任務(wù)的實例詳解
此處使用的是Quartz中SimpleScheduleBuilder類,非CronScheduleBuilder,CronScheduleBuilder是Cron表達式的。具體請自行百度。
實現(xiàn)代碼:
/**
* 新增任務(wù)
* @param scheduleJob
* @throws Exception
*/
@Override
@SuppressWarnings("unchecked")
public void addJobSimple(ScheduleJob scheduleJob) throws Exception{
TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
//任務(wù)觸發(fā)
Trigger trigger = scheduler.getTrigger(triggerKey);
if (null == trigger) {
JobDetail jobDetail = JobBuilder.newJob((Class? extends Job>) Class.forName(scheduleJob.getClazz()))
.withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).build();
jobDetail.getJobDataMap().put("scheduleJob", scheduleJob);
SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
if("秒".equals(scheduleJob.getTimeType()) ||
"second".equalsIgnoreCase(scheduleJob.getTimeType())){
simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue()).repeatForever();
}else if("分".equals(scheduleJob.getTimeType()) || "分鐘".equals(scheduleJob.getTimeType()) ||
"minute".equalsIgnoreCase(scheduleJob.getTimeType())){
simpleScheduleBuilder.withIntervalInMinutes(scheduleJob.getTimeValue()).repeatForever();
}else if("時".equals(scheduleJob.getTimeType()) || "小時".equals(scheduleJob.getTimeType()) ||
"hour".equalsIgnoreCase(scheduleJob.getTimeType())){
simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()).repeatForever();
}else if("天".equals(scheduleJob.getTimeType()) ||
"date".equalsIgnoreCase(scheduleJob.getTimeType())){
simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()).repeatForever();
}
ScheduleBuilderSimpleTrigger> schedBuilder = simpleScheduleBuilder.withMisfireHandlingInstructionNextWithRemainingCount();
trigger =TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup())
.withSchedule(schedBuilder).build();
trigger = TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).withSchedule(schedBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
log.info(CC.LOG_PREFIX + "新增簡單任務(wù):"+JasonUtils.Object2String(scheduleJob));
}else {
this.updateJobCron(scheduleJob);
}
}
需要注意是的,當(dāng)定時任務(wù)只執(zhí)行一次時,就要仔細看代碼,如上面的簡單定時任務(wù),剛開始是我這樣的:
simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue());
結(jié)果只執(zhí)行一次,而我需要的是不斷循環(huán)執(zhí)行,最后改成:
simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue()).repeatForever();
可以看出,后面多了個:repeatForever(),表示永遠重復(fù)。
如果是希望執(zhí)行多少次后不執(zhí)行,就使用:.withRepeatCount(triggerRepeatCount),如下:
simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()).withRepeatCount(triggerRepeatCount);
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:- Spring quartz Job依賴注入使用詳解
- spring和quartz整合,并簡單調(diào)用(實例講解)
- springboot整合quartz實現(xiàn)定時任務(wù)示例
- SpringBoot定時任務(wù)兩種(Spring Schedule 與 Quartz 整合 )實現(xiàn)方法
- 詳解Spring整合Quartz實現(xiàn)動態(tài)定時任務(wù)
- Spring整合Quartz實現(xiàn)動態(tài)定時器的示例代碼
- 在Java的Spring框架中配置Quartz的教程