博文原址:對xxl-job進行simpleTrigger並動態建立任務擴展java
需求上要求能實現quartz的simpleTrigger任務,同時還須要動態的建立任務而非在控制面板上建立,查閱xxl-job官方文檔發現simpelTrigger其暫時還躺在to do list,而動態建立能夠調用
JobInfoController
其add和start接口(可是有個弊端,start/stop/remove等方法都只要求傳送id,這點上能夠本身擴展接口實現),還須要放開登陸權限,接下來就老夫上去就上一梭子,開幹。git
CREATE TABLE `XXL_JOB_QRTZ_TRIGGER_INFO` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`job_group` int(11) NOT NULL COMMENT '執行器主鍵ID',
`job_cron` varchar(128) DEFAULT NULL COMMENT '任務執行CRON',
`job_desc` varchar(255) NOT NULL,
`add_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`author` varchar(64) DEFAULT NULL COMMENT '做者',
`alarm_email` varchar(255) DEFAULT NULL COMMENT '報警郵件',
`executor_route_strategy` varchar(50) DEFAULT NULL COMMENT '執行器路由策略',
`executor_handler` varchar(255) DEFAULT NULL COMMENT '執行器任務handler',
`executor_param` varchar(512) DEFAULT NULL COMMENT '執行器任務參數',
`executor_block_strategy` varchar(50) DEFAULT NULL COMMENT '阻塞處理策略',
`executor_timeout` int(11) NOT NULL DEFAULT '0' COMMENT '任務執行超時時間,單位秒',
`executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT '失敗重試次數',
`glue_type` varchar(50) NOT NULL COMMENT 'GLUE類型',
`glue_source` mediumtext COMMENT 'GLUE源代碼',
`glue_remark` varchar(128) DEFAULT NULL COMMENT 'GLUE備註',
`glue_updatetime` datetime DEFAULT NULL COMMENT 'GLUE更新時間',
`child_jobid` varchar(255) DEFAULT NULL COMMENT '子任務ID,多個逗號分隔',
`ext_reamrk` varchar(255) DEFAULT NULL,
`job_start_time` bigint(20) DEFAULT NULL COMMENT 'trigger開始時間於當前時間的時間間隔 單位毫秒\n',
`job_end_time` datetime DEFAULT NULL COMMENT 'trigger結束時間',
`job_repeat_count` bigint(7) DEFAULT '-1' COMMENT 'simple_trigger 執行次數 ',
`job_repeat_interval` bigint(12) DEFAULT NULL COMMENT 'simple_trigger 時間間隔 單位毫秒',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
複製代碼
xxl-job在新建任務的時候只是把job數據保存到數據庫裏,只有啓動的時候纔會建立jobDetail
和trigger
並由scheduler
來啓動job。github
重寫XxlJobDynamicScheduler
的addJob
方法:根據jobInfo中的值構建cron的trigger仍是simple的trigger。sql
public static boolean addJob(XxlJobInfo xxlJobInfo) throws SchedulerException {
String jobName = String.valueOf(xxlJobInfo.getId());
String cronExpression = xxlJobInfo.getJobCron();
Integer jobRepeatCount = xxlJobInfo.getJobRepeatCount();
Long jobRepeatInterval = xxlJobInfo.getJobRepeatInterval();
// 一、job key
TriggerKey triggerKey = TriggerKey.triggerKey(jobName);
JobKey jobKey = new JobKey(jobName);
// 二、valid trigger
if(scheduler.checkExists(triggerKey)) {
return true;
}
// 3.一、corn trigger
Trigger trigger = null;
if (!StringUtils.isEmpty(cronExpression)) {
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing(); // withMisfireHandlingInstructionDoNothing 忽略掉調度終止過程當中忽略的調度
TriggerBuilder<CronTrigger> builder = TriggerBuilder.newTrigger().withIdentity(triggerKey)
.withSchedule(cronScheduleBuilder)
.endAt(xxlJobInfo.getJobEndTime());
Optional.ofNullable(xxlJobInfo.getJobStartTime())
.ifPresent(start -> builder.startAt(new Date(System.currentTimeMillis() + start)));
trigger = builder.build();
} else
// 3.二、simple trigger
if (Objects.nonNull(jobRepeatInterval) && Objects.nonNull(jobRepeatCount)) {
SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMilliseconds(jobRepeatInterval)
.withRepeatCount(jobRepeatCount);
TriggerBuilder<SimpleTrigger> builder = TriggerBuilder.newTrigger().withIdentity(triggerKey)
.withSchedule(simpleScheduleBuilder)
.endAt(xxlJobInfo.getJobEndTime());
Optional.ofNullable(xxlJobInfo.getJobStartTime())
.ifPresent(start -> builder.startAt(new Date(System.currentTimeMillis() + start)));
trigger = builder.build();
}
// 3.三、valid trigger
if(Objects.isNull(trigger)) {
return true;
}
// 四、job detail
Class<? extends Job> jobClass_ = RemoteHttpJobBean.class; // Class.forName(jobInfo.getJobClass());
JobDetail jobDetail = JobBuilder.newJob(jobClass_).withIdentity(jobKey).build();
/*if (jobInfo.getJobData()!=null) { JobDataMap jobDataMap = jobDetail.getJobDataMap(); jobDataMap.putAll(JacksonUtil.readValue(jobInfo.getJobData(), Map.class)); // JobExecutionContext context.getMergedJobDataMap().get("mailGuid"); }*/
// 五、schedule job
Date date = scheduler.scheduleJob(jobDetail, trigger);
logger.info(">>>>>>>>>>> addJob success(quartz), jobDetail:{}, trigger:{}, date:{}", jobDetail, trigger, date);
return true;
}
複製代碼
能夠看到將數據庫裏3任務改成間隔3秒,job_repeat_count爲1,任務實際會執行2次。數據庫
關注瞭解最新動態更新微信