JobBuilder也可向任务传递数据,通常情况下,我们使用它就可向任务类发送数据了,如有特别复杂的传递参数,它提供了一个传递:JobDataMap对象的方法,当然这个要根据个人使用习惯来说了呀
它重载了方法usingJobData(),如下图:
JobDetail job = newJob(StatefulDumbJob.class)
.withIdentity("statefulJob1", "group1")
.usingJobData(StatefulDumbJob.EXECUTION_DELAY, 10000L)//传递数据
.build();
SimpleTrigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startAt(startTime)
.withSchedule(simpleSchedule()
.withIntervalInSeconds(3)
.repeatForever())
.build();
Date ft = sched.scheduleJob(job, trigger);
log.error("任务key: "+job.getKey() + ",执行运行时间: " + sdf.format(ft) +
",触发器重复执行次数: " + trigger.getRepeatCount() +
",触发器执行时间: " + trigger.getRepeatInterval() / 1000 + " seconds");
在任务类一样可取出:
JobDataMap map = context.getJobDetail().getJobDataMap();
int executeCount = 0;
if (map.containsKey(NUM_EXECUTIONS)) {
executeCount = map.getInt(NUM_EXECUTIONS);
}
executeCount++;
map.put(NUM_EXECUTIONS, executeCount);