Using JobDataMap in Quartz Scheduler

The last post was an introduction to scheduling Jobs using Quartz Scheduler. This post is a continuation to it which shells out some more information on using Quartz Scheduler.

At the end of the last post I mentioned that for every run of the scheduled job, a new instance of the job class is created and run, thus making data persistence across different runs of jobs useless with local variables, also inability in passing arguments.

This problem can be overcome by using the JobDataMap. A jobDataMap is actually a MAP which contains key-value pairs of data that can be shared across scheduled jobs in configuring/tracking the job. Below is a sample application which demos the usage of JobDataMap which shares author name.

AlarmJob.java –

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionException;

public class AlarmJob implements Job {

	@Override
	public void execute(JobExecutionContext jeContext) throws JobExecutionException {
		JobDataMap jdMap = jeContext.getJobDetail().getJobDataMap();
		String auth_name = jdMap.get("auth_name").toString();
		System.out.println("WAKE UP CALL "+new Date()+" by "+auth_name);
	}

}

AlarmSchedule.java –

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;

public class AlarmSchedule {

	public AlarmSchedule(){
		try{
			SchedulerFactory schdFact = new StdSchedulerFactory();
			Scheduler schd = schdFact.getScheduler();
			schd.start();
			JobDetail jd = new JobDetail("alarmjob", Scheduler.DEFAULT_GROUP, AlarmJob.class);
			jd.getJobDataMap().put("auth_name", "vageesh");
			Trigger t = TriggerUtils.makeDailyTrigger("alarmtrigger", 06, 00);
			t.setStartTime(new Date());
			schd.scheduleJob(jd, t);
		}
		catch(SchedulerException e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new AlarmSchedule();
	}

}

 

Please note that in the above program the jobDataMap is associated with the JobDetail. However, a jobDataMap can also be associated with a trigger and not just jobDetail or even both trigger and jobDetail.

The statement

JobDataMap jdMap = jeContext.getJobDetail().getJobDataMap();

Gets the JobDataMap associated with a jobDetail.
In order to access the JobDataMap associated with a Trigger, the following statement can be used –

JobDataMap jdMap = jeContext.getTrigger().getJobDataMap();

In case jobDataMap associated with both jobDetail and Trigger are used, the jobDataMap accessible in the job instance is actually a merge of the two maps with the values of the latter overriding that of the former. The code below describes the usage in such cases –

JobDataMap jdMap = jeContext.getMergedJobDataMap();

One important point to note here is that any changes that are made to the Map during the job execution are not reflected outside the job. That means any changes to the map made during the job execution is limited to that particular execution of the job and is immediately lost once the execution is complete.

One thought on “Using JobDataMap in Quartz Scheduler

Leave a comment