Spring4 Quartz2 動態任務,Spring4整合quartz2.2.3簡單動態任務
阿新 • • 發佈:2017-09-06
extend for ignore extends 執行 void throw uart images
Spring4 Quartz2 動態任務
Spring4整合quartz2.2.3簡單動態任務,
Quartz2.2.3簡單動態定時任務二,
SimpleScheduleBuilder簡單定時任務
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
?Copyright 蕃薯耀 2017年9月6日
http://www.cnblogs.com/fanshuyao/
此處使用的是Quartz中SimpleScheduleBuilder類,非CronScheduleBuilder,CronScheduleBuilder是Cron表達式的。具體請自行百度。
Java代碼
- /**
- * 新增任務
- * @param scheduleJob
- * @throws Exception
- */
- @Override
- @SuppressWarnings("unchecked")
- public void addJobSimple(ScheduleJob scheduleJob) throws Exception{
- TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
- //任務觸發
- 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();
- }
- ScheduleBuilder<SimpleTrigger> 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 + "新增簡單任務:"+JasonUtils.Object2String(scheduleJob));
- }else {
- this.updateJobCron(scheduleJob);
- }
- }
需要註意是的,當定時任務只執行一次時,就要仔細看代碼,如上面的簡單定時任務,剛開始是我這樣的:
Java代碼- simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue());
結果只執行一次,而我需要的是不斷循環執行,最後改成:
Java代碼- simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue()).repeatForever();
可以看出,後面多了個:repeatForever(),表示永遠重復。
如果是希望執行多少次後不執行,就使用:.withRepeatCount(triggerRepeatCount),如下:
Java代碼- simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()).withRepeatCount(triggerRepeatCount);
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
?Copyright 蕃薯耀 2017年9月6日
http://www.cnblogs.com/fanshuyao/
Spring4 Quartz2 動態任務,Spring4整合quartz2.2.3簡單動態任務