Quartz uses trigger keys to uniquely identify triggers. A
trigger key composed of two components.
a.
Name
b.
Group
Name of the trigger must be unique in a group. Trigger
key is formed by groupName followed by . followed by name.
TriggerKey class is defined like below.
Example
SimpleTriggerImpl trigger1 = new SimpleTriggerImpl();
trigger1.setKey(new
TriggerKey("printJobTrigger", "printMsgsGroup"));
Trigger key for above example is ‘printMsgsGroup.printJobTrigger’.
If you do not specify the group, then the default group
name is 'DEFAULT'.
Find the below working example.
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.TriggerKey; 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 trigger1.setKey(new TriggerKey("printJobTrigger", "printMsgsGroup")); SimpleTriggerImpl trigger2 = new SimpleTriggerImpl(); trigger2.setStartTime(new Date(System.currentTimeMillis() + 1000)); trigger2.setRepeatCount(2); trigger2.setRepeatInterval(1000); trigger2.setName("Second Trigger"); trigger2.setKey(new TriggerKey("helloJobTrigger", "printMsgsGroup")); // Start scheduler scheduler.start(); // Schedule the jobs using triggers scheduler.scheduleJob(printJobDetails, trigger1); scheduler.scheduleJob(helloJobDetails, trigger2); System.out.println("Trigger 1 key : " + trigger1.getKey()); System.out.println("Trigger 2 key : " + trigger2.getKey()); } }
Output
Trigger 1 key : printMsgsGroup.printJobTrigger
Trigger 2 key : printMsgsGroup.helloJobTrigger
Printing.....
Hello!!!
Printing.....
Hello!!!
Printing.....
Hello!!!
No comments:
Post a Comment