本文概览:介绍相关功能的代码实现
1 一个任务触发的入口
目前有三种方式:手动执行一次、任务触发、父任务自动触发。
1 2 3 4 5 6 7 |
public enum TriggerTypeEnum { MANUAL(I18nUtil.getString("jobconf_trigger_type_manual")), CRON(I18nUtil.getString("jobconf_trigger_type_cron")), RETRY(I18nUtil.getString("jobconf_trigger_type_retry")), PARENT(I18nUtil.getString("jobconf_trigger_type_parent")), API(I18nUtil.getString("jobconf_trigger_type_api")); } |
1、方式 1 手动执行触发
1 2 3 4 5 6 7 8 9 10 11 12 |
@RequestMapping("/trigger") @ResponseBody //@PermissionLimit(limit = false) public ReturnT<String> triggerJob(int id, String executorParam) { // force cover job param if (executorParam == null) { executorParam = ""; } JobTriggerPoolHelper.trigger(id, TriggerTypeEnum.MANUAL, -1, null, executorParam); return ReturnT.SUCCESS; } |
2、方式2 定时任务cron触发
3、方式3 父子任务触发
4、共同点
都是通过如下函数进行触发
1 2 3 4 5 6 7 8 9 10 11 12 13 |
### 类名称 ### XxlJobTrigger ### 类名称 ### public static void trigger(int jobId, TriggerTypeEnum triggerType, int failRetryCount, String executorShardingParam, String executorParam) { // load data XxlJobInfo jobInfo = XxlJobAdminConfig.getAdminConfig().getXxlJobInfoDao().loadById(jobId); if (jobInfo == null) { logger.warn(">>>>>>>>>>>> trigger fail, jobId invalid,jobId={}", jobId); return; } ...... } |
2 报警处理
JobFailMonitorHelper.failAlarm
3 路由策略
各种路由策略的实现
1 2 3 4 5 6 7 8 9 10 11 12 |
public enum ExecutorRouteStrategyEnum { FIRST(I18nUtil.getString("jobconf_route_first"), new ExecutorRouteFirst()), LAST(I18nUtil.getString("jobconf_route_last"), new ExecutorRouteLast()), ROUND(I18nUtil.getString("jobconf_route_round"), new ExecutorRouteRound()), RANDOM(I18nUtil.getString("jobconf_route_random"), new ExecutorRouteRandom()), CONSISTENT_HASH(I18nUtil.getString("jobconf_route_consistenthash"), new ExecutorRouteConsistentHash()), LEAST_FREQUENTLY_USED(I18nUtil.getString("jobconf_route_lfu"), new ExecutorRouteLFU()), LEAST_RECENTLY_USED(I18nUtil.getString("jobconf_route_lru"), new ExecutorRouteLRU()), FAILOVER(I18nUtil.getString("jobconf_route_failover"), new ExecutorRouteFailover()), BUSYOVER(I18nUtil.getString("jobconf_route_busyover"), new ExecutorRouteBusyover()), SHARDING_BROADCAST(I18nUtil.getString("jobconf_route_shard"), null); |