/**
  * do callback, will retry if error
  * @param callbackParamList
  */
 private void doCallback(List <HandleCallbackParam> callbackParamList)
 {
     // callback, will retry if error
     foreach (AdminBiz adminBiz in XxlJobExecutor.getAdminBizList())
     {
         try
         {
             ReturnT <String> callbackResult = adminBiz.callback(callbackParamList);
             if (callbackResult != null && ReturnT <string> .SUCCESS_CODE == callbackResult.code)
             {
                 callbackResult = ReturnT <string> .SUCCESS;
                 logger.Info(string.Format(">>>>>>>>>>> xxl-job callback success, callbackParamList:{0}, callbackResult:{1}", callbackParamList, callbackResult));
                 break;
             }
             else
             {
                 logger.Info(string.Format(">>>>>>>>>>> xxl-job callback fail, callbackParamList:{0}, callbackResult:{1}", callbackParamList, callbackResult));
             }
         }
         catch (Exception e)
         {
             logger.Error(string.Format(">>>>>>>>>>> xxl-job callback error, callbackParamList:{0}", callbackParamList), e);
             //getInstance().callBackQueue.addAll(callbackParamList);
         }
     }
 }
Пример #2
0
        void innerStart()
        {
            while (!toStop1)
            {
                running = false;
                idleTimes++;
                try
                {
                    // to check toStop signal, we need cycle, so wo cannot use queue.take(), instand of poll(timeout)
                    var result = triggerQueue.TryTake(out TriggerParam triggerParam, 3 * 1000);


                    if (triggerParam != null)
                    {
                        running   = true;
                        idleTimes = 0;
                        triggerLogIdSet.Remove(triggerParam.logId);

                        // parse param
                        string[] handlerParams = (triggerParam.executorParams != null && triggerParam.executorParams.Trim().Length > 0)
                                ? triggerParam.executorParams.Split(',') : null;

                        // handle job
                        ReturnT <String> executeResult = null;
                        try
                        {
                            // log filename: yyyy-MM-dd/9999.log
                            String logFileName = XxlJobFileAppender.makeLogFileName(TimeUtil.ToTime(triggerParam.logDateTim), triggerParam.logId);

                            XxlJobFileAppender.contextHolder.Value = (logFileName);
                            ShardingUtil.setShardingVo(new ShardingUtil.ShardingVO(triggerParam.broadcastIndex, triggerParam.broadcastTotal));
                            XxlJobLogger.log("<br>----------- xxl-job job execute start -----------<br>----------- Params:" + string.Join(",", handlerParams ?? new[] { "" }));

                            executeResult = handler.execute(handlerParams);
                            if (executeResult == null)
                            {
                                executeResult = ReturnT <string> .FAIL;
                            }

                            XxlJobLogger.log("<br>----------- xxl-job job execute end(finish) -----------<br>----------- ReturnT:" + executeResult);
                        }
                        catch (Exception e)
                        {
                            if (toStop1)
                            {
                                XxlJobLogger.log("<br>----------- JobThread toStop, stopReason:" + stopReason);
                            }

                            //StringWriter stringWriter = new StringWriter();
                            //e.printStackTrace(new PrintWriter(stringWriter));
                            String errorMsg = e.ToString();
                            executeResult = new ReturnT <String>(ReturnT <string> .FAIL_CODE, errorMsg);

                            XxlJobLogger.log("<br>----------- JobThread Exception:" + errorMsg + "<br>----------- xxl-job job execute end(error) -----------");
                        }

                        // callback handler info
                        if (!toStop1)
                        {
                            // commonm
                            TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.logId, executeResult));
                        }
                        else
                        {
                            // is killed
                            ReturnT <String> stopResult = new ReturnT <String>(ReturnT <string> .FAIL_CODE, stopReason + " [业务运行中,被强制终止]");
                            TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.logId, stopResult));
                        }
                    }
                    else
                    {
                        if (idleTimes > 30)
                        {
                            XxlJobExecutor.removeJobThread(jobId, "excutor idel times over limit.");
                        }
                    }
                }
                catch (Exception e)
                {
                    if (toStop1)
                    {
                        XxlJobLogger.log("<br>----------- xxl-job toStop, stopReason:" + stopReason);
                    }


                    String errorMsg = e.ToString();
                    XxlJobLogger.log("----------- xxl-job JobThread Exception:" + errorMsg);
                }
            }

            // callback trigger request in queue
            while (triggerQueue != null && triggerQueue.Count > 0)
            {
                triggerQueue.TryTake(out TriggerParam triggerParam);
                if (triggerParam != null)
                {
                    // is killed
                    ReturnT <String> stopResult = new ReturnT <String>(ReturnT <string> .FAIL_CODE, stopReason + " [任务尚未执行,在调度队列中被终止]");
                    TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.logId, stopResult));
                }
            }

            logger.Info(string.Format(">>>>>>>>>>>> xxl-job JobThread stoped, hashCode:{0}", Thread.CurrentThread));
        }
Пример #3
0
        public void start(int port, String ip, String appName)
        {
            // valid
            if (appName == null || appName.Trim().Length == 0)
            {
                logger.Warn(">>>>>>>>>>>> xxl-job, executor registry config fail, appName is null.");
                return;
            }
            if (XxlJobExecutor.getAdminBizList() == null)
            {
                logger.Warn(">>>>>>>>>>>> xxl-job, executor registry config fail, adminAddresses is null.");
                return;
            }

            // executor address (generate addredd = ip:port)
            String executorAddress;

            if (ip != null && ip.Trim().Length > 0)
            {
                executorAddress = ip.Trim() + (":") + port;
            }
            else
            {
                executorAddress = IpUtil.getIpPort(port);
            }

            registryThread = new Thread(
                () =>
            {
                // registry
                while (!toStop1)
                {
                    try
                    {
                        RegistryParam registryParam = new RegistryParam(RegistryConfig.RegistType.EXECUTOR.ToString(), appName, executorAddress);
                        foreach (AdminBiz adminBiz in XxlJobExecutor.getAdminBizList())
                        {
                            try
                            {
                                Object registry = adminBiz.registry(registryParam);
                                ReturnT <String> registryResult = adminBiz.registry(registryParam);
                                if (registryResult != null && ReturnT <string> .SUCCESS_CODE == registryResult.code)
                                {
                                    registryResult = ReturnT <string> .SUCCESS;
                                    logger.Info(string.Format(">>>>>>>>>>> xxl-job registry success, registryParam:{0}, registryResult:{1}", registryParam, registryResult));
                                    break;
                                }
                                else
                                {
                                    logger.Info(string.Format(">>>>>>>>>>> xxl-job registry fail, registryParam:{0}, registryResult:{1}", registryParam, registryResult));
                                }
                            }
                            catch (Exception e)
                            {
                                logger.Info($">>>>>>>>>>> xxl-job registry error, registryParam:{registryParam}", e);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error(e.Message, e);
                    }

                    Thread.Sleep(RegistryConfig.BEAT_TIMEOUT * 1000);
                }

                // registry remove
                try
                {
                    RegistryParam registryParam = new RegistryParam(RegistryConfig.RegistType.EXECUTOR.ToString(), appName, executorAddress);
                    foreach (AdminBiz adminBiz in XxlJobExecutor.getAdminBizList())
                    {
                        try
                        {
                            ReturnT <String> registryResult = adminBiz.registryRemove(registryParam);
                            if (registryResult != null && ReturnT <string> .SUCCESS_CODE == registryResult.code)
                            {
                                registryResult = ReturnT <string> .SUCCESS;
                                logger.Info(string.Format(">>>>>>>>>>> xxl-job registry-remove success, registryParam:{0}, registryResult:{1}", registryParam, registryResult));
                                break;
                            }
                            else
                            {
                                logger.Info(string.Format(">>>>>>>>>>> xxl-job registry-remove fail, registryParam:{0}, registryResult:{1}", registryParam, registryResult));
                            }
                        }
                        catch (Exception e)
                        {
                            logger.Info(string.Format(">>>>>>>>>>> xxl-job registry-remove error, registryParam:{0}", registryParam), e);
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.Error(e.Message, e);
                }
                logger.Warn(">>>>>>>>>>>> xxl-job, executor registry thread destory.");
            }
                );
            registryThread.IsBackground = (true);
            registryThread.Start();
        }
        public void start()
        {
            // valid
            if (XxlJobExecutor.getAdminBizList() == null)
            {
                logger.WarnFormat(">>>>>>>>>>>> xxl-job, executor callback config fail, adminAddresses is null.");
                return;
            }

            triggerCallbackThread = new Thread(() =>
            {
                // normal callback
                while (!toStop1)
                {
                    try
                    {
                        getInstance().callBackQueue.TryTake(out HandleCallbackParam callback);
                        if (callback != null)
                        {
                            // callback list param
                            List <HandleCallbackParam> callbackParamList = new List <HandleCallbackParam>();
                            var arrays = getInstance().callBackQueue.ToArray();
                            callbackParamList.AddRange(arrays);
                            callbackParamList.Add(callback);

                            // callback, will retry if error
                            if (callbackParamList != null && callbackParamList.Count > 0)
                            {
                                doCallback(callbackParamList);
                            }
                        }
                        if (callback == null)
                        {
                            Thread.Sleep(10);
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error(e.Message, e);
                    }
                }

                // last callback
                try
                {
                    List <HandleCallbackParam> callbackParamList = new List <HandleCallbackParam>();
                    //int drainToNum = getInstance().callBackQueue.drainTo(callbackParamList);
                    callbackParamList.AddRange(getInstance().callBackQueue.ToArray());
                    if (callbackParamList != null && callbackParamList.Count > 0)
                    {
                        doCallback(callbackParamList);
                    }
                }
                catch (Exception e)
                {
                    logger.Error(e.Message, e);
                }
                logger.WarnFormat(">>>>>>>>>>>> xxl-job, executor callback thread destory.");
            });
            triggerCallbackThread.IsBackground = (true);
            triggerCallbackThread.Name         = ("triggerCallbackThread");
            triggerCallbackThread.Start();
        }