Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update an existing JobDataMap

Tags:

I have a Quartz job that has already been scheduled. I want to update the JobDataMap associated with it. If I get a JobDataMap with JobDataMap jobDataMap = scheduler.getJobDetail(....).getJobDataMap(), is that map "live"? ie. if I change it, will it be persisted in the scheduler? If not, how do I persist it?

like image 679
Paul Tomblin Avatar asked May 13 '10 19:05

Paul Tomblin


People also ask

How do I update my job in quartz?

Delete existing job first using scheduler. deleteJob(jobKey(name,group)). Then Schedule new job using scheduler. scheduleJob(jobDetail,trigger,true).

What is JobDataMap in quartz?

Holds state information for Job instances. JobDataMap instances are stored once when the Job is added to a scheduler. They are also re-persisted after every execution of StatefulJob instances. JobDataMap instances can also be stored with a Trigger .


Video Answer


2 Answers

In quartz 2.0. StatefulJob is deprecated. In order to persist the job data map, use @PersistJobDataAfterExecution on the job class. It usually goes with @DisallowConcurrentExecution.

like image 58
Bozho Avatar answered Sep 19 '22 14:09

Bozho


I had a similar problem: I have a secondly trigger which fires a stateful job that works on a queue in the job's data map. Every time the job fires, it polls from the queue and performs some work on the polled element. With each job execution, the queue has one less element (the queue is updated correctly from within the job). When the queue is empty, the job unschedules itself.

I wanted to be able to externally update the list of arguments of an ongoing job/trigger to provide more arguments to the queue. However, just retrieving the data map and updating the queue was not enough (the following execution shows the queue is not updated). The problem is that Quartz only updates the job data map of a job instance after execution.

Here's the solution I found:

JobDetail jobDetail = scheduler.getJobDetail("myJob", "myGroup");
jobDetail.getJobDataMap.put("jobQueue", updatedQueue);
scheduler.addJob(jobDetail, true);

The last line instructs Quartz to replace the stored job with the one you are providing. The next time the job is fired it will see the updated queue.

like image 31
Leo Avatar answered Sep 21 '22 14:09

Leo