java 更新现有的 JobDataMap
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2829731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update an existing JobDataMap
提问by Paul Tomblin
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?
我有一个已经安排好的 Quartz 工作。我想更新与之关联的 JobDataMap。如果我得到一个带有 的 JobDataMap JobDataMap jobDataMap = scheduler.getJobDetail(....).getJobDataMap(),那张地图是“实时”的吗?IE。如果我更改它,它会保留在调度程序中吗?如果没有,我该如何坚持?
采纳答案by Tommi
See http://www.quartz-scheduler.org/docs/tutorial/TutorialLesson03.html:
请参阅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.
Job 实例可以定义为“有状态”或“无状态”。无状态作业仅在将它们添加到调度程序时存储它们的 JobDataMap。这意味着在作业执行期间对作业数据映射的内容所做的任何更改都将丢失,并且作业下次执行时将看不到。
...有状态的作业正好相反 - 它的 JobDataMap 在每次执行作业后重新存储。
您可以通过让 Job 实现 StatefulJob 接口而不是 Job 接口来将其“标记”为有状态的。
回答by Bozho
In quartz 2.0. StatefulJobis deprecated. In order to persist the job data map, use @PersistJobDataAfterExecutionon the job class. It usually goes with @DisallowConcurrentExecution.
在石英 2.0 中。StatefulJob已弃用。为了持久化作业数据映射,请@PersistJobDataAfterExecution在作业类上使用。它通常与@DisallowConcurrentExecution.
回答by Leo
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.
我希望能够从外部更新正在进行的作业/触发器的参数列表,以向队列提供更多参数。但是,仅仅检索数据映射并更新队列是不够的(以下执行显示队列未更新)。问题是 Quartz 只在执行后更新作业实例的作业数据映射。
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.
最后一行指示 Quartz 将存储的作业替换为您提供的作业。下次触发作业时,它将看到更新的队列。

