【发布时间】:2011-04-29 17:45:11
【问题描述】:
我正在使用石英调度程序 1.8.5。我创建了一个实现 StatefulJob 的作业。我使用 SimpleTrigger 和 StdSchedulerFactory 安排作业。
似乎除了 JobDetail 的 JobDataMap 之外,我还必须更新 Trigger 的 JobDataMap 才能从 Job 内部更改 JobDataMap。我试图理解为什么有必要同时更新两者?我注意到 JobDataMap 设置为脏。也许我必须明确保存它或其他什么?
我想我必须深入研究 Quartz 的源代码才能真正了解这里发生了什么,但我想我会偷懒并先问。感谢您深入了解 JobDataMap 的内部工作原理!
这是我的工作:
public class HelloJob implements StatefulJob {
public HelloJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
int count = context.getMergedJobDataMap().getInt("count");
int count2 = context.getJobDetail().getJobDataMap().getInt("count");
//int count3 = context.getTrigger().getJobDataMap().getInt("count");
System.err.println("HelloJob is executing. Count: '"+count+"', "+count2+"'");
//The count only gets updated if I updated both the Trigger and
// JobDetail DataMaps. If I only update the JobDetail, it doesn't persist.
context.getTrigger().getJobDataMap().put("count", count++);
context.getJobDetail().getJobDataMap().put("count", count++);
//This has no effect inside the job, but it works outside the job
try {
context.getScheduler().addJob(context.getJobDetail(), true);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//These don't seem to persist between jobs
//context.put("count", count++);
//context.getMergedJobDataMap().put("count", count++);
}
}
我是这样安排工作的:
try {
// define the job and tie it to our HelloJob class
JobDetail job = new JobDetail(JOB_NAME, JOB_GROUP_NAME,
HelloJob.class);
job.getJobDataMap().put("count", 1);
// Trigger the job to run now, and every so often
Trigger trigger = new SimpleTrigger("myTrigger", "group1",
SimpleTrigger.REPEAT_INDEFINITELY, howOften);
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);
return job;
} catch (SchedulerException e) {
throw e;
}
更新:
似乎我必须将值放入 JobDetail 的 JobDataMap 两次才能使其持久化,这是可行的:
public class HelloJob implements StatefulJob {
public HelloJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
int count = (Integer) context.getMergedJobDataMap().get("count");
System.err.println("HelloJob is executing. Count: '"+count+"', and is the job stateful? "+context.getJobDetail().isStateful());
context.getJobDetail().getJobDataMap().put("count", count++);
context.getJobDetail().getJobDataMap().put("count", count++);
}
}
这似乎是一个错误,也许?或者也许我缺少一个步骤来告诉 JobDetail 将其 JobDataMap 的内容刷新到 JobStore?
【问题讨论】:
标签: java quartz-scheduler