Implementing more than one Job Details and Triggers

In this quartz tutorial, we will learn how to implement more than one triggers and jobs with a quartz scheduler.

Implementing more than one Job Details and Triggers

Implementing more than one Job Details and Triggers

     

In this quartz tutorial, we will learn how to implement more than one triggers and jobs with a quartz scheduler. We know that the scheduler is a main interface of quartz scheduler that contains Job Details and Triggers. It has a name and group associated with them and it can be identified by the single scheduler. See below for an implementation of more than one job details and triggers.

Description of program:

Here, we are going to implement more than one job details and triggers with the help of following program that jobs name, triggers name and its firing date and time. We have known about the quartz application, when we wish to implement any quartz application then we will require two classes: one is Scheduler class (MoreTriggerSchedule.java) and another is Job class (MoreTriggerJob.java) that will implement in the Job interface. In this program, the Job class will represent the job's name, trigger's name and its firing times with day and date as well as GMT format time. The scheduler class is a main class of this quartz application that implements the Scheduler instance. But, when we will go to implement this instance then we need an object of the SchedulerFactory invoked by the getScheduler() method. Now, we start the scheduler by using the start() method. The quartz Scheduler has JobDetail and CronTrigger objects. These are attached in the quartz by the scheduleJob() method. After completing the entire processes then we will get all jobs and triggers to be used in it.

Description of code:

(0/5   *  *   *   *   ?): This corn expression provides the facility for firing the trigger every 5 seconds to un-limit time.

(0/8   *   *   *   *   ?): This expression allows to the trigger for firing every 8 seconds to infinite time.

Here is the code of Scheduler class (MoreTriggerSchedule.java):

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class MoreTriggerSchedule {
  public static void main(String args[]){
  try{
  new MoreTriggerSchedule();
  }catch(Exception e){
  e.printStackTrace();
  }
  }
  public MoreTriggerSchedule()throws Exception{
  JobDetail jDetail;
  CronTrigger cronTrigger;
  SchedulerFactory sf = new StdSchedulerFactory();
  Scheduler sche = sf.getScheduler();
sche.start();

  jDetail = new JobDetail("Job1","group1",MoreTriggerJob.class);
  cronTrigger = new CronTrigger("cronTrigger1","group1","0/5 * * * * ?");
  sche.scheduleJob(jDetail, cronTrigger);
  jDetail = new JobDetail("Job2","group2",MoreTriggerJob.class);
  cronTrigger = new CronTrigger("cronTrigger2","group2","0/8 * * * * ?");
  sche.scheduleJob(jDetail, cronTrigger);
  }
}

 Download this example.

Here is the code of Job class (MoreTriggerJob.java):

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class MoreTriggerJob implements Job {
  public void execute(JobExecutionContext contextthrows JobExecutionException {
  // TODO Auto-generated method stub
  System.out.println("Job name: " + context.getJobDetail().getName());
 System.out.println("Trigger name: " + context.getTrigger().getName());
  System.out.println("Firing Time: " + context.getFireTime());
  }
}

 Download this example.

Output of program:

log4j:WARN No appenders could be found for logger (org.quartz.simpl.SimpleThreadPool).

log4j:WARN Please initialize the log4j system properly.

Job name: Job2

Trigger name: cronTrigger2

Firing Time: Tue Feb 27 10:06:56 GMT+05:30 2007

Job name: Job1

Trigger name: cronTrigger1

Firing Time: Tue Feb 27 10:07:00 GMT+05:30 2007

Job name: Job2

Trigger name: cronTrigger2

Firing Time: Tue Feb 27 10:07:00 GMT+05:30 2007