Search in sources :

Example 1 with IJobHandler

use of com.xxl.job.core.handler.IJobHandler in project xxl-job by xuxueli.

the class ExecutorBizImpl method run.

@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 = "更换JobHandler或更换任务模式,终止旧任务线程";
            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 = "更新任务逻辑或更换任务模式,终止旧任务线程";
            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 = "更新任务逻辑或更换任务模式,终止旧任务线程";
            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, "阻塞处理策略-生效:" + ExecutorBlockStrategyEnum.DISCARD_LATER.getTitle());
            }
        } else if (ExecutorBlockStrategyEnum.COVER_EARLY == blockStrategy) {
            // kill running jobThread
            if (jobThread.isRunningOrHasQueue()) {
                removeOldReason = "阻塞处理策略-生效:" + ExecutorBlockStrategyEnum.COVER_EARLY.getTitle();
                jobThread = null;
            }
        } else {
        // just queue trigger
        }
    }
    // replace thread (new or exists invalid)
    if (jobThread == null) {
        jobThread = XxlJobExecutor.registJobThread(triggerParam.getJobId(), jobHandler, removeOldReason);
    }
    // push data to queue
    ReturnT<String> pushResult = jobThread.pushTriggerQueue(triggerParam);
    return pushResult;
}
Also used : GlueJobHandler(com.xxl.job.core.handler.impl.GlueJobHandler) IJobHandler(com.xxl.job.core.handler.IJobHandler) JobThread(com.xxl.job.core.thread.JobThread) GlueTypeEnum(com.xxl.job.core.glue.GlueTypeEnum) ScriptJobHandler(com.xxl.job.core.handler.impl.ScriptJobHandler) ReturnT(com.xxl.job.core.biz.model.ReturnT) ExecutorBlockStrategyEnum(com.xxl.job.core.enums.ExecutorBlockStrategyEnum)

Example 2 with IJobHandler

use of com.xxl.job.core.handler.IJobHandler in project xxl-job by xuxueli.

the class XxlJobExecutor method initJobHandlerRepository.

private static 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 naming conflicts.");
                }
                registJobHandler(name, handler);
            }
        }
    }
}
Also used : IJobHandler(com.xxl.job.core.handler.IJobHandler) JobHandler(com.xxl.job.core.handler.annotation.JobHandler) IJobHandler(com.xxl.job.core.handler.IJobHandler)

Example 3 with IJobHandler

use of com.xxl.job.core.handler.IJobHandler in project xxl-job by xuxueli.

the class NutzSetup method init.

@Override
public void init(NutConfig cfg) {
    // regist JobHandler
    String[] beanNames = cfg.getIoc().getNamesByType(IJobHandler.class);
    if (beanNames == null || beanNames.length == 0) {
        return;
    }
    for (String beanName : beanNames) {
        IJobHandler jobHandler = cfg.getIoc().get(IJobHandler.class, beanName);
        String name = jobHandler.getClass().getAnnotation(JobHandler.class).value();
        XxlJobExecutor.registJobHandler(name, jobHandler);
    }
    // load executor prop
    PropertiesProxy xxlJobProp = new PropertiesProxy("xxl-job-executor.properties");
    // init executor
    xxlJobExecutor = new XxlJobExecutor();
    xxlJobExecutor.setAdminAddresses(xxlJobProp.get("xxl.job.admin.addresses"));
    xxlJobExecutor.setAppName(xxlJobProp.get("xxl.job.executor.appname"));
    xxlJobExecutor.setIp(xxlJobProp.get("xxl.job.executor.ip"));
    xxlJobExecutor.setPort(xxlJobProp.getInt("xxl.job.executor.port"));
    xxlJobExecutor.setAccessToken(xxlJobProp.get("xxl.job.accessToken"));
    xxlJobExecutor.setLogPath(xxlJobProp.get("xxl.job.executor.logpath"));
    xxlJobExecutor.setLogRetentionDays(xxlJobProp.getInt("xxl.job.executor.logretentiondays"));
    // start executor
    try {
        xxlJobExecutor.start();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : IJobHandler(com.xxl.job.core.handler.IJobHandler) JobHandler(com.xxl.job.core.handler.annotation.JobHandler) IJobHandler(com.xxl.job.core.handler.IJobHandler) PropertiesProxy(org.nutz.ioc.impl.PropertiesProxy) XxlJobExecutor(com.xxl.job.core.executor.XxlJobExecutor)

Example 4 with IJobHandler

use of com.xxl.job.core.handler.IJobHandler in project nutzboot by nutzam.

the class XxlJobStarter method start.

public void start() throws Exception {
    // 从ioc容器中找出所有实现了IJobHandler接口的对象,注册到XxlJobExecutor
    for (IJobHandler jobHandler : appContext.getBeans(IJobHandler.class)) {
        // 看看有没有@JobHandler注解
        JobHandler annoJobHandler = jobHandler.getClass().getAnnotation(JobHandler.class);
        // 得到jobHandlerName
        String jobHandlerName = jobHandler.getClass().getSimpleName();
        if (annoJobHandler != null && !Strings.isBlank(annoJobHandler.value()))
            jobHandlerName = annoJobHandler.value();
        // 注册到XxlJobExecutor上下文
        XxlJobExecutor.registJobHandler(jobHandlerName, jobHandler);
    }
    // 获取XxlJobExecutor,从而触发XxlJobExecutor的初始化
    appContext.getIoc().getByType(XxlJobExecutor.class);
}
Also used : IJobHandler(com.xxl.job.core.handler.IJobHandler) JobHandler(com.xxl.job.core.handler.annotation.JobHandler) IJobHandler(com.xxl.job.core.handler.IJobHandler)

Aggregations

IJobHandler (com.xxl.job.core.handler.IJobHandler)4 JobHandler (com.xxl.job.core.handler.annotation.JobHandler)3 ReturnT (com.xxl.job.core.biz.model.ReturnT)1 ExecutorBlockStrategyEnum (com.xxl.job.core.enums.ExecutorBlockStrategyEnum)1 XxlJobExecutor (com.xxl.job.core.executor.XxlJobExecutor)1 GlueTypeEnum (com.xxl.job.core.glue.GlueTypeEnum)1 GlueJobHandler (com.xxl.job.core.handler.impl.GlueJobHandler)1 ScriptJobHandler (com.xxl.job.core.handler.impl.ScriptJobHandler)1 JobThread (com.xxl.job.core.thread.JobThread)1 PropertiesProxy (org.nutz.ioc.impl.PropertiesProxy)1