Search in sources :

Example 6 with ReturnT

use of com.xxl.job.core.biz.model.ReturnT in project xxl-job by xuxueli.

the class ExecutorBizImpl method log.

@Override
public ReturnT<LogResult> log(long logDateTim, int logId, int fromLineNum) {
    // log filename: logPath/yyyy-MM-dd/9999.log
    String logFileName = XxlJobFileAppender.makeLogFileName(new Date(logDateTim), logId);
    LogResult logResult = XxlJobFileAppender.readLog(logFileName, fromLineNum);
    return new ReturnT<LogResult>(logResult);
}
Also used : LogResult(com.xxl.job.core.biz.model.LogResult) ReturnT(com.xxl.job.core.biz.model.ReturnT) Date(java.util.Date)

Example 7 with ReturnT

use of com.xxl.job.core.biz.model.ReturnT in project xxl-job by xuxueli.

the class ExecutorBizImpl method idleBeat.

@Override
public ReturnT<String> idleBeat(int jobId) {
    // isRunningOrHasQueue
    boolean isRunningOrHasQueue = false;
    JobThread jobThread = XxlJobExecutor.loadJobThread(jobId);
    if (jobThread != null && jobThread.isRunningOrHasQueue()) {
        isRunningOrHasQueue = true;
    }
    if (isRunningOrHasQueue) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, "job thread is running or has trigger queue.");
    }
    return ReturnT.SUCCESS;
}
Also used : JobThread(com.xxl.job.core.thread.JobThread) ReturnT(com.xxl.job.core.biz.model.ReturnT)

Example 8 with ReturnT

use of com.xxl.job.core.biz.model.ReturnT 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 9 with ReturnT

use of com.xxl.job.core.biz.model.ReturnT in project xxl-job by xuxueli.

the class AdminBizImpl method callback.

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) {
        // avoid repeat callback, trigger child job etc
        return new ReturnT<String>(ReturnT.FAIL_CODE, "log repeate callback.");
    }
    // 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 && StringUtils.isNotBlank(xxlJobInfo.getChildJobId())) {
            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 = (StringUtils.isNotBlank(childJobIds[i]) && StringUtils.isNumeric(childJobIds[i])) ? Integer.valueOf(childJobIds[i]) : -1;
                if (childJobId > 0) {
                    ReturnT<String> triggerChildResult = xxlJobService.triggerJob(childJobId);
                    // 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]);
                }
            }
        }
    } else if (IJobHandler.FAIL_RETRY.getCode() == handleCallbackParam.getExecuteResult().getCode()) {
        ReturnT<String> retryTriggerResult = xxlJobService.triggerJob(log.getJobId());
        callbackMsg = "<br><br><span style=\"color:#F39C12;\" > >>>>>>>>>>>" + I18nUtil.getString("jobconf_exe_fail_retry") + "<<<<<<<<<<< </span><br>";
        callbackMsg += MessageFormat.format(I18nUtil.getString("jobconf_callback_msg1"), (retryTriggerResult.getCode() == ReturnT.SUCCESS_CODE ? I18nUtil.getString("system_success") : I18nUtil.getString("system_fail")), retryTriggerResult.getMsg());
    }
    // 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);
    }
    // success, save log
    log.setHandleTime(new Date());
    log.setHandleCode(handleCallbackParam.getExecuteResult().getCode());
    log.setHandleMsg(handleMsg.toString());
    xxlJobLogDao.updateHandleInfo(log);
    return ReturnT.SUCCESS;
}
Also used : ReturnT(com.xxl.job.core.biz.model.ReturnT) XxlJobLog(com.xxl.job.admin.core.model.XxlJobLog) Date(java.util.Date) XxlJobInfo(com.xxl.job.admin.core.model.XxlJobInfo)

Example 10 with ReturnT

use of com.xxl.job.core.biz.model.ReturnT in project xxl-job by xuxueli.

the class XxlJobServiceImpl method triggerJob.

@Override
public ReturnT<String> triggerJob(int id) {
    XxlJobInfo xxlJobInfo = xxlJobInfoDao.loadById(id);
    if (xxlJobInfo == null) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_id") + I18nUtil.getString("system_unvalid")));
    }
    String group = String.valueOf(xxlJobInfo.getJobGroup());
    String name = String.valueOf(xxlJobInfo.getId());
    try {
        XxlJobDynamicScheduler.triggerJob(name, group);
        return ReturnT.SUCCESS;
    } catch (SchedulerException e) {
        logger.error(e.getMessage(), e);
        return new ReturnT<String>(ReturnT.FAIL_CODE, e.getMessage());
    }
}
Also used : SchedulerException(org.quartz.SchedulerException) ReturnT(com.xxl.job.core.biz.model.ReturnT) XxlJobInfo(com.xxl.job.admin.core.model.XxlJobInfo)

Aggregations

ReturnT (com.xxl.job.core.biz.model.ReturnT)16 XxlJobInfo (com.xxl.job.admin.core.model.XxlJobInfo)7 Date (java.util.Date)7 ExecutorBiz (com.xxl.job.core.biz.ExecutorBiz)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 XxlJobLog (com.xxl.job.admin.core.model.XxlJobLog)3 SchedulerException (org.quartz.SchedulerException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 XxlJobGroup (com.xxl.job.admin.core.model.XxlJobGroup)2 TriggerParam (com.xxl.job.core.biz.model.TriggerParam)2 ExecutorBlockStrategyEnum (com.xxl.job.core.enums.ExecutorBlockStrategyEnum)2 JobThread (com.xxl.job.core.thread.JobThread)2 ExecutorFailStrategyEnum (com.xxl.job.admin.core.enums.ExecutorFailStrategyEnum)1 XxlJobLogGlue (com.xxl.job.admin.core.model.XxlJobLogGlue)1 ExecutorRouteStrategyEnum (com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum)1 HandleCallbackParam (com.xxl.job.core.biz.model.HandleCallbackParam)1 LogResult (com.xxl.job.core.biz.model.LogResult)1 GlueTypeEnum (com.xxl.job.core.glue.GlueTypeEnum)1 IJobHandler (com.xxl.job.core.handler.IJobHandler)1 GlueJobHandler (com.xxl.job.core.handler.impl.GlueJobHandler)1