xxl-job任务执行过程中的admin端和executor端

xxl-job任务执行过程

一,admin
1.JobInfoController
2.JobTriggerPoolHelper
执行JobTriggerPoolHelper.trigger,实际上是调用了JobTriggerPoolHelper.addTrigger方法。
/**	 
	* 触发任务	
	* 
	* @param jobId	
	*     调度任务信息XxlJobInfo表主键	
	* @param triggerType	
	*            触发类型	
	* @param failRetryCount	   
	*	>=0: 用这个值 <0: 用XxlJobInfo中配置好的重试次数	
	* @param executorShardingParam	 
	*            执行分片的参数	
	* @param executorParam	
	*            执行参数:
	*            null: 用XxlJobInfo中配置好的参数 ;not null: 使用这个参数(覆盖XxlJobInfo中配置好的参数)	 
*/
    public void addTrigger(final int jobId, final TriggerTypeEnum triggerType, final int failRetryCount, final String executorShardingParam, final String executorParam) {

        // choose thread pool
        //这里区分了快慢线程池,1分钟内超过500毫秒的请求大于10次,放入慢线程池处理
        ThreadPoolExecutor triggerPool_ = fastTriggerPool;
        AtomicInteger jobTimeoutCount = jobTimeoutCountMap.get(98);
        if (jobTimeoutCount!=null && jobTimeoutCount.get() > 10) {      // job-timeout 10 times in 1 min
            triggerPool_ = slowTriggerPool;
        }

        // trigger
        triggerPool_.execute(new Runnable() {
            @Override
            public void run() {

                long start = System.currentTimeMillis();

                try {
                    // do trigger 触发执行任务
                    XxlJobTrigger.trigger(jobId, triggerType, failRetryCount, executorShardingParam, executorParam);
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                } finally {

                    // check timeout-count-map
                    long minTim_now = System.currentTimeMillis()/60000;
                    if (minTim != minTim_now) {
                        minTim = minTim_now;
                        jobTimeoutCountMap.clear();
                    }

                    // incr timeout-count-map
                    long cost = System.currentTimeMillis()-start;
                    if (cost > 500) {       // ob-timeout threshold 500ms
                        AtomicInteger timeoutCount = jobTimeoutCountMap.putIfAbsent(jobId, new AtomicInteger(1));
                        if (timeoutCount != null) {
                            timeoutCount.incrementAndGet();
                        }
                    }

                }

            }
        });
    }
3.XxlJobTrigger的trigger方法
public static void trigger(int jobId, TriggerTypeEnum triggerType, int failRetryCount, String executorShardingParam, String executorParam) {
        // load data
        //通过JobId从数据库中查询该任务的具体信息
        XxlJobInfo jobInfo = XxlJobAdminConfig.getAdminConfig().getXxlJobInfoDao().loadById(jobId);
        if (jobInfo == null) {
            logger.warn(">>>>>>>>>>>> trigger fail, jobId invalid,jobId={}", jobId);
            return;
        }
        if (executorParam != null) {
            jobInfo.setExecutorParam(executorParam);
        }
        // 设置失败重试次数
        int finalFailRetryCount = failRetryCount>=0?failRetryCount:jobInfo.getExecutorFailRetryCount();
        //获取该类型的执行器信息
        XxlJobGroup group = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().load(jobInfo.getJobGroup());

        // sharding param
        //分片信息
        int[] shardingParam = null;
        if (executorShardingParam!=null){
            String[] shardingArr = executorShardingParam.split("/");
            if (shardingArr.length==2 && isNumeric(shardingArr[0]) && isNumeric(shardingArr[1])) {
                shardingParam = new int[2];
                shardingParam[0] = Integer.valueOf(shardingArr[0]);
                shardingParam[1] = Integer.valueOf(shardingArr[1]);
            }
        }
        //广播模式,循环执行器配置的服务地址列表
        if (ExecutorRouteStrategyEnum.SHARDING_BROADCAST==ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null)
                && group.getRegistryList()!=null && !group.getRegistryList().isEmpty()
                && shardingParam==null) {
            for (int i = 0; i < group.getRegistryList().size(); i++) {
                processTrigger(group, jobInfo, finalFailRetryCount, triggerType, i, group.getRegistryList().size());
            }
        } else {
            if (shardingParam == null) {
                shardingParam = new int[]{0, 1};
            }
            //非广播模式进入
            processTrigger(group, jobInfo, finalFailRetryCount, triggerType, shardingParam[0], shardingParam[1]);
        }

    }
4.XxlJobTrigger的processTrigger
选择路由和阻塞策略,保存日志信息,初始化任务参数,通过runExecutor方法调用执行器远程执行,修改日志信息并更新。
5.XxlJobTrigger的runExecutor
public static ReturnT<String> runExecutor(TriggerParam triggerParam, String address){
        ReturnT<String> runResult = null;
        try {
            //创建一个executorBiz对象(XxlRpcReferenceBean),执行器的rpc客户端对象,准备发送请求
            ExecutorBiz executorBiz = XxlJobScheduler.getExecutorBiz(address);
            //每个Job对应一个线程,线程有任务参数队列,任务线程去轮询这个任务  JobThread.run()
            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("
address:"
).append(address); runResultSB.append("
code:"
).append(runResult.getCode()); runResultSB.append("
msg:"
).append(runResult.getMsg()); runResult.setMsg(runResultSB.toString()); return runResult; }
二,executor
1.XxlJobExecutor的start和XxlJobExecutor.start
//public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationContextAware, InitializingBean, DisposableBean     
	@Override
    public void afterPropertiesSet() throws Exception {
		// 获取类上有@JobHandler注解的bean,放到jobHandlerRepository变量map中
        initJobHandlerRepository(applicationContext);
        // 将方法上有@XxlJob注解的方法,打包成MethodJobHandler类,加入到jobHandlerRepository变量map中
        initJobHandlerMethodRepository(applicationContext);
		// 初始化GlueFactory=new SpringGlueFactory()
        GlueFactory.refreshInstance(1);
        // super start
        super.start();
    }
public void start() throws Exception {
        // 初始化日志路径logpath
        XxlJobFileAppender.initLogPath(logPath);
        // 初始化调度中心客户端对象 AdminBizClient,加入到List adminBizList变量中
        initAdminBizList(adminAddresses, accessToken);
        // 启动日志清理线程,定时清理时间范围内的日志文件
        JobLogFileCleanThread.getInstance().start(logRetentionDays);
        // 启动一个线程,消费callBackQueue队列,遍历adminBizList调用adminBiz.callback(callbackParamList)方法
        TriggerCallbackThread.getInstance().start();
        // 初始化监听服务
        port = port>0?port: NetUtil.findAvailablePort(9999);
        ip = (ip!=null&&ip.trim().length()>0)?ip: IpUtil.getIp();
        initRpcProvider(ip, port, appName, accessToken);
    }

你可能感兴趣的:(java,quartz)