Monday 29 July 2019

Quartz: JobDataMap: Pass information to a job, trigger


All the trigger, job data implementation classes provides setJobDataMap methods, it is used to pass information to triggers, JobDetail objects.

Example1: Pass jobDataMap to printJobDetails
JobDetailImpl printJobDetails = new JobDetailImpl();
printJobDetails.setJobClass(PrintJob.class);
printJobDetails.setName("PrintJob");
printJobDetails.setGroup("PrintMessages");

JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("Description", "This job prints some message to console");
printJobDetails.setJobDataMap(jobDataMap);

Example 2: Pass jobDataMap to trigger
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();

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.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.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.JobDetailImpl;
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();

  // Creating JobDetailImpl and link to our Job class
  JobDetailImpl printJobDetails = new JobDetailImpl();
  printJobDetails.setJobClass(PrintJob.class);
  printJobDetails.setName("PrintJob");
  printJobDetails.setGroup("PrintMessages");
  
  JobDataMap jobDataMap = new JobDataMap();
  jobDataMap.put("Description", "This job prints some message to console");
  printJobDetails.setJobDataMap(jobDataMap);
  
  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

.....
.....
.....






Previous                                                    Next                                                    Home

No comments:

Post a Comment