需求背景:创建的quartz任务需要用到三个参数,根据参数去执行并将结果入库,并且参数可修改更新。
这里,有三个参数需要传入,分别是planId、target、officeId
定义Job类:
public class StatisticsTaskService implements Job { private Logger logger = LoggerFactory.getLogger(StatisticsTaskService.class); private StatusStatisticsService statusStatisticsService = ApplicationContextHelper.getBean(StatusStatisticsService.class); @Override public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap dataMap = context.getJobDetail().getJobDataMap(); JobParameter jobParam = (JobParameter)dataMap.get(JobParameter.JOB_PARAM); String planId = jobParam.getPlanId(); String target = jobParam.getTarget(); String officeId = jobParam.getOfficeId(); //System.out.println(jobParam.toString()); //根据传入的target参数执行不同操作 if(target.equals("1")){ //执行操作1 }else if(target.equals("2")){ //执行操作2 }else{ //执行其他操作 } logger.info("Job:"+planId+" target:"+target); } }
定义参数类:
public class JobParameter implements Serializable{ private static final long serialVersionUID = 1L; public static final String JOB_PARAM = "jobParam"; private String planId; private String target; private String officeId; public String getPlanId() { return planId; } public void setPlanId(String planId) { this.planId = planId; } public String getTarget() { return target; } public void setTarget(String target) { this.target = target; } public String getOfficeId() { return officeId; } public void setOfficeId(String officeId) { this.officeId = officeId; } @Override public String toString() { return "JobParameter [planId=" + planId + ", target=" + target + ", officeId=" + officeId + "]"; } }
创建任务创建和修改方法:
1.创建任务:
public static void createScheduleJobWithParams(Scheduler scheduler, String jobName, String jobGroup, String cronExpression, Class<? extends Job> jobClass,
String planId,String target,String officeId) { // 构建job信息 JobDetail jobDetail = JobBuilder.newJob(jobClass) .withIdentity(jobName, jobGroup) .build();
//创建初始参数 JobParameter jobParam = new JobParameter(); jobParam.setPlanId(planId); jobParam.setTarget(target); jobParam.setOfficeId(officeId); jobDetail.getJobDataMap().put(JobParameter.JOB_PARAM, jobParam); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder .cronSchedule(cronExpression); CronTrigger trigger = TriggerBuilder.newTrigger() .withIdentity(jobName, jobGroup).withSchedule(scheduleBuilder) .build(); try { scheduler.scheduleJob(jobDetail, trigger); if (!scheduler.isStarted()) { scheduler.start(); } } catch (SchedulerException e) { e.printStackTrace(); } }
2.修改任务:
public static void updateScheduleJobWithParams(Scheduler scheduler, String jobName, String jobGroup, String cronExpression,
String planId,String target) { try { TriggerKey triggerKey = getTriggerKey(jobName, jobGroup); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); // 按新的cronExpression表达式重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); //参数修改 JobKey jobKey = getJobKey(jobName, jobGroup); JobParameter jobParam = (JobParameter) scheduler.getJobDetail(jobKey).getJobDataMap().get(JobParameter.JOB_PARAM); jobParam.setPlanId(planId); jobParam.setTarget(target); // 按新的trigger重新设置job执行 scheduler.rescheduleJob(triggerKey, trigger); } catch (SchedulerException e) { e.printStackTrace(); } }
编码总结:
最开始时我将三个参数直接put进JobDetail的JobDataMap,发现任务中能够正常读取,但是在更新参数是发现set后值不变,不知是不是JobDataMap继承DirtyFlagMap的原因,没有深究。
后将三个参数封装为对象,并实现序列化,以对象形式传入,修改就没有问题了。