Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz Scheduler - Updating only the JobDataMap, between jobs

I have a Quartz Job that I can schedule with some Cron Trigger.

ReportSchedule reportSchedule = ... // my object
JobDetail jobDetail = new JobDetail(reportSchedule.getScheduleName(), 
                                    reportSchedule.getScheduleGroup(),
                                    ExtendedReportJob.class /* my job */);

jobDetail.getJobDataMap().put("reportSchedule", reportSchedule);
jobDetail.setDescription(reportSchedule.getScheduleDescription());
CronTrigger trigger = ...; // depends on the report schedule 

scheduler.scheduleJob(jobDetail, trigger); 

This code successfully writes the job and details to a database.

The reportSchedule object contains specific parameters that are required for the job. However, I may want to change the parameters.

I can do this with

scheduler.deleteJob(name, group);
scheduler.scheduleJob(jobDetail, trigger); 
// where jobDetail.getJobDataMap() has the updated reportSchedule

Doing this, however, will trigger the job right away since the trigger depends on the report schedule and I don't want to change it (I want to keep original date). So my question: Is there any way to modify the JobDetail or JobDataMap between jobs without changing the Trigger?

I'm using Quartz 1.6.0.

like image 299
Sotirios Delimanolis Avatar asked Dec 05 '22 11:12

Sotirios Delimanolis


1 Answers

The solution is simple enough, just have to know the API.

The Scheduler class has the following method

Scheduler#addJob(JobDetail, boolean);

In which the passed JobDetail will overwrite the previous one if the boolean argument is set to true.

So

// name and group are the primary key of the job detail
final JobDetail jobDetail = new JobDetail(name, group, ExtendedReportJob.class);

// reportSchedule is the object I've previously modified
jobDetail.getJobDataMap().put(ORStatics.REPORT_SCHEDULE, reportSchedule);
jobDetail.setDescription(reportSchedule.getScheduleDescription());

// overwrite the previous job, however retaining the triggers       
scheduler.addJob(jobDetail, true);

will update the job detail in persistent storage. Since the primary key for the table containing the JobDetail will remain the same, we don't need to change the triggers. They will still execute it as scheduled.

like image 109
Sotirios Delimanolis Avatar answered Jan 01 '23 04:01

Sotirios Delimanolis