溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何對(duì)xxl-job進(jìn)行simpleTrigger并動(dòng)態(tài)創(chuàng)建任務(wù)擴(kuò)展

發(fā)布時(shí)間:2021-11-24 14:11:55 來源:億速云 閱讀:286 作者:小新 欄目:軟件技術(shù)

這篇文章給大家分享的是有關(guān)如何對(duì)xxl-job進(jìn)行simpleTrigger并動(dòng)態(tài)創(chuàng)建任務(wù)擴(kuò)展的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

業(yè)務(wù)場(chǎng)景

需求上要求能實(shí)現(xiàn)quartz的simpleTrigger任務(wù),同時(shí)還需要?jiǎng)討B(tài)的創(chuàng)建任務(wù)而非在控制面板上創(chuàng)建,查閱xxl-job官方文檔發(fā)現(xiàn)simpelTrigger其暫時(shí)還躺在to do list,而動(dòng)態(tài)創(chuàng)建可以調(diào)用JobInfoController其add和start接口(但是有個(gè)弊端,start/stop/remove等方法都只要求傳送id,這點(diǎn)上可以自己擴(kuò)展接口實(shí)現(xiàn)),還需要放開登錄權(quán)限,接下來就老夫上去就上一梭子,開干。

job_info字段調(diào)整說明

CREATE TABLE `XXL_JOB_QRTZ_TRIGGER_INFO` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `job_group` int(11) NOT NULL COMMENT '執(zhí)行器主鍵ID',
  `job_cron` varchar(128) DEFAULT NULL COMMENT '任務(wù)執(zhí)行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 '報(bào)警郵件',
  `executor_route_strategy` varchar(50) DEFAULT NULL COMMENT '執(zhí)行器路由策略',
  `executor_handler` varchar(255) DEFAULT NULL COMMENT '執(zhí)行器任務(wù)handler',
  `executor_param` varchar(512) DEFAULT NULL COMMENT '執(zhí)行器任務(wù)參數(shù)',
  `executor_block_strategy` varchar(50) DEFAULT NULL COMMENT '阻塞處理策略',
  `executor_timeout` int(11) NOT NULL DEFAULT '0' COMMENT '任務(wù)執(zhí)行超時(shí)時(shí)間,單位秒',
  `executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT '失敗重試次數(shù)',
  `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更新時(shí)間',
  `child_jobid` varchar(255) DEFAULT NULL COMMENT '子任務(wù)ID,多個(gè)逗號(hào)分隔',
  `ext_reamrk` varchar(255) DEFAULT NULL,
  `job_start_time` bigint(20) DEFAULT NULL COMMENT 'trigger開始時(shí)間于當(dāng)前時(shí)間的時(shí)間間隔 單位毫秒\n',
  `job_end_time` datetime DEFAULT NULL COMMENT 'trigger結(jié)束時(shí)間',
  `job_repeat_count` bigint(7) DEFAULT '-1' COMMENT 'simple_trigger 執(zhí)行次數(shù) ',
  `job_repeat_interval` bigint(12) DEFAULT NULL COMMENT 'simple_trigger 時(shí)間間隔 單位毫秒',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
  • job_cron設(shè)為空,支持simpleTrigger;

  • ext_reamrk自定義擴(kuò)展字段,可以根據(jù)它來刷選job從而避免start/stop/remove接口只有id作為參數(shù);

  • job_repeat_count,simplerTrigger的執(zhí)行次數(shù),默認(rèn)-1即為無限次,0的話是執(zhí)行1次;

  • job_repeat_interval,simplerTrigger每次執(zhí)行的時(shí)間間隔,單位毫秒;

  • job_start_time,trigger開始時(shí)間和當(dāng)前時(shí)間的時(shí)間間隔,單位毫秒;

  • job_end_time,trigger結(jié)束時(shí)間

重寫啟動(dòng)任務(wù)

xxl-job在新建任務(wù)的時(shí)候只是把job數(shù)據(jù)保存到數(shù)據(jù)庫里,只有啟動(dòng)的時(shí)候才會(huì)創(chuàng)建jobDetailtrigger并由scheduler來啟動(dòng)job。

重寫XxlJobDynamicScheduleraddJob方法:根據(jù)jobInfo中的值構(gòu)建cron的trigger還是simple的trigger。

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();

        // 1、job key
        TriggerKey triggerKey = TriggerKey.triggerKey(jobName);
        JobKey jobKey = new JobKey(jobName);

        // 2、valid trigger
        if(scheduler.checkExists(triggerKey)) {
            return true;
        }

        // 3.1、corn trigger
        Trigger trigger = null;
        if (!StringUtils.isEmpty(cronExpression)) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing();   // withMisfireHandlingInstructionDoNothing 忽略掉調(diào)度終止過程中忽略的調(diào)度
            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.2、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.3、valid trigger
        if(Objects.isNull(trigger)) {
            return true;
        }

        // 4、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");
        }*/

        // 5、schedule job
        Date date = scheduler.scheduleJob(jobDetail, trigger);

        logger.info(">>>>>>>>>>> addJob success(quartz), jobDetail:{}, trigger:{}, date:{}", jobDetail, trigger, date);
        return true;
    }

感謝各位的閱讀!關(guān)于“如何對(duì)xxl-job進(jìn)行simpleTrigger并動(dòng)態(tài)創(chuàng)建任務(wù)擴(kuò)展”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI