/**
  * 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);
         }
     }
 }
Ejemplo n.º 2
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();
        }