xxl-job源码2-调度端

2019-03-21  本文已影响0人  modou1618

一 服务端主体流程

服务端流程

二 表

2.1 XxlJobRegistry

public class XxlJobInfo {
    
    private int id;             // 主键ID     (JobKey.name)
    
    private int jobGroup;       // 执行器主键ID  (JobKey.group)
    private String jobCron;     // 任务执行CRON表达式 【base on quartz】
    private String jobDesc;
    
    private Date addTime;
    private Date updateTime;
    
    private String author;      // 负责人
    private String alarmEmail;  // 报警邮件

    private String executorRouteStrategy;   // 执行器路由策略
    private String executorHandler;         // 执行器,任务Handler名称
    private String executorParam;           // 执行器,任务参数
    private String executorBlockStrategy;   // 阻塞处理策略
    private int executorTimeout;            // 任务执行超时时间,单位秒
    private int executorFailRetryCount;     // 失败重试次数
    
    private String glueType;        // GLUE类型   #com.xxl.job.core.glue.GlueTypeEnum
    private String glueSource;      // GLUE源代码
    private String glueRemark;      // GLUE备注
    private Date glueUpdatetime;    // GLUE更新时间

    private String childJobId;      // 子任务ID,多个逗号分隔
    
    // copy from quartz
    private String jobStatus;       // 任务状态 【base on quartz】
}
public class XxlJobLog {
    
    private int id;
    
    // job info
    private int jobGroup;//执行器主键id
    private int jobId;

    // execute info
    private String executorAddress;//执行器地址
    private String executorHandler;//执行器任务执行函数
    private String executorParam;//参数
    private String executorShardingParam;//分片参数
    private int executorFailRetryCount;//失败重试次数
    
    // trigger info
    private Date triggerTime;//触发时间
    private int triggerCode;//触发结果
    private String triggerMsg;
    
    // handle info
    private Date handleTime;//处理完成时间
    private int handleCode;//处理结果
    private String handleMsg;
}
public class XxlJobGroup {

    private int id;
    private String appName;
    private String title;
    private int order;
    private int addressType;    // 执行器地址类型:0=自动注册、1=手动录入
    private String addressList;    // 执行器地址列表,多地址逗号分隔(手动录入)
}

三 任务触发

protected void executeInternal(JobExecutionContext context)
        throws JobExecutionException {

    // load jobId
    JobKey jobKey = context.getTrigger().getJobKey();
    Integer jobId = Integer.valueOf(jobKey.getName());

    // trigger
    JobTriggerPoolHelper.trigger(jobId, TriggerTypeEnum.CRON, -1, null, null);
}
public static void trigger(int jobId, TriggerTypeEnum triggerType, int failRetryCount, String executorShardingParam, String executorParam) {
    helper.addTrigger(jobId, triggerType, failRetryCount, executorShardingParam, executorParam);
}

public void addTrigger(final int jobId, final TriggerTypeEnum triggerType, final int failRetryCount, final String executorShardingParam, final String executorParam) {
    triggerPool.execute(new Runnable() {
        @Override
        public void run() {
            XxlJobTrigger.trigger(jobId, triggerType, failRetryCount, executorShardingParam, executorParam);
        }
    });
}
TriggerParam triggerParam = new TriggerParam();
//任务id
triggerParam.setJobId(jobInfo.getId());
//任务处理函数,参数
triggerParam.setExecutorHandler(jobInfo.getExecutorHandler());
triggerParam.setExecutorParams(jobInfo.getExecutorParam());
//阻塞策略
triggerParam.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
//任务执行超时时间配置
triggerParam.setExecutorTimeout(jobInfo.getExecutorTimeout());
//任务触发记录id
triggerParam.setLogId(jobLog.getId());
triggerParam.setLogDateTim(jobLog.getTriggerTime().getTime());
//任务执行函数源码信息
triggerParam.setGlueType(jobInfo.getGlueType());
triggerParam.setGlueSource(jobInfo.getGlueSource());
triggerParam.setGlueUpdatetime(jobInfo.getGlueUpdatetime().getTime());
//分片信息
triggerParam.setBroadcastIndex(index);
triggerParam.setBroadcastTotal(total);
public static ReturnT<String> runExecutor(TriggerParam triggerParam, String address){
    ReturnT<String> runResult = null;
    try {
        ExecutorBiz executorBiz = XxlJobDynamicScheduler.getExecutorBiz(address);
        runResult = executorBiz.run(triggerParam);
    } catch (Exception e) {
        logger.error(">>>>>>>>>>> xxl-job trigger error, please check if the executor[{}] is running.", address, e);
        runResult = new ReturnT<String>(ReturnT.FAIL_CODE, ThrowableUtil.toString(e));
    }

    StringBuffer runResultSB = new StringBuffer(I18nUtil.getString("jobconf_trigger_run") + ":");
    runResultSB.append("<br>address:").append(address);
    runResultSB.append("<br>code:").append(runResult.getCode());
    runResultSB.append("<br>msg:").append(runResult.getMsg());

    runResult.setMsg(runResultSB.toString());
    return runResult;
}

四 定时任务

上一篇下一篇

猜你喜欢

热点阅读