Saturday, 27 July 2019

Quartz: Job keys


Job specifies an action to be performed by scheduler. Every trigger maintains a job key to uniquely identify job information.

A trigger key composed of two components.
a.   Name
b.   Group

Job key is formed by groupName followed by . followed by name.

JobKey class is defined like below.




Example
JobDetailImpl printJobDetails = new JobDetailImpl();
printJobDetails.setJobClass(PrintJob.class);
printJobDetails.setName("PrintJob");
printJobDetails.setGroup("PrintMessages");

For the above job, the job key value is equal to ‘PrintMessages.PrintJob’.

If you do not specify the group, then the default group name is 'DEFAULT'.

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.setJobClass(PrintJob.class);
  printJobDetails.setName("PrintJob");
  printJobDetails.setGroup("PrintMessages");

  JobDetailImpl helloJobDetails = new JobDetailImpl();
  helloJobDetails.setJobClass(HelloJob.class);
  helloJobDetails.setName("HelloJob");
  helloJobDetails.setGroup("PrintMessages");

  // 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");

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

  // Schedule the jobs using triggers
  scheduler.scheduleJob(printJobDetails, trigger1);
  scheduler.scheduleJob(helloJobDetails, trigger2);

  System.out.println("Trigger 1 job key : " + trigger1.getJobKey());
  System.out.println("Trigger 2 job key : " + trigger2.getJobKey());

 }
}

Output
Trigger 1 job key : PrintMessages.PrintJob
Trigger 2 job key : PrintMessages.HelloJob
Printing.....
Hello!!!
Printing.....
Hello!!!
Printing.....
Hello!!!





Previous                                                    Next                                                    Home

No comments:

Post a Comment