Saturday 27 July 2019

Quartz: Priority of the trigger


Trigger interface provides 'getPriority' method, to get the priority of the trigger.

What is the use of trigger priority?
It acts as a tiebreaker. For example, if there are two triggers 'T1', 'T2' have scheduled at the same time, then the trigger with high priority will get access to the worker thread first.

If the application does not set the priority, then default priority is 5.

Find the below working application.

HelloJob.java
package com.sample.jobs;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class HelloJob implements Job {
 public void execute(JobExecutionContext jec) throws JobExecutionException {
  System.out.println("Hello!!!");
 }
}


PrintJob.java
package com.sample.jobs;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * Job prints 'Hello World' message to console.
 * 
 * @author Krishna
 *
 */
public class PrintJob implements Job {
 public void execute(JobExecutionContext jec) throws JobExecutionException {
  System.out.println("Printing.....");
 }
}


QuartzSchedulerEx.java
package com.sample.app;

import java.util.Date;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.SimpleTriggerImpl;

import com.sample.jobs.HelloJob;
import com.sample.jobs.PrintJob;

public class QuartzSchedulerEx {
 public static void main(String args[]) throws SchedulerException {

  // Initiate a Schedule Factory
  SchedulerFactory schedulerFactory = new StdSchedulerFactory();
  // Retrieve a scheduler from schedule factory
  Scheduler scheduler = schedulerFactory.getScheduler();

  // Creating JobDetailImpl and link to our Job class
  JobDetailImpl printJobDetails = new JobDetailImpl();
  printJobDetails.setName("First Job");
  printJobDetails.setJobClass(PrintJob.class);

  JobDetailImpl helloJobDetails = new JobDetailImpl();
  helloJobDetails.setName("Second Job");
  helloJobDetails.setJobClass(HelloJob.class);

  // Creating schedule time with trigger
  SimpleTriggerImpl trigger1 = new SimpleTriggerImpl();
  trigger1.setStartTime(new Date(System.currentTimeMillis() + 1000));
  trigger1.setRepeatCount(2);
  trigger1.setRepeatInterval(1000);
  trigger1.setName("First Trigger");
  trigger1.setPriority(10); // Seting trigger 1 priority to 10

  SimpleTriggerImpl trigger2 = new SimpleTriggerImpl();
  trigger2.setStartTime(new Date(System.currentTimeMillis() + 1000));
  trigger2.setRepeatCount(2);
  trigger2.setRepeatInterval(1000);
  trigger2.setName("Second Trigger");

  // Start scheduler
  scheduler.start();
  scheduler.scheduleJob(printJobDetails, trigger1);
  scheduler.scheduleJob(helloJobDetails, trigger2);

  System.out.println("Trigger 1 priority : " + trigger1.getPriority());
  System.out.println("Trigger 2 priority : " + trigger2.getPriority());

 }
}

Output
Trigger 1 priority : 10
Trigger 2 priority : 5
Printing.....
Hello!!!
Printing.....
Hello!!!
Printing.....
Hello!!!



Previous                                                    Next                                                    Home

No comments:

Post a Comment