java - How do you update a value in a Quartz JobDataMap? -
i'm using quartz-scheduler 1.8.5. i've created job implementing statefuljob. schedule job using simpletrigger , stdschedulerfactory.
it seems have update trigger's jobdatamap in addition jobdetail's jobdatamap in order change jobdatamap inside job. i'm trying understand why it's necessary update both? noticed jobdatamap set dirty. maybe have explicitly save or something?
i'm thinking i'll have dig source code of quartz understand going on here, figured i'd lazy , ask first. insight inner workings of jobdatamap!
here's 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"); //int count3 = context.gettrigger().getjobdatamap().getint("count"); system.err.println("hellojob executing. count: '"+count+"', "+count2+"'"); //the count gets updated if updated both trigger , // jobdetail datamaps. if update jobdetail, doesn't persist. context.gettrigger().getjobdatamap().put("count", count++); context.getjobdetail().getjobdatamap().put("count", count++); //this has no effect inside job, works outside job try { context.getscheduler().addjob(context.getjobdetail(), true); } catch (schedulerexception e) { // todo auto-generated catch block e.printstacktrace(); } //these don't seem persist between jobs //context.put("count", count++); //context.getmergedjobdatamap().put("count", count++); } }
here's how i'm scheduling job:
try { // define job , tie our hellojob class jobdetail job = new jobdetail(job_name, job_group_name, hellojob.class); job.getjobdatamap().put("count", 1); // trigger job run now, , every trigger trigger = new simpletrigger("mytrigger", "group1", simpletrigger.repeat_indefinitely, howoften); // tell quartz schedule job using our trigger sched.schedulejob(job, trigger); return job; } catch (schedulerexception e) { throw e; }
update:
seems have put value jobdetail's jobdatamap twice persist, 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 executing. count: '"+count+"', , job stateful? "+context.getjobdetail().isstateful()); context.getjobdetail().getjobdatamap().put("count", count++); context.getjobdetail().getjobdatamap().put("count", count++); } }
this seems bug, maybe? or maybe there's step i'm missing tell jobdetail flush contents of jobdatamap jobstore?
i think problem using postfix ++ operator - when do:
context.getjobdetail().getjobdatamap().put("count", count++);
you're setting value in map count , incrementing count.
to me looks wanted:
context.getjobdetail().getjobdatamap().put("count", ++count);
which need done once.
Comments
Post a Comment