如何更新 Quartz JobDataMap 中的值? [英] How do you update a value in a Quartz JobDataMap?

查看:33
本文介绍了如何更新 Quartz JobDataMap 中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是quartz-scheduler 1.8.5.我创建了一个实现 StatefulJob 的 Job.我使用 SimpleTrigger 和 StdSchedulerFactory 安排作业.

I'm using quartz-scheduler 1.8.5. I've created a Job implementing StatefulJob. I schedule the job using a SimpleTrigger and StdSchedulerFactory.

似乎除了 JobDetail 的 JobDataMap 之外,我还必须更新 Trigger 的 JobDataMap,以便从 Job 内部更改 JobDataMap.我试图理解为什么有必要同时更新两者?我注意到 JobDataMap 设置为脏.也许我必须明确保存它或其他什么?

It seems that I have to update the Trigger's JobDataMap in addition to the JobDetail's JobDataMap in order to change the JobDataMap from inside the Job. I'm trying to understand why it's necessary to update both? I noticed that the JobDataMap is set to dirty. Maybe I have to explicitly save it or something?

我想我必须深入研究 Quartz 的源代码才能真正理解这里发生了什么,但我想我会偷懒先问一下.感谢您深入了解 JobDataMap 的内部工作原理!

I'm thinking I'll have to dig into the source code of Quartz to really understand what is going on here, but I figured I'd be lazy and ask first. Thanks for any insight into the inner workings of 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++);
    }
}

这是我安排工作的方式:

Here's how I'm scheduling the job:

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 两次才能使其持久化,这有效:

Seems that I have to put the value into the JobDetail's JobDataMap twice to get it to persist, this works:

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?

This seems like a bug, maybe? Or maybe there's a step I'm missing to tell the JobDetail to flush the contents of its JobDataMap to the JobStore?

推荐答案

我认为您的问题在于使用 postfix ++ 运算符 - 当您这样做时:

I think your problem is with using the postfix ++ operator - when you do:

context.getJobDetail().getJobDataMap().put("count", count++);  

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

you're setting the value in the map to count and THEN incrementing count.

对我来说它看起来像你想要的:

To me it looks like you wanted:

context.getJobDetail().getJobDataMap().put("count", ++count);  

这只需要做一次.

这篇关于如何更新 Quartz JobDataMap 中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆