JobDataMap
class holds state information for
Job
instances. JobDataMap object is used to store state information for a given
instance of your job class.import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloWorld implements Job{ @Override public void execute(JobExecutionContext jec) throws JobExecutionException { /* Get Job Details */ JobDetail jobDetails = jec.getJobDetail(); /* Get JobDataMap */ JobDataMap dataMap = jobDetails.getJobDataMap(); String author = dataMap.getString("author"); String topic= dataMap.getString("topic"); String about = dataMap.getString("about"); System.out.println("***************************************"); System.out.println("Information in JobDataMap"); System.out.println("author : " + author); System.out.println("topic : " + topic); System.out.println("about : " + about); System.out.println("***************************************\n\n"); } }
import java.util.Date; import org.quartz.JobDataMap; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.triggers.SimpleTriggerImpl; 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 jobDetail = new JobDetailImpl(); jobDetail.setName("First Job"); jobDetail.setJobClass(HelloWorld.class); jobDetail.setDescription("Simple Hello World Application"); jobDetail.setRequestsRecovery(true); jobDetail.setDurability(true); /* Set the JobDataMap for the Job */ JobDataMap dataMap = new JobDataMap (); dataMap.put("author", "Krishna"); dataMap.put("topic", "Quartz Scheduler"); dataMap.put("about", "JobDataMap"); jobDetail.setJobDataMap(dataMap); //Creating schedule time with trigger SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl(); simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000)); simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); simpleTrigger.setRepeatInterval(3000); simpleTrigger.setName("FirstTrigger"); simpleTrigger.setDescription("Simple Trigger"); //Start scheduler scheduler.start(); scheduler.scheduleJob(jobDetail,simpleTrigger); } }
Output
log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. *************************************** Information in JobDataMap author : Krishna topic : Quartz Scheduler about : JobDataMap *************************************** *************************************** Information in JobDataMap author : Krishna topic : Quartz Scheduler about : JobDataMap ***************************************
1.Just like Job, Triggers can also
have JobDataMap associated with them. It is useful for passing to a job
parameters that are specific to the firings of the trigger.
import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.Trigger; public class HelloWorld implements Job{ @Override public void execute(JobExecutionContext jec) throws JobExecutionException { /* Get Job Details */ JobDetail jobDetails = jec.getJobDetail(); /* Get JobDataMap */ JobDataMap jobDataMap = jobDetails.getJobDataMap(); String author = jobDataMap.getString("author"); String topic= jobDataMap.getString("topic"); String about = jobDataMap.getString("about"); /* Get Trigger handle */ Trigger trigger = jec.getTrigger(); /* Get DataMap associated with Trigger */ JobDataMap triggerDataMap =trigger.getJobDataMap(); String repeatInterval = triggerDataMap.getString("repeatInterval"); String triggerName = triggerDataMap.getString("triggerName"); System.out.println("***************************************"); System.out.println("Information in JobDataMap"); System.out.println("author : " + author); System.out.println("topic : " + topic); System.out.println("about : " + about); System.out.println("Information in triggerDataMap"); System.out.println("Repeat Interval is : " + repeatInterval); System.out.println("Trigger Name is : " + triggerName); System.out.println("***************************************\n\n"); } }
import java.util.Date; import org.quartz.JobDataMap; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.triggers.SimpleTriggerImpl; 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 jobDetail = new JobDetailImpl(); jobDetail.setName("First Job"); jobDetail.setJobClass(HelloWorld.class); jobDetail.setDescription("Simple Hello World Application"); jobDetail.setRequestsRecovery(true); jobDetail.setDurability(true); /* Set JobDataMap for the Job */ JobDataMap jobDataMap = new JobDataMap (); jobDataMap.put("author", "Krishna"); jobDataMap.put("topic", "Quartz Scheduler"); jobDataMap.put("about", "JobDataMap"); jobDetail.setJobDataMap(jobDataMap); //Creating schedule time with trigger SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl(); simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000)); simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); simpleTrigger.setRepeatInterval(3000); simpleTrigger.setName("FirstTrigger"); simpleTrigger.setDescription("Simple Trigger"); /* Set triggerDataMap for the Job */ JobDataMap triggerDataMap = new JobDataMap (); triggerDataMap.put("repeatInterval", "3000"); triggerDataMap.put("triggerName", "firstTrigger"); simpleTrigger.setJobDataMap(triggerDataMap); //Start scheduler scheduler.start(); scheduler.scheduleJob(jobDetail,simpleTrigger); } }
Output
log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. *************************************** Information in JobDataMap author : Krishna topic : Quartz Scheduler about : JobDataMap Information in triggerDataMap Repeat Interval is : 3000 Trigger Name is : firstTrigger *************************************** *************************************** Information in JobDataMap author : Krishna topic : Quartz Scheduler about : JobDataMap Information in triggerDataMap Repeat Interval is : 3000 Trigger Name is : firstTrigger ***************************************
No comments:
Post a Comment