简体   繁体   English

Quartz Scheduler - 在作业之间仅更新JobDataMap

[英]Quartz Scheduler - Updating only the JobDataMap, between jobs

I have a Quartz Job that I can schedule with some Cron Trigger. 我有一个Quartz Job,我可以安排一些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. reportSchedule对象包含作业所需的特定参数。 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 ? 所以我的问题是:有没有办法在不更改Trigger情况下修改作业之间的JobDetailJobDataMap

I'm using Quartz 1.6.0. 我正在使用Quartz 1.6.0。

The solution is simple enough, just have to know the API. 解决方案很简单,只需要知道API。

The Scheduler class has the following method Scheduler类具有以下方法

Scheduler#addJob(JobDetail, boolean);

In which the passed JobDetail will overwrite the previous one if the boolean argument is set to true . 如果boolean参数设置为true ,则传递的JobDetail将覆盖前一个JobDetail

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. 由于包含JobDetail的表的主键将保持不变,因此我们无需更改触发器。 They will still execute it as scheduled. 他们仍将按计划执行。

What about getting the trigger with getTrigger(String triggerName, String triggerGroup) and store it in a variable. 如何使用getTrigger(String triggerName, String triggerGroup)获取触发器并将其存储在变量中。 Then create a new job with your new jobDataMap and use the old trigger? 然后使用新的jobDataMap创建一个新作业并使用旧的触发器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM