I am using quartz-scheduler 1.8.5. I created a Job running StatefulJob. I plan to work with SimpleTrigger and StdSchedulerFactory.
It seems that I should update the Trigger JobDataMap in addition to the JobDetail JobDataMap in order to change the JobDataMap inside the job. I'm trying to understand why it is necessary to update both? I noticed that JobDataMap is configured for pollution. Maybe I should explicitly save it or something else?
I think I will have to delve into the Quartz source code to really understand what is going on here, but I thought I would be lazy and ask first. Thank you for understanding the inner workings of JobDataMap!
Here is my job:
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");
This is how I plan the work:
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; }
Update:
It seems that I have to add the value twice to the JobDetail JobDataMap so that it is saved, 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++); } }
It seems like a mistake, maybe? Or maybe there is some step that I am missing to tell JobDetail to clear the contents of my JobDataMap to the JobStore?