Quartz JobDataMap Example Part 2

In the previous post under Quartz Scheduler, I explained what JobDataMap is about and I also mentioned that JobDataMap can be passed to an instance of Job through JobDetail and Trigger.

In the previous post I explained with an example on how to pass JobDataMap to an instance of Job through JobDetail.

In this post I will explain how to pass JobDataMap to an instance of Job through Trigger.

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


1 TriggerBuilder triggerBuilder = TriggerBuilder.newTrigger();
2 triggerBuilder.usingJobData("firstName", "Alice");
3 triggerBuilder.usingJobData("lastName", "Pierce");
4 triggerBuilder.withSchedule(simpleScheduleBuilder).withDescription("Trigger Description").withIdentity("trigger1", "group1").startNow();
5         
6 Trigger trigger = triggerBuilder.build();

Again we take the help of TriggerBuilder class

We put the data by calling the method “usingJobData”. Refer to line 2 and 3. 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;
5  
6  public class HelloJob1 implements Job {
7   private String firstName;
8   private String lastName;
9   
10     @Override
11     public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
12      JobDataMap jobDataMap = jobExecutionContext.getTrigger().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.

Below is the 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 QuartzDemo3 {
    public static void main(String[] args) throws SchedulerException, IOException, InterruptedException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler schedular = schedulerFactory.getScheduler();
        
        JobBuilder jobBuilder = JobBuilder.newJob(HelloJob1.class);
        jobBuilder.withDescription("Job Description").withIdentity("jobDetail1", "group1");
        
        JobDetail jobDetail = jobBuilder.build();
        
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.repeatSecondlyForTotalCount(10);
        
        TriggerBuilder triggerBuilder = TriggerBuilder.newTrigger();
        triggerBuilder.usingJobData("firstName", "Alice");
        triggerBuilder.usingJobData("lastName", "Pierce");
        triggerBuilder.withSchedule(simpleScheduleBuilder).withDescription("Trigger Description").withIdentity("trigger1", "group1").startNow();
        
        Trigger trigger = triggerBuilder.build();
        
        schedular.scheduleJob(jobDetail, trigger);
        
        schedular.start();
        
        Thread.sleep(60000);
        
        schedular.shutdown(true);
    }
}

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.
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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s