Search in sources :

Example 1 with HandleCallbackParam

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

the class JobThread method run.

@Override
public void run() {
    // init
    try {
        handler.init();
    } catch (Throwable e) {
        logger.error(e.getMessage(), e);
    }
    // execute
    while (!toStop) {
        running = false;
        idleTimes++;
        TriggerParam triggerParam = null;
        ReturnT<String> executeResult = null;
        try {
            // to check toStop signal, we need cycle, so wo cannot use queue.take(), instand of poll(timeout)
            triggerParam = triggerQueue.poll(3L, TimeUnit.SECONDS);
            if (triggerParam != null) {
                running = true;
                idleTimes = 0;
                triggerLogIdSet.remove(triggerParam.getLogId());
                // log filename, like "logPath/yyyy-MM-dd/9999.log"
                String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
                XxlJobFileAppender.contextHolder.set(logFileName);
                ShardingUtil.setShardingVo(new ShardingUtil.ShardingVO(triggerParam.getBroadcastIndex(), triggerParam.getBroadcastTotal()));
                // execute
                XxlJobLogger.log("<br>----------- xxl-job job execute start -----------<br>----------- Param:" + triggerParam.getExecutorParams());
                executeResult = handler.execute(triggerParam.getExecutorParams());
                if (executeResult == null) {
                    executeResult = IJobHandler.FAIL;
                }
                XxlJobLogger.log("<br>----------- xxl-job job execute end(finish) -----------<br>----------- ReturnT:" + executeResult);
            } else {
                if (idleTimes > 30) {
                    XxlJobExecutor.removeJobThread(jobId, "excutor idel times over limit.");
                }
            }
        } catch (Throwable e) {
            if (toStop) {
                XxlJobLogger.log("<br>----------- JobThread toStop, stopReason:" + stopReason);
            }
            StringWriter stringWriter = new StringWriter();
            e.printStackTrace(new PrintWriter(stringWriter));
            String errorMsg = stringWriter.toString();
            executeResult = new ReturnT<String>(ReturnT.FAIL_CODE, errorMsg);
            XxlJobLogger.log("<br>----------- JobThread Exception:" + errorMsg + "<br>----------- xxl-job job execute end(error) -----------");
        } finally {
            if (triggerParam != null) {
                // callback handler info
                if (!toStop) {
                    // commonm
                    TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), executeResult));
                } else {
                    // is killed
                    ReturnT<String> stopResult = new ReturnT<String>(ReturnT.FAIL_CODE, stopReason + " [业务运行中,被强制终止]");
                    TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), stopResult));
                }
            }
        }
    }
    // callback trigger request in queue
    while (triggerQueue != null && triggerQueue.size() > 0) {
        TriggerParam triggerParam = triggerQueue.poll();
        if (triggerParam != null) {
            // is killed
            ReturnT<String> stopResult = new ReturnT<String>(ReturnT.FAIL_CODE, stopReason + " [任务尚未执行,在调度队列中被终止]");
            TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), stopResult));
        }
    }
    // destroy
    try {
        handler.destroy();
    } catch (Throwable e) {
        logger.error(e.getMessage(), e);
    }
    logger.info(">>>>>>>>>>> xxl-job JobThread stoped, hashCode:{}", Thread.currentThread());
}
Also used : TriggerParam(com.xxl.job.core.biz.model.TriggerParam) StringWriter(java.io.StringWriter) ReturnT(com.xxl.job.core.biz.model.ReturnT) HandleCallbackParam(com.xxl.job.core.biz.model.HandleCallbackParam) ShardingUtil(com.xxl.job.core.util.ShardingUtil) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Example 2 with HandleCallbackParam

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

the class TriggerCallbackThread method start.

public void start() {
    // valid
    if (XxlJobExecutor.getAdminBizList() == null) {
        logger.warn(">>>>>>>>>>> xxl-job, executor callback config fail, adminAddresses is null.");
        return;
    }
    triggerCallbackThread = new Thread(new Runnable() {

        @Override
        public void run() {
            // normal callback
            while (!toStop) {
                try {
                    HandleCallbackParam callback = getInstance().callBackQueue.take();
                    if (callback != null) {
                        // callback list param
                        List<HandleCallbackParam> callbackParamList = new ArrayList<HandleCallbackParam>();
                        int drainToNum = getInstance().callBackQueue.drainTo(callbackParamList);
                        callbackParamList.add(callback);
                        // callback, will retry if error
                        if (callbackParamList != null && callbackParamList.size() > 0) {
                            doCallback(callbackParamList);
                        }
                    }
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }
            // last callback
            try {
                List<HandleCallbackParam> callbackParamList = new ArrayList<HandleCallbackParam>();
                int drainToNum = getInstance().callBackQueue.drainTo(callbackParamList);
                if (callbackParamList != null && callbackParamList.size() > 0) {
                    doCallback(callbackParamList);
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            logger.info(">>>>>>>>>>> xxl-job, executor callback thread destory.");
        }
    });
    triggerCallbackThread.setDaemon(true);
    triggerCallbackThread.start();
}
Also used : HandleCallbackParam(com.xxl.job.core.biz.model.HandleCallbackParam) ArrayList(java.util.ArrayList)

Example 3 with HandleCallbackParam

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

the class AdminBizImpl method callback.

@Override
public ReturnT<String> callback(List<HandleCallbackParam> callbackParamList) {
    for (HandleCallbackParam handleCallbackParam : callbackParamList) {
        ReturnT<String> callbackResult = callback(handleCallbackParam);
        logger.info(">>>>>>>>> JobApiController.callback {}, handleCallbackParam={}, callbackResult={}", (callbackResult.getCode() == IJobHandler.SUCCESS.getCode() ? "success" : "fail"), handleCallbackParam, callbackResult);
    }
    return ReturnT.SUCCESS;
}
Also used : HandleCallbackParam(com.xxl.job.core.biz.model.HandleCallbackParam)

Aggregations

HandleCallbackParam (com.xxl.job.core.biz.model.HandleCallbackParam)3 ReturnT (com.xxl.job.core.biz.model.ReturnT)1 TriggerParam (com.xxl.job.core.biz.model.TriggerParam)1 ShardingUtil (com.xxl.job.core.util.ShardingUtil)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1