首页 > 代码库 > Quartz Trigger Priority 触发器优先级

Quartz Trigger Priority 触发器优先级

Quartz Trigger Priority 触发器优先级

 

当多个触发器在一个相同的时间内触发,并且调度引擎中的资源有限的情况下,那么具有较高优先级的触发器先触发。

需要将配置文件中的org.quartz.threadPool.threadCount = 1设置为1,这样能更好的测试出效果。

 

package com.gary.operation.jobdemo.example14;import static org.quartz.DateBuilder.futureDate;import static org.quartz.JobBuilder.newJob;import static org.quartz.SimpleScheduleBuilder.simpleSchedule;import static org.quartz.TriggerBuilder.newTrigger;import java.util.Date;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerFactory;import org.quartz.Trigger;import org.quartz.DateBuilder.IntervalUnit;import org.quartz.impl.StdSchedulerFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class PriorityExample {        public void run() throws Exception {        Logger log = LoggerFactory.getLogger(PriorityExample.class);        // First we must get a reference to a scheduler        SchedulerFactory sf = new StdSchedulerFactory();        Scheduler sched = sf.getScheduler();        JobDetail job = newJob(TriggerEchoJob.class)            .withIdentity("TriggerEchoJob")            .build();                    Date startTime = futureDate(5, IntervalUnit.SECOND);                Trigger trigger1 = newTrigger()            .withIdentity("Priority7 Trigger5SecondRepeat")            .startAt(startTime)            .withSchedule(simpleSchedule().withRepeatCount(1).withIntervalInSeconds(5))            .withPriority(7)            .forJob(job)            .build();        Trigger trigger2 = newTrigger()            .withIdentity("Priority5 Trigger10SecondRepeat")            .startAt(startTime)            .withPriority(5)            .withSchedule(simpleSchedule().withRepeatCount(1).withIntervalInSeconds(5))            .forJob(job)            .build();                Trigger trigger3 = newTrigger()            .withIdentity("Priority10 Trigger15SecondRepeat")            .startAt(startTime)            .withSchedule(simpleSchedule().withRepeatCount(1).withIntervalInSeconds(5))            .withPriority(10)            .forJob(job)            .build();        // Tell quartz to schedule the job using our trigger        sched.scheduleJob(job, trigger1);        sched.scheduleJob(trigger2);        sched.scheduleJob(trigger3);        sched.start();        log.info("------- Waiting 30 seconds... -------------");        try {            Thread.sleep(30L * 1000L);             // executing...        } catch (Exception e) {        }        sched.shutdown(true);    }    public static void main(String[] args) throws Exception {        PriorityExample example = new PriorityExample();        example.run();    }}