Update an existing JobDataMap

南笙酒味 提交于 2019-12-02 20:51:07

See http://www.quartz-scheduler.org/docs/tutorial/TutorialLesson03.html:

A Job instance can be defined as "stateful" or "non-stateful". Non-stateful jobs only have their JobDataMap stored at the time they are added to the scheduler. This means that any changes made to the contents of the job data map during execution of the job will be lost, and will not seen by the job the next time it executes.

...a stateful job is just the opposite - its JobDataMap is re-stored after every execution of the job.

You 'mark' a Job as stateful by having it implement the StatefulJob interface, rather than the Job interface.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!