Quartz JobDataMap Example Part 1

In this post of Quartz scheduler, I will explain what is JobDataMap and how to use it.

JobDataMap is an implementation of java Map interface. In addition to the methods of Map interface additional methods are added for storing and retrieving primitive types.

JobDataMap holds data as a key value pair. It is used to pass data to an instance of Job using two approaches, which are
1) using JobDetail
2) using Trigger

In this post I will explain the first approach

Below is how we create a JobDetail with data to be passed to an instance of job.

1    JobBuilder jobBuilder = JobBuilder.newJob(HelloJob.class);
2    jobBuilder.withDescription("Job Description").withIdentity("jobDetail1", "group1");
3    jobBuilder.usingJobData("firstName", "Alice");
4    jobBuilder.usingJobData("lastName", "Pierce");
6    JobDetail jobDetail = jobBuilder.build();

Again we take the help of JobBuilder class.

We put the data by calling the method “usingJobData”. Refer to line 3 and 4. Where “firstName” and “lastName” is the key, and “Alice” and “Pierce” is the value.

Next we access this data in the Job class as shown below

Job Class

1  import org.quartz.Job;
2  import org.quartz.JobDataMap;
3  import org.quartz.JobExecutionContext;
4  import org.quartz.JobExecutionException;
6  public class HelloJob implements Job {
7   private String firstName;
8   private String lastName;
10     @Override
11     public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
12          JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
13          lastName = jobDataMap.getString("lastName");
14          firstName = jobDataMap.getString("firstName");
15          System.out.println("Hello " + firstName + " " + lastName + " Job is executing.");
16     }
17 }

At line 12 we access the instance of JobDataMap.

At line 13 and 14, we access the data passed from JobDetail through JobDataMap.

The JobDataMap obtained at line 12 contains data merged from both the JobDetail and Trigger, with values in the latter overriding the values with same named keys in the former.

Whenever trigger is fired according to the schedule, a new job instance is created and executed, but the data (passed from JobDetail) will be same to each job instance.

Below is the complete Main class code

Main Code

import java.io.IOException;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzDemo2 {
    public static void main(String[] args) throws SchedulerException, IOException, InterruptedException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler schedular = schedulerFactory.getScheduler();
        JobBuilder jobBuilder = JobBuilder.newJob(HelloJob.class);
        jobBuilder.withDescription("Job Description").withIdentity("jobDetail1", "group1");
        jobBuilder.usingJobData("firstName", "Alice");
        jobBuilder.usingJobData("lastName", "Pierce");
        JobDetail jobDetail = jobBuilder.build();
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.repeatSecondlyForTotalCount(10);
        TriggerBuilder triggerBuilder = TriggerBuilder.newTrigger();
        triggerBuilder.withSchedule(simpleScheduleBuilder).withDescription("Trigger Description").withIdentity("trigger1", "group1").startNow();
        Trigger trigger = triggerBuilder.build();
        schedular.scheduleJob(jobDetail, trigger);


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.
Hello Alice Pierce Job is executing.
Hello Alice Pierce Job is executing.
Hello Alice Pierce Job is executing.
Hello Alice Pierce Job is executing.
Hello Alice Pierce Job is executing.
Hello Alice Pierce Job is executing.
Hello Alice Pierce Job is executing.
Hello Alice Pierce Job is executing.
Hello Alice Pierce Job is executing.
Hello Alice Pierce Job is executing.

Leave a Reply