quartz2.3.0 (fourteen) trigger trigger prioritization

job task categories:

Package org.quartz.examples.example14; 

Import org.slf4j.Logger;
 Import org.slf4j.LoggerFactory;
 Import the org.quartz.Job;
 Import org.quartz.JobExecutionContext;
 Import org.quartz.JobExecutionException; 

/ ** 
 * a simple the job task 
 * / 
public  class TriggerEchoJob the implements the job { 

    Private  static  Final Logger LoggerFactory.getLogger the LOG = (TriggerEchoJob. class ); 

    // must be modified public constructor with no arguments 
    public TriggerEchoJob () { 
    } 

    // task execution method of 
    public void execute(JobExecutionContext context) throws JobExecutionException {
        LOG.info("任务执行。TRIGGER: " + context.getTrigger().getKey());
    }

}

 

Scheduler class:

package org.quartz.examples.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 org.quartz.DateBuilder.IntervalUnit;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
importorg.slf4j.LoggerFactory; 

Import java.util.Date; 

/ ** 
 * This example demonstrates how the trigger is sorted by priority. 
 * / 
Public  class PriorityExample { 
    Logger the LOG = LoggerFactory.getLogger (PriorityExample. Class ); 

    public  void RUN () throws Exception { 

        // initialization scheduler
 //         the SchedulerFactory the StdSchedulerFactory new new SF = ( "ORG / Quartz / examples / example14 / quartz_priority. Properties "); 
        the SchedulerFactory SF = new new the StdSchedulerFactory (); 
        Scheduler sched = sf.getScheduler (); 

        the JobDetail JobNewJob = (TriggerEchoJob. Class ) .withIdentity ( "TriggerEchoJob" ;) .build () 

        // Once prioritized, the larger the number three priority triggers will also trigger them first, the higher the priority .
        // then repeat once, because the time interval in seconds is not the same, it will trigger the display does not seem to trigger in order of priority, in fact, did not ignore the precedence. 

        // the provision of the trigger priority, so we should see the order of execution of the following triggers:
         // 1. Priority10Trigger15SecondRepeat
         // 2. Priority5Trigger10SecondRepeat
         // 3. Priority1Trigger5SecondRepeat
         // 4. Priority1Trigger5SecondRepeat
         // 5. The Priority5Trigger10SecondRepeat
         // . 6 . Priority10Trigger15SecondRepeat 

        // the Calculate All the Triggers at The Start Time of 5 seconds The AS from now
        FutureDate the startTime = DATE (. 5 , IntervalUnit.SECOND); 

        // first trigger priority 1 (withPriority), lower the priority number, the lower priority trigger 
        the Trigger trigger1 = newTrigger () withIdentity ( "Priority1Trigger5SecondRepeat." ) .startAt (the startTime) 
                .withSchedule (. simpleSchedule () withRepeatCount ( .. 1) .withIntervalInSeconds (. 5)) withPriority (. 1 ) .forJob (Job) 
                .build (); 

        // second trigger priority is not set, the default implementation of priority set priority flip-flop, flip-flop so that the lowest priority 
        the trigger Trigger2 = newTrigger (). withIdentity ( "Priority5Trigger10SecondRepeat" ) .startAt (startTime) 
                .withSchedule (simpleSchedule (). withRepeatCount ( 1) .withIntervalInSeconds (10 .)) forJob (the Job) .build (); 

        //Trigger priority 10 (withPriority), the greater the priority number the higher the priority of the flip-flop, flip-flop in the case of performing concurrent higher priority trigger execution of the front 
        Trigger trigger3 = newTrigger (). WithIdentity ( " Priority10Trigger15SecondRepeat " ) .startAt (the startTime) 
                .withSchedule (simpleSchedule () withRepeatCount (. . 1) .withIntervalInSeconds (15)) withPriority (10. ) 
                .forJob (Job) .build (); 

        sched.scheduleJob (Job, trigger1); 
        sched .scheduleJob (Trigger2); 
        sched.scheduleJob (trigger 3); 
        
        // start the scheduler, only to start the scheduler, triggers and tasks will actually perform 
        sched.start (); 
        log.info ( "------- Scheduler ----------------- Started " ); 
        Thread.sleep ( 30L * 1000L );
         //关闭调度器
        sched.shutdown(true);
        LOG.info("------- Shutdown Complete -----------------");
    }

    public static void main(String[] args) throws Exception {
        PriorityExample example = new PriorityExample();
        example.run();
    }
}

 

Guess you like

Origin www.cnblogs.com/zhuwenjoyce/p/11241279.html