如何更新Quartz JobDataMap中的值?

Upg*_*ave 5 java quartz-scheduler

我正在使用quartz-scheduler 1.8.5.我创建了一个实现StatefulJob的Job.我使用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++);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我安排工作的方式:

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;
}
Run Code Online (Sandbox Code Playgroud)

更新:

似乎我必须将值放入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++);
    }
}
Run Code Online (Sandbox Code Playgroud)

这似乎是个bug,也许吧?或者也许我缺少一个步骤告诉JobDetail将其JobDataMap的内容刷新到JobStore?

小智 15

我认为你的问题是使用postfix ++运算符 - 当你这样做:

context.getJobDetail().getJobDataMap().put("count", count++);  
Run Code Online (Sandbox Code Playgroud)

您要将地图中的值设置为count,然后设置递增计数.

对我而言,看起来你想要:

context.getJobDetail().getJobDataMap().put("count", ++count);  
Run Code Online (Sandbox Code Playgroud)

这只需要做一次.