其他分享
首页 > 其他分享> > quartz原理分析2----job和trigger的存储

quartz原理分析2----job和trigger的存储

作者:互联网

quartz原理分析2----job和trigger的存储

2018年07月16日 16:44:56 李民11 阅读数:254

    对于数据库模式的调度任务,job和trigger需要被存储到数据库中。当服务器再次启动的时候调度任务可以继续执行。

    下面分析任务的存储代码。


 
  1. public static void main(String[] args) throws SchedulerException {

  2.  
  3. SchedulerFactory sf = new StdSchedulerFactory();

  4.  
  5. Scheduler scheduler = sf.getScheduler();

  6.  
  7. scheduler.pauseJob(JobKey.jobKey("ramJob", "ramGroup"));

  8. scheduler.pauseTrigger(TriggerKey.triggerKey("ramTrigger", "ramTriggerGroup"));

  9. scheduler.unscheduleJob(TriggerKey.triggerKey("ramTrigger", "ramTriggerGroup"));

  10. scheduler.deleteJob(JobKey.jobKey("ramJob", "ramGroup"));

  11.  
  12. //job

  13. JobDetail jb = JobBuilder.newJob(RamJob.class).withDescription("description:ram job")

  14. .withIdentity("ramJob", "ramGroup").build();

  15.  
  16. //trigger

  17. Trigger trigger = TriggerBuilder.newTrigger().withDescription("ram trigger")

  18. .withIdentity("ramTrigger", "ramTriggerGroup").startAt(new Date(System.currentTimeMillis() + 3 * 1000))

  19. .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build();

  20.  
  21. //包含存储操作

  22. scheduler.scheduleJob(jb, trigger);

  23.  
  24. scheduler.start();

  25. }

    Schedule.scheduleJob方法执行存储操作。


 
  1. public Date scheduleJob(JobDetail jobDetail, Trigger trigger)

  2. throws SchedulerException {

  3. return sched.scheduleJob(jobDetail, trigger);

  4. }

    sched是 QuartzScheduler实例,包含存储job和trigger的方法。 


 
  1. public Date scheduleJob(JobDetail jobDetail,

  2. Trigger trigger) throws SchedulerException {

  3. validateState();

  4.  
  5. if (jobDetail == null) {

  6. throw new SchedulerException("JobDetail cannot be null");

  7. }

  8.  
  9. if (trigger == null) {

  10. throw new SchedulerException("Trigger cannot be null");

  11. }

  12.  
  13. if (jobDetail.getKey() == null) {

  14. throw new SchedulerException("Job's key cannot be null");

  15. }

  16.  
  17. if (jobDetail.getJobClass() == null) {

  18. throw new SchedulerException("Job's class cannot be null");

  19. }

  20.  
  21. OperableTrigger trig = (OperableTrigger)trigger;

  22.  
  23. if (trigger.getJobKey() == null) {

  24. trig.setJobKey(jobDetail.getKey());

  25. } else if (!trigger.getJobKey().equals(jobDetail.getKey())) {

  26. throw new SchedulerException(

  27. "Trigger does not reference given job!");

  28. }

  29.  
  30. trig.validate();

  31.  
  32. Calendar cal = null;

  33. if (trigger.getCalendarName() != null) {

  34. cal = resources.getJobStore().retrieveCalendar(trigger.getCalendarName());

  35. }

  36. Date ft = trig.computeFirstFireTime(cal);

  37.  
  38. if (ft == null) {

  39. throw new SchedulerException(

  40. "Based on configured schedule, the given trigger '" + trigger.getKey() + "' will never fire.");

  41. }

  42.  
  43. resources.getJobStore().storeJobAndTrigger(jobDetail, trig);

  44. notifySchedulerListenersJobAdded(jobDetail);

  45. notifySchedulerThread(trigger.getNextFireTime().getTime());

  46. notifySchedulerListenersSchduled(trigger);

  47.  
  48. return ft;

  49. }

     resources.getJobStore().storeJobAndTrigger(jobDetail, trig);是执行存储的语句。 

    resources是QuartzSchedulerResources的实例,此类中包含JobStore的实例,JobStore是专门用于存储job和trigger对象的接口。此处使用JobStoreSupport中的方法。JobStoreSupport实现了JobStore接口。 


 
  1. public void storeJobAndTrigger(final JobDetail newJob,

  2. final OperableTrigger newTrigger)

  3. throws JobPersistenceException {

  4. executeInLock(

  5. (isLockOnInsert()) ? LOCK_TRIGGER_ACCESS : null,

  6. new VoidTransactionCallback() {

  7. public void executeVoid(Connection conn) throws JobPersistenceException {

  8. storeJob(conn, newJob, false);

  9. storeTrigger(conn, newTrigger, newJob, false,

  10. Constants.STATE_WAITING, false, false);

  11. }

  12. });

  13. }

   storeJob 和 storeTrigger方法使用sql语句插入任务信息。此处不再列出代码。

标签:quartz,job,trigger,SchedulerException,new,null,jobDetail
来源: https://blog.csdn.net/f45056231p/article/details/90204528