'org.quartz.JobBuilder' class uses builder pattern to
instantiate JobDetail object.
Example
JobDetailImpl printJobDetails = new JobDetailImpl();
printJobDetails.setJobClass(PrintJob.class);
printJobDetails.setName("PrintJob");
printJobDetails.setGroup("PrintMessagesGroup");
printJobDetails.setJobDataMap(jobDataMap);
You can rewrite above lines using builder pattern like
below.
JobDetail printJobDetails = newJob(PrintJob.class)
.withIdentity("PrintJob",
"PrintMessagesGroup")
.usingJobData(jobDataMap)
.build();
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; public class PrintJob implements Job { public void execute(JobExecutionContext jec) throws JobExecutionException { 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()); } } }
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.Trigger; 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"); // Creating JobDetailImpl and link to our Job class JobDetail printJobDetails = newJob(PrintJob.class) .withIdentity("PrintJob", "PrintMessagesGroup") .usingJobData(jobDataMap) .build(); JobDataMap triggerJobDataMap = new JobDataMap(); triggerJobDataMap.put("userName", "krishna123"); triggerJobDataMap.put("token", "myToken4321"); // Creating schedule time with trigger Trigger trigger = newTrigger().withIdentity(triggerKey("myTrigger", "myTriggerGroup")) .withSchedule(simpleSchedule().withIntervalInSeconds(10).repeatForever()) .startAt(futureDate(500, IntervalUnit.MILLISECOND)).withDescription("Job executes every 10 seconds") .usingJobData(triggerJobDataMap).build(); // Start scheduler scheduler.start(); // Schedule the jobs using triggers scheduler.scheduleJob(printJobDetails, trigger); } }
Run 'QuartzSchedulerEx.java', you can able to see below
messages in console.
Printing Job data map information
Description : This job prints some message to console
Printing Trigger Job data map information
userName : krishna123
token : myToken4321
....
....
....
No comments:
Post a Comment