'org.quartz.impl.triggers.AbstractTrigger' class provide
below methods to get trigger, job names and group names.
public String getName() : Get the name of the trigger
public String getGroup() : Get the group name of the
trigger
public String getJobName() : Get job name of the trigger
public String getJobGroup() : Get job group of the
trigger
All the other core trigger classes
'CalendarIntervalTriggerImpl', 'CronTriggerImpl',
'DailyTimeIntervalTriggerImpl', 'SimpleTriggerImpl' extends AbstractTrigger
class.
You can provide custom implementation of trigger by
extending 'AbstractTrigger' class or any one of the sub classes.
Find the below working application.
PrintJob.java
package com.sample.jobs; import java.util.Map.Entry; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.impl.triggers.SimpleTriggerImpl; public class PrintJob implements Job { public void execute(JobExecutionContext jec) throws JobExecutionException { System.out.println("********************************************"); System.out.println("Printing Job data map information"); JobDataMap jobDataMap = jec.getJobDetail().getJobDataMap(); for (Entry<String, Object> entry : jobDataMap.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } System.out.println("\nPrinting Trigger Job data map information"); JobDataMap triggerJobDataMap = jec.getTrigger().getJobDataMap(); for (Entry<String, Object> entry : triggerJobDataMap.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } String jobName = ((SimpleTriggerImpl) jec.getTrigger()).getJobName(); String jobGroup = ((SimpleTriggerImpl) jec.getTrigger()).getJobGroup(); String triggerName = ((SimpleTriggerImpl) jec.getTrigger()).getName(); String triggerGroup = ((SimpleTriggerImpl) jec.getTrigger()).getGroup(); String jobInfo = String.format("Trigger '%s' from group '%s' executing the job '%s' of group '%s'", triggerName, triggerGroup, jobName, jobGroup); System.out.println(jobInfo); System.out.println("********************************************"); } }
QuartzSchedulerEx.java
package com.sample.app; 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 static org.quartz.TriggerKey.triggerKey; import org.quartz.DateBuilder.IntervalUnit; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.StdSchedulerFactory; import com.sample.jobs.PrintJob; public class QuartzSchedulerEx { public static void main(String args[]) throws SchedulerException, InterruptedException { // Initiate a Schedule Factory SchedulerFactory schedulerFactory = new StdSchedulerFactory(); // Retrieve a scheduler from schedule factory Scheduler scheduler = schedulerFactory.getScheduler(); JobDataMap jobDataMap = new JobDataMap(); jobDataMap.put("Description", "This job prints some message to console"); JobDataMap triggerJobDataMap = new JobDataMap(); triggerJobDataMap.put("userName", "krishna123"); triggerJobDataMap.put("token", "myToken4321"); // Creating JobDetailImpl and link to our Job class JobDetail printJobDetails = newJob(PrintJob.class).withIdentity("PrintJob", "PrintJobMessagesGroup") .usingJobData(jobDataMap).build(); // Creating schedule time with trigger SimpleTrigger trigger = (SimpleTrigger) (newTrigger().withIdentity(triggerKey("myTrigger", "myTriggerGroup")) .withSchedule(simpleSchedule().withIntervalInSeconds(3).withRepeatCount(1)) .startAt(futureDate(500, IntervalUnit.MILLISECOND)).withDescription("Job executes every 10 seconds") .usingJobData(triggerJobDataMap).build()); scheduler.start(); // Schedule the jobs using triggers scheduler.scheduleJob(printJobDetails, trigger); } }
Output
********************************************
Printing Job data map information
Description : This job prints some message to console
Printing Trigger Job data map information
userName : krishna123
token : myToken4321
Trigger 'myTrigger' from group 'myTriggerGroup' executing
the job 'PrintJob' of group 'PrintJobMessagesGroup'
********************************************
********************************************
Printing Job data map information
Description : This job prints some message to console
Printing Trigger Job data map information
userName : krishna123
token : myToken4321
Trigger 'myTrigger' from group 'myTriggerGroup' executing
the job 'PrintJob' of group 'PrintJobMessagesGroup'
********************************************
No comments:
Post a Comment