关于quartz 2.x 一些记录

功能需要 审计(即对不同规则的定制的东西进行轮巡)如果每一个轮询都是一个任务的话,则很影响效率,所以将其建成就一个任务,检查最小时间,将最小时间作为下次执行的时间,根据时间和任务传递过来的参数判断执行的是那个规则。

所以此处的任务应当满足,时间的更新,datamap参数的更新。

log.info("------- Initializing ----------------------");

			    SchedulerFactory sf = new StdSchedulerFactory();
			    Scheduler sched = sf.getScheduler();
                 System.out.println(sched.getSchedulerName());
			    log.info("------- Initialization Complete -----------");

			    Date runTime = DateBuilder.evenSecondDate(new Date());
			    log.info("------- Scheduling Job  -------------------");
                //job1、trigger1 用来审计
			    JobDetail job1 = JobBuilder.newJob(DelSchedule.class).withIdentity("job1", "group1").build();
			    Trigger trigger1 = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startAt(runTime).withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatHourlyForever(12)).build();

     具体更新操作

    

 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); 
             Scheduler scheduler = schedulerFactory.getScheduler("DefaultQuartzScheduler");
       JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey("job1", "group1"));
      // retrieve the trigger
        Trigger oldTrigger = scheduler.getTriggersOfJob(JobKey.jobKey("job1", "group1")).get(0);

        // obtain a builder that would produce the trigger
        TriggerBuilder tb = oldTrigger.getTriggerBuilder();

        // update the schedule associated with the builder, and build the new trigger
        // (other builder methods could be called, to change the trigger in any desired way)
        Trigger newTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startAt(minDate).withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatHourlyForever(12)).build();       
          jobDetail.getJobDataMap().put("id",newId);
          scheduler.addJob(jobDetail, true);
          scheduler.rescheduleJob(oldTrigger.getKey(), newTrigger);

 

对于其它一些需求或深入了解,可以进入官网详细了解http://quartz-scheduler.org/documentation/quartz-2.1.x/quick-start

 

 

你可能感兴趣的:(quartz)