xxl-job分布式调度平台

1.任务调度?
某一时间段进行任务的操作
2.使用场景?
是为了定时发送报表?数据同步?推送?
3.作用,好处?
传统实现定时任务的方式?Thread、TimeTask、ScheduleExecutorService、Quartz 等;
这几种方式都是在单点系统使用,一旦Job服务器宕机之后,就必须采取一些措施;具体操作如下:
(1):使用心跳检测监控自动重启、任务补偿机制(任务做标记)
(2):定时任务在执行代码的时候中间突然报错,使用日志记录错误,跳过继续执行,在使用定时Job 扫描日志错误记录,进行补偿信息。
(3):定时Job 在执行的时候,导致整个 Job 异常结束掉,发送邮件通知给运维人员。
分布式定时任务的方式?XXL-Job、Elastic-job等。
既然采用分布式,那么肯定会遇到项目部署集群,导致任务重复执行多次;具体操作如下:
(1):Zookeeper 实现分布式锁,每次保证拿到锁再执行,效率比较低。
(2):配置文件中加入定时任务的开关,但是只能保证一台服务器执行,变为单击服务器。
(3):启动的时候使用数据库唯一标识;同样是效率低。
(4):分布式调度任务平台,解决了任务幂等问题,Job 负载均衡轮询机制(推荐)。
首先传统的定时任务,几乎无法做到高可用,再加上项目部署集群,会导致任务幂等性问题;
此时分布式定时任务调度平台便发挥了作用,本篇文章咱们拿 XXL-Job 来进行说明;相关作用如下:
(1):支持Job集群,Job 负载均衡轮询机制保证幂等性问题
(2):支持Job补偿,如果Job执行失败的话,会自动实现重试机制,超过重启次数后,会发送邮件通知运维人员。
(3):支持Job日志记录。
(4):动态配置定时规则,传统定时Job触发规则都是写死在代码中。

4.xxl-job

1.设计思路:
将调度行为抽象形成“调度中心”公共平台,而平台自身并不承担业务逻辑,“调度中心”负责发起调度请求;
将任务抽象成分散的JobHandler,交由“执行器”统一管理,“执行器”负责接收调度请求并执行对应的JobHandler中业务逻辑;
因此,“调度”和“任务”两部分可以相互解耦,提高系统整体稳定性和扩展性;
2.系统组成
调度中心:统一管理任务调度的平台,负责转发任务到对应的执行服务器。
执行器:负责接收调度请求并执行任务逻辑。任务模块专注于任务的执行等操作;
任务管理:执行服务器配置定时任务规则、路由策略、允许模式等。
3.工作原理
xxl-job原理图
1.任务执行器根据配置的调度中心的地址,自动注册到调度中心
2.达到任务触发条件,调度中心下发任务
3.执行器基于线程池执行任务,并把执行结果放入内存队列中、把执行日志写入日志3.文件中执行器的回调线程消费内存队列中的执行结果,主动上报给调度中心
4.当用户在调度中心查看任务日志,调度中心请求任务执行器,任务执行器读取任务日志文件并返回日志详情

1.自动注册

(1)任务执行器根据配置的调度中心的地址,注册到调度中心。
以下以Spring项目为例,注册到调度中心:

<!-- ********************************* 基础配置 ********************************* -->

<!-- 配置01、JobHandler 扫描路径 -->
<context:component-scan base-package="com.xxl.job.executor.service.jobhandler" />

<!-- 配置02、执行器 -->
<bean id="xxlJobSpringExecutor" class="com.xxl.job.core.executor.impl.XxlJobSpringExecutor" >
   <!-- 执行器注册中心地址[选填],为空则关闭自动注册 -->
   <property name="adminAddresses" value="${xxl.job.admin.addresses}" />
   <!-- 执行器AppName[选填],为空则关闭自动注册 -->
   <property name="appName" value="${xxl.job.executor.appname}" />
   <!-- 执行器IP[选填],为空则自动获取 -->
   <property name="ip" value="${xxl.job.executor.ip}" />
   <!-- 执行器端口号[选填],小于等于0则自动获取 -->
   <property name="port" value="${xxl.job.executor.port}" />
   <!-- 访问令牌[选填],非空则进行匹配校验 -->
   <property name="accessToken" value="${xxl.job.accessToken}" />
   <!-- 执行器日志路径[选填],为空则使用默认路径 -->
   <property name="logPath" value="${xxl.job.executor.logpath}" />
   <!-- 日志保存天数[选填],值大于3时生效 -->
   <property name="logRetentionDays" value="${xxl.job.executor.logretentiondays}" />
</bean>

如上所示,配置时需要我们将XxlJobSpringExecutor这个bean加入到spring容器中。这里要说一下自动注册和手动注册:
自动注册需要我们给XxlJobSpringExecutor的adminAddresses属性赋值。这个时候我们在调动中心配置执行器时就可选择自动注册,不需填写执行器地址;
手动注册则需要我们在调度中心配置执行器时手动的输入执行器地址。
下面可以看一下XxlJobSpringExecutor的实现:

public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationContextAware, InitializingBean, DisposableBean {


    // start
    @Override
    public void afterPropertiesSet() throws Exception {

        // init JobHandler Repository
        initJobHandlerRepository(applicationContext);

        // init JobHandler Repository (for method)
        initJobHandlerMethodRepository(applicationContext);

        // refresh GlueFactory
        GlueFactory.refreshInstance(1);

        // super start
        super.start();
    }

    // destroy
    @Override
    public void destroy() {
        super.destroy();
    }


    private void initJobHandlerRepository(ApplicationContext applicationContext) {
        if (applicationContext == null) {
            return;
        }

        // init job handler action
        Map<String, Object> serviceBeanMap = applicationContext.getBeansWithAnnotation(JobHandler.class);

        if (serviceBeanMap != null && serviceBeanMap.size() > 0) {
            for (Object serviceBean : serviceBeanMap.values()) {
                if (serviceBean instanceof IJobHandler) {
                    String name = serviceBean.getClass().getAnnotation(JobHandler.class).value();
                    IJobHandler handler = (IJobHandler) serviceBean;
                    if (loadJobHandler(name) != null) {
                        throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
                    }
                    registJobHandler(name, handler);
                }
            }
        }
    }

    private void initJobHandlerMethodRepository(ApplicationContext applicationContext) {
        if (applicationContext == null) {
            return;
        }

        // init job handler from method
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            Object bean = applicationContext.getBean(beanDefinitionName);
            Method[] methods = bean.getClass().getDeclaredMethods();
            for (Method method: methods) {
                XxlJob xxlJob = AnnotationUtils.findAnnotation(method, XxlJob.class);
                if (xxlJob != null) {

                    // name
                    String name = xxlJob.value();
                    if (name.trim().length() == 0) {
                        throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + bean.getClass() + "#"+ method.getName() +"] .");
                    }
                    if (loadJobHandler(name) != null) {
                        throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
                    }

                    // execute method
                    if (!(method.getParameterTypes()!=null && method.getParameterTypes().length==1 && method.getParameterTypes()[0].isAssignableFrom(String.class))) {
                        throw new RuntimeException("xxl-job method-jobhandler param-classtype invalid, for[" + bean.getClass() + "#"+ method.getName() +"] , " +
                                "The correct method format like \" public ReturnT<String> execute(String param) \" .");
                    }
                    if (!method.getReturnType().isAssignableFrom(ReturnT.class)) {
                        throw new RuntimeException("xxl-job method-jobhandler return-classtype invalid, for[" + bean.getClass() + "#"+ method.getName() +"] , " +
                                "The correct method format like \" public ReturnT<String> execute(String param) \" .");
                    }
                    method.setAccessible(true);

                    // init and destory
                    Method initMethod = null;
                    Method destroyMethod = null;

                    if(xxlJob.init().trim().length() > 0) {
                        try {
                            initMethod = bean.getClass().getDeclaredMethod(xxlJob.init());
                            initMethod.setAccessible(true);
                        } catch (NoSuchMethodException e) {
                            throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + bean.getClass() + "#"+ method.getName() +"] .");
                        }
                    }
                    if(xxlJob.destroy().trim().length() > 0) {
                        try {
                            destroyMethod = bean.getClass().getDeclaredMethod(xxlJob.destroy());
                            destroyMethod.setAccessible(true);
                        } catch (NoSuchMethodException e) {
                            throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + bean.getClass() + "#"+ method.getName() +"] .");
                        }
                    }

                    // registry jobhandler
                    registJobHandler(name, new MethodJobHandler(bean, method, initMethod, destroyMethod));
                }
            }
        }
    }

    // ---------------------- applicationContext ----------------------
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

}

总的来说就是启动后初始化方法,遍历所有方法查找@XxlJob 实例化XxlJob放到map中保存。
后创建一个MethodJobHandler的对象,调用XxlJobExecutor的registJobHandler方法 将对象放入jobHandlerRepository这个map中,最终调用到XxlRpcProviderFactory类中的start方法创建 NettyServer,设置启动停止方法等等。
(2)调度中心响应执行器自动注册请求
这一过程是调度器的JobApiController的api()方法去响应的:

@RequestMapping("/registry")
@ResponseBody
@PermissionLimit(limit=false)
public ReturnT<String> registry(HttpServletRequest request, @RequestBody(required = false) String data) {
    // valid
    validAccessToken(request);

    // param
    RegistryParam registryParam = (RegistryParam) parseParam(data, RegistryParam.class);

    // invoke
    return adminBiz.registry(registryParam);
}

而调度中心是启用ConcurrentHashMap来完成执行器的自动发现和注册的:

// ---------------------- executor-client ----------------------
private static ConcurrentMap<String, ExecutorBiz> executorBizRepository = new ConcurrentHashMap<String, ExecutorBiz>();
public static ExecutorBiz getExecutorBiz(String address) throws Exception {
    // valid
    if (address==null || address.trim().length()==0) {
        return null;
    }

    // load-cache
    address = address.trim();
    ExecutorBiz executorBiz = executorBizRepository.get(address);
    if (executorBiz != null) {
        return executorBiz;
    }

    // set-cache
    XxlRpcReferenceBean referenceBean = new XxlRpcReferenceBean();
    referenceBean.setClient(NettyHttpClient.class);
    referenceBean.setSerializer(HessianSerializer.class);
    referenceBean.setCallType(CallType.SYNC);
    referenceBean.setLoadBalance(LoadBalance.ROUND);
    referenceBean.setIface(ExecutorBiz.class);
    referenceBean.setVersion(null);
    referenceBean.setTimeout(3000);
    referenceBean.setAddress(address);
    referenceBean.setAccessToken(XxlJobAdminConfig.getAdminConfig().getAccessToken());
    referenceBean.setInvokeCallback(null);
    referenceBean.setInvokerFactory(null);

    executorBiz = (ExecutorBiz) referenceBean.getObject();

    executorBizRepository.put(address, executorBiz);
    return executorBiz;
}
任务下发

参考博客:xxl-job任务定时触发流程

执行结果异步上报

在执行器开始的时候,会开启一个回调线程,TriggerCallbackThread.getInstance().start();
该线程不断查找从 回调队列callBackQueue中查找回调信息,用就进行回调doCallback(callbackParamList);
这一段的代码在AdminBizImpl.callback中:

@Override
public ReturnT<String> callback(List<HandleCallbackParam> callbackParamList) {
    for (HandleCallbackParam handleCallbackParam: callbackParamList) {
        ReturnT<String> callbackResult = callback(handleCallbackParam);
        logger.debug(">>>>>>>>> JobApiController.callback {}, handleCallbackParam={}, callbackResult={}",
                (callbackResult.getCode()==IJobHandler.SUCCESS.getCode()?"success":"fail"), handleCallbackParam, callbackResult);
    }

    return ReturnT.SUCCESS;
}

private ReturnT<String> callback(HandleCallbackParam handleCallbackParam) {
    // valid log item
    XxlJobLog log = xxlJobLogDao.load(handleCallbackParam.getLogId());
    if (log == null) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, "log item not found.");
    }
    if (log.getHandleCode() > 0) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, "log repeate callback.");     // avoid repeat callback, trigger child job etc
    }

    // trigger success, to trigger child job
    String callbackMsg = null;
    if (IJobHandler.SUCCESS.getCode() == handleCallbackParam.getExecuteResult().getCode()) {
        XxlJobInfo xxlJobInfo = xxlJobInfoDao.loadById(log.getJobId());
        if (xxlJobInfo!=null && xxlJobInfo.getChildJobId()!=null && xxlJobInfo.getChildJobId().trim().length()>0) {
            callbackMsg = "<br><br><span style=\"color:#00c0ef;\" > >>>>>>>>>>>"+ I18nUtil.getString("jobconf_trigger_child_run") +"<<<<<<<<<<< </span><br>";

            String[] childJobIds = xxlJobInfo.getChildJobId().split(",");
            for (int i = 0; i < childJobIds.length; i++) {
                int childJobId = (childJobIds[i]!=null && childJobIds[i].trim().length()>0 && isNumeric(childJobIds[i]))?Integer.valueOf(childJobIds[i]):-1;
                if (childJobId > 0) {

                    JobTriggerPoolHelper.trigger(childJobId, TriggerTypeEnum.PARENT, -1, null, null, null);
                    ReturnT<String> triggerChildResult = ReturnT.SUCCESS;

                    // add msg
                    callbackMsg += MessageFormat.format(I18nUtil.getString("jobconf_callback_child_msg1"),
                            (i+1),
                            childJobIds.length,
                            childJobIds[i],
                            (triggerChildResult.getCode()==ReturnT.SUCCESS_CODE?I18nUtil.getString("system_success"):I18nUtil.getString("system_fail")),
                            triggerChildResult.getMsg());
                } else {
                    callbackMsg += MessageFormat.format(I18nUtil.getString("jobconf_callback_child_msg2"),
                            (i+1),
                            childJobIds.length,
                            childJobIds[i]);
                }
            }

        }
    }

    // handle msg
    StringBuffer handleMsg = new StringBuffer();
    if (log.getHandleMsg()!=null) {
        handleMsg.append(log.getHandleMsg()).append("<br>");
    }
    if (handleCallbackParam.getExecuteResult().getMsg() != null) {
        handleMsg.append(handleCallbackParam.getExecuteResult().getMsg());
    }
    if (callbackMsg != null) {
        handleMsg.append(callbackMsg);
    }

    if (handleMsg.length() > 15000) {
        handleMsg = new StringBuffer(handleMsg.substring(0, 15000));  // text最大64kb 避免长度过长
    }

    // success, save log
    log.setHandleTime(new Date());
    log.setHandleCode(handleCallbackParam.getExecuteResult().getCode());
    log.setHandleMsg(handleMsg.toString());
    xxlJobLogDao.updateHandleInfo(log);

    return ReturnT.SUCCESS;
}
日志查询并回调
调度中心和执行器之间的通信

a.在调度中心XxlJobScheduler首先生成代理对象,构建执行器:

// ---------------------- executor-client ----------------------
private static ConcurrentMap<String, ExecutorBiz> executorBizRepository = new ConcurrentHashMap<String, ExecutorBiz>();
public static ExecutorBiz getExecutorBiz(String address) throws Exception {
    // valid
    if (address==null || address.trim().length()==0) {
        return null;
    }

    // load-cache
    address = address.trim();
    ExecutorBiz executorBiz = executorBizRepository.get(address);
    if (executorBiz != null) {
        return executorBiz;
    }

    // set-cache
    XxlRpcReferenceBean referenceBean = new XxlRpcReferenceBean();
    referenceBean.setClient(NettyHttpClient.class);
    referenceBean.setSerializer(HessianSerializer.class);
    referenceBean.setCallType(CallType.SYNC);
    referenceBean.setLoadBalance(LoadBalance.ROUND);
    referenceBean.setIface(ExecutorBiz.class);
    referenceBean.setVersion(null);
    referenceBean.setTimeout(3000);
    referenceBean.setAddress(address);
    referenceBean.setAccessToken(XxlJobAdminConfig.getAdminConfig().getAccessToken());
    referenceBean.setInvokeCallback(null);
    referenceBean.setInvokerFactory(null);
//通过代理类创建代理对象,这一步是关键
    executorBiz = (ExecutorBiz) referenceBean.getObject();

    executorBizRepository.put(address, executorBiz);
    return executorBiz;
}

b.看看他的getObject()方法:

public Object getObject() throws Exception {
    this.initClient();
    return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{this.iface}, new InvocationHandler() {
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            String className = method.getDeclaringClass().getName();
            String varsion_ = XxlRpcReferenceBean.this.version;
            String methodName = method.getName();
            Class<?>[] parameterTypes = method.getParameterTypes();
            Object[] parameters = args;
            if (className.equals(XxlRpcGenericService.class.getName()) && methodName.equals("invoke")) {
                Class<?>[] paramTypes = null;
                if (args[3] != null) {
                    String[] paramTypes_str = (String[])((String[])args[3]);
                    if (paramTypes_str.length > 0) {
                        paramTypes = new Class[paramTypes_str.length];

                        for(int i = 0; i < paramTypes_str.length; ++i) {
                            paramTypes[i] = ClassUtil.resolveClass(paramTypes_str[i]);
                        }
                    }
                }

                className = (String)args[0];
                varsion_ = (String)args[1];
                methodName = (String)args[2];
                parameterTypes = paramTypes;
                parameters = (Object[])((Object[])args[4]);
            }

            if (className.equals(Object.class.getName())) {
                XxlRpcReferenceBean.logger.info(">>>>>>>>>>> xxl-rpc proxy class-method not support [{}#{}]", className, methodName);
                throw new XxlRpcException("xxl-rpc proxy class-method not support");
            } else {
                String finalAddress = XxlRpcReferenceBean.this.address;
                if ((finalAddress == null || finalAddress.trim().length() == 0) && XxlRpcReferenceBean.this.invokerFactory != null && XxlRpcReferenceBean.this.invokerFactory.getServiceRegistry() != null) {
                    String serviceKey = XxlRpcProviderFactory.makeServiceKey(className, varsion_);
                    TreeSet<String> addressSet = XxlRpcReferenceBean.this.invokerFactory.getServiceRegistry().discovery(serviceKey);
                    if (addressSet != null && addressSet.size() != 0) {
                        if (addressSet.size() == 1) {
                            finalAddress = (String)addressSet.first();
                        } else {
                            finalAddress = XxlRpcReferenceBean.this.loadBalance.xxlRpcInvokerRouter.route(serviceKey, addressSet);
                        }
                    }
                }
		//这里封装任务的信息,发送给远程服务器
                if (finalAddress != null && finalAddress.trim().length() != 0) {
                    XxlRpcRequest xxlRpcRequest = new XxlRpcRequest();
                    xxlRpcRequest.setRequestId(UUID.randomUUID().toString());
                    xxlRpcRequest.setCreateMillisTime(System.currentTimeMillis());
                    xxlRpcRequest.setAccessToken(XxlRpcReferenceBean.this.accessToken);
                    xxlRpcRequest.setClassName(className);
                    xxlRpcRequest.setMethodName(methodName);
                    xxlRpcRequest.setParameterTypes(parameterTypes);
                    xxlRpcRequest.setParameters(parameters);
                    xxlRpcRequest.setVersion(XxlRpcReferenceBean.this.version);
                    XxlRpcFutureResponse futureResponse;
                    if (CallType.SYNC == XxlRpcReferenceBean.this.callType) {
                        futureResponse = new XxlRpcFutureResponse(XxlRpcReferenceBean.this.invokerFactory, xxlRpcRequest, (XxlRpcInvokeCallback)null);

                        Object var31;
                        try {
                            XxlRpcReferenceBean.this.clientInstance.asyncSend(finalAddress, xxlRpcRequest);
                            XxlRpcResponse xxlRpcResponse = futureResponse.get(XxlRpcReferenceBean.this.timeout, TimeUnit.MILLISECONDS);
                            if (xxlRpcResponse.getErrorMsg() != null) {
                                throw new XxlRpcException(xxlRpcResponse.getErrorMsg());
                            }

                            var31 = xxlRpcResponse.getResult();
                        } catch (Exception var21) {
                            XxlRpcReferenceBean.logger.info(">>>>>>>>>>> xxl-rpc, invoke error, address:{}, XxlRpcRequest{}", finalAddress, xxlRpcRequest);
                            throw (Throwable)(var21 instanceof XxlRpcException ? var21 : new XxlRpcException(var21));
                        } finally {
                            futureResponse.removeInvokerFuture();
                        }

                        return var31;
                    } else if (CallType.FUTURE == XxlRpcReferenceBean.this.callType) {
                        futureResponse = new XxlRpcFutureResponse(XxlRpcReferenceBean.this.invokerFactory, xxlRpcRequest, (XxlRpcInvokeCallback)null);

                        try {
                            XxlRpcInvokeFuture invokeFuture = new XxlRpcInvokeFuture(futureResponse);
                            XxlRpcInvokeFuture.setFuture(invokeFuture);
                            XxlRpcReferenceBean.this.clientInstance.asyncSend(finalAddress, xxlRpcRequest);
                            return null;
                        } catch (Exception var20) {
                            XxlRpcReferenceBean.logger.info(">>>>>>>>>>> xxl-rpc, invoke error, address:{}, XxlRpcRequest{}", finalAddress, xxlRpcRequest);
                            futureResponse.removeInvokerFuture();
                            throw (Throwable)(var20 instanceof XxlRpcException ? var20 : new XxlRpcException(var20));
                        }
                    } else if (CallType.CALLBACK == XxlRpcReferenceBean.this.callType) {
                        XxlRpcInvokeCallback finalInvokeCallback = XxlRpcReferenceBean.this.invokeCallback;
                        XxlRpcInvokeCallback threadInvokeCallback = XxlRpcInvokeCallback.getCallback();
                        if (threadInvokeCallback != null) {
                            finalInvokeCallback = threadInvokeCallback;
                        }

                        if (finalInvokeCallback == null) {
                            throw new XxlRpcException("xxl-rpc XxlRpcInvokeCallback(CallType=" + CallType.CALLBACK.name() + ") cannot be null.");
                        } else {
                            XxlRpcFutureResponse futureResponsex = new XxlRpcFutureResponse(XxlRpcReferenceBean.this.invokerFactory, xxlRpcRequest, finalInvokeCallback);

                            try {
                                XxlRpcReferenceBean.this.clientInstance.asyncSend(finalAddress, xxlRpcRequest);
                                return null;
                            } catch (Exception var19) {
                                XxlRpcReferenceBean.logger.info(">>>>>>>>>>> xxl-rpc, invoke error, address:{}, XxlRpcRequest{}", finalAddress, xxlRpcRequest);
                                futureResponsex.removeInvokerFuture();
                                throw (Throwable)(var19 instanceof XxlRpcException ? var19 : new XxlRpcException(var19));
                            }
                        }
                    } else if (CallType.ONEWAY == XxlRpcReferenceBean.this.callType) {
                        XxlRpcReferenceBean.this.clientInstance.asyncSend(finalAddress, xxlRpcRequest);
                        return null;
                    } else {
                        throw new XxlRpcException("xxl-rpc callType[" + XxlRpcReferenceBean.this.callType + "] invalid");
                    }
                } else {
                    throw new XxlRpcException("xxl-rpc reference bean[" + className + "] address empty");
                }
            }
        }
    });
}

从上面可以清晰的看到,生成的代理对象里面的 InvocationHandler 中的invoke方法,并没有真实的执行方法,而是将类名,方法名,参数等信息发送给远程服务器。
c.下面可以看一下,远程服务器拿到请求之后如何处理的。

@Override
public ReturnT<String> run(TriggerParam triggerParam) {
    // load old:jobHandler + jobThread
    JobThread jobThread = XxlJobExecutor.loadJobThread(triggerParam.getJobId());
    IJobHandler jobHandler = jobThread!=null?jobThread.getHandler():null;
    String removeOldReason = null;

    // valid:jobHandler + jobThread
    GlueTypeEnum glueTypeEnum = GlueTypeEnum.match(triggerParam.getGlueType());
    if (GlueTypeEnum.BEAN == glueTypeEnum) {

        // new jobhandler
        IJobHandler newJobHandler = XxlJobExecutor.loadJobHandler(triggerParam.getExecutorHandler());

        // valid old jobThread
        if (jobThread!=null && jobHandler != newJobHandler) {
            // change handler, need kill old thread
            removeOldReason = "change jobhandler or glue type, and terminate the old job thread.";

            jobThread = null;
            jobHandler = null;
        }

        // valid handler
        if (jobHandler == null) {
            jobHandler = newJobHandler;
            if (jobHandler == null) {
                return new ReturnT<String>(ReturnT.FAIL_CODE, "job handler [" + triggerParam.getExecutorHandler() + "] not found.");
            }
        }

    } else if (GlueTypeEnum.GLUE_GROOVY == glueTypeEnum) {

        // valid old jobThread
        if (jobThread != null &&
                !(jobThread.getHandler() instanceof GlueJobHandler
                    && ((GlueJobHandler) jobThread.getHandler()).getGlueUpdatetime()==triggerParam.getGlueUpdatetime() )) {
            // change handler or gluesource updated, need kill old thread
            removeOldReason = "change job source or glue type, and terminate the old job thread.";

            jobThread = null;
            jobHandler = null;
        }

        // valid handler
        if (jobHandler == null) {
            try {
                IJobHandler originJobHandler = GlueFactory.getInstance().loadNewInstance(triggerParam.getGlueSource());
                jobHandler = new GlueJobHandler(originJobHandler, triggerParam.getGlueUpdatetime());
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                return new ReturnT<String>(ReturnT.FAIL_CODE, e.getMessage());
            }
        }
    } else if (glueTypeEnum!=null && glueTypeEnum.isScript()) {

        // valid old jobThread
        if (jobThread != null &&
                !(jobThread.getHandler() instanceof ScriptJobHandler
                        && ((ScriptJobHandler) jobThread.getHandler()).getGlueUpdatetime()==triggerParam.getGlueUpdatetime() )) {
            // change script or gluesource updated, need kill old thread
            removeOldReason = "change job source or glue type, and terminate the old job thread.";

            jobThread = null;
            jobHandler = null;
        }

        // valid handler
        if (jobHandler == null) {
            jobHandler = new ScriptJobHandler(triggerParam.getJobId(), triggerParam.getGlueUpdatetime(), triggerParam.getGlueSource(), GlueTypeEnum.match(triggerParam.getGlueType()));
        }
    } else {
        return new ReturnT<String>(ReturnT.FAIL_CODE, "glueType[" + triggerParam.getGlueType() + "] is not valid.");
    }

    // executor block strategy
    if (jobThread != null) {
        ExecutorBlockStrategyEnum blockStrategy = ExecutorBlockStrategyEnum.match(triggerParam.getExecutorBlockStrategy(), null);
        if (ExecutorBlockStrategyEnum.DISCARD_LATER == blockStrategy) {
            // discard when running
            if (jobThread.isRunningOrHasQueue()) {
                return new ReturnT<String>(ReturnT.FAIL_CODE, "block strategy effect:"+ExecutorBlockStrategyEnum.DISCARD_LATER.getTitle());
            }
        } else if (ExecutorBlockStrategyEnum.COVER_EARLY == blockStrategy) {
            // kill running jobThread
            if (jobThread.isRunningOrHasQueue()) {
                removeOldReason = "block strategy effect:" + ExecutorBlockStrategyEnum.COVER_EARLY.getTitle();

                jobThread = null;
            }
        } else {
            // just queue trigger
        }
    }

    // replace thread (new or exists invalid)重新实例化jobThread开启线程
    if (jobThread == null) {
        jobThread = XxlJobExecutor.registJobThread(triggerParam.getJobId(), jobHandler, removeOldReason);
    }

    // push data to queue  将调度中心传来的参数入队列
    ReturnT<String> pushResult = jobThread.pushTriggerQueue(triggerParam);
    return pushResult;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值