码迷,mamicode.com
首页 > 其他好文 > 详细

Quartz Trigger Priority 触发器优先级

时间:2014-08-21 17:00:04      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   io   文件   for   ar   

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();
    }
}

 

Quartz Trigger Priority 触发器优先级,布布扣,bubuko.com

Quartz Trigger Priority 触发器优先级

标签:style   blog   color   java   io   文件   for   ar   

原文地址:http://www.cnblogs.com/daxin/p/3927432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!