Skip to content

Commit

Permalink
任务Cron更新逻辑优化,改为rescheduleJob,同时防止cron重复设置;
Browse files Browse the repository at this point in the history
  • Loading branch information
xueli.xue committed May 24, 2017
1 parent 54d92de commit 25cf93f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,9 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
- 5、执行参数编辑时丢失问题修复;
- 6、新增任务测试Demo,方便在开发时进行任务逻辑测试;
#### 6.16 版本 V1.8.0 特性[Coding]
- 1、任务Cron更新逻辑优化,改为rescheduleJob,同时防止cron重复设置;
#### TODO LIST
- 1、任务权限管理:执行器为粒度分配权限,核心操作校验权限;
- 2、任务分片:一个任务被拆分成N个独立的任务单元,然后由分布式部署的执行器分别执行某一个或几个分片单元;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,40 @@ public static boolean rescheduleJob(String jobGroup, String jobName, String cron

// TriggerKey : name + group
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
JobKey jobKey = new JobKey(jobName, jobGroup);

// CronTrigger : TriggerKey + cronExpression
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing();
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build();

//scheduler.rescheduleJob(triggerKey, cronTrigger);

// JobDetail-JobDataMap fresh
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
/*JobDataMap jobDataMap = jobDetail.getJobDataMap();
jobDataMap.clear();
jobDataMap.putAll(JacksonUtil.readValue(jobInfo.getJobData(), Map.class));*/

// Trigger fresh
HashSet<Trigger> triggerSet = new HashSet<Trigger>();
triggerSet.add(cronTrigger);

scheduler.scheduleJob(jobDetail, triggerSet, true);
CronTrigger oldTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);

if (oldTrigger != null) {
// avoid repeat
String oldCron = oldTrigger.getCronExpression();
if (oldCron.equals(cronExpression)){
return true;
}

// CronTrigger : TriggerKey + cronExpression
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing();
oldTrigger = oldTrigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build();

// rescheduleJob
scheduler.rescheduleJob(triggerKey, oldTrigger);
} else {
// CronTrigger : TriggerKey + cronExpression
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing();
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build();

// JobDetail-JobDataMap fresh
JobKey jobKey = new JobKey(jobName, jobGroup);
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
/*JobDataMap jobDataMap = jobDetail.getJobDataMap();
jobDataMap.clear();
jobDataMap.putAll(JacksonUtil.readValue(jobInfo.getJobData(), Map.class));*/

// Trigger fresh
HashSet<Trigger> triggerSet = new HashSet<Trigger>();
triggerSet.add(cronTrigger);

scheduler.scheduleJob(jobDetail, triggerSet, true);
}

logger.info(">>>>>>>>>>> resumeJob success, JobGroup:{}, JobName:{}", jobGroup, jobName);
return true;
}
Expand Down

0 comments on commit 25cf93f

Please sign in to comment.