1. 程式人生 > >Spring4 Quartz2 動態任務,Spring4整合quartz2.2.3簡單動態任務

Spring4 Quartz2 動態任務,Spring4整合quartz2.2.3簡單動態任務

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代碼 技術分享
  1. /**
  2. * 新增任務
  3. * @param scheduleJob
  4. * @throws Exception
  5. */
  6. @Override
  7. @SuppressWarnings("unchecked")
  8. public void addJobSimple(ScheduleJob scheduleJob) throws Exception{
  9. TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
  10. //任務觸發
  11. Trigger trigger = scheduler.getTrigger(triggerKey);
  12. if (null == trigger) {
  13. JobDetail jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(scheduleJob.getClazz()))
  14. .withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).build();
  15. jobDetail.getJobDataMap().put("scheduleJob", scheduleJob);
  16. SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
  17. if("秒".equals(scheduleJob.getTimeType()) ||
  18. "second".equalsIgnoreCase(scheduleJob.getTimeType())){
  19. simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue()).repeatForever();
  20. }else if("分".equals(scheduleJob.getTimeType()) || "分鐘".equals(scheduleJob.getTimeType()) ||
  21. "minute".equalsIgnoreCase(scheduleJob.getTimeType())){
  22. simpleScheduleBuilder.withIntervalInMinutes(scheduleJob.getTimeValue()).repeatForever();
  23. }else if("時".equals(scheduleJob.getTimeType()) || "小時".equals(scheduleJob.getTimeType()) ||
  24. "hour".equalsIgnoreCase(scheduleJob.getTimeType())){
  25. simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()).repeatForever();
  26. }else if("天".equals(scheduleJob.getTimeType()) ||
  27. "date".equalsIgnoreCase(scheduleJob.getTimeType())){
  28. simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()).repeatForever();
  29. }
  30. ScheduleBuilder<SimpleTrigger> schedBuilder = simpleScheduleBuilder.withMisfireHandlingInstructionNextWithRemainingCount();
  31. trigger =TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup())
  32. .withSchedule(schedBuilder).build();
  33. trigger = TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).withSchedule(schedBuilder).build();
  34. scheduler.scheduleJob(jobDetail, trigger);
  35. log.info(CC.LOG_PREFIX + "新增簡單任務:"+JasonUtils.Object2String(scheduleJob));
  36. }else {
  37. this.updateJobCron(scheduleJob);
  38. }
  39. }

需要註意是的,當定時任務只執行一次時,就要仔細看代碼,如上面的簡單定時任務,剛開始是我這樣的:

Java代碼 技術分享
  1. simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue());

結果只執行一次,而我需要的是不斷循環執行,最後改成:

Java代碼 技術分享
  1. simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue()).repeatForever();

可以看出,後面多了個:repeatForever(),表示永遠重復。

如果是希望執行多少次後不執行,就使用:.withRepeatCount(triggerRepeatCount),如下:

Java代碼 技術分享
  1. simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()).withRepeatCount(triggerRepeatCount);

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

?Copyright 蕃薯耀 2017年9月6日

http://www.cnblogs.com/fanshuyao/

Spring4 Quartz2 動態任務,Spring4整合quartz2.2.3簡單動態任務