public static void Start(bool lazyStart = false)
        {
            if (lazyStart)
            {
                LazyStart = true;
                YmatouLoggingService.Debug("延迟启动...");
                return;
            }
            Status = BootstrapperStatus.Starting;
            var watch = Stopwatch.StartNew();

            YmatouLoggingService.Debug("YmatouFramework开始启动...内部版本号:{0}", Version);

            bootstrapper = new BootstrapperProxy(container);
            if (bootstrapper.Execute())
            {
                Status = BootstrapperStatus.Started;
                YmatouLoggingService.Debug("YmatouFramework启动完成!,耗时:{0} 秒...内部版本号:{1}", watch.ElapsedMilliseconds / 1000.0, Version);
            }
            else
            {
                Status = BootstrapperStatus.FailedToStart;
                throw new Exception(string.Format("YmatouFramework启动失败!,耗时 {0} 秒 {1}", watch.ElapsedMilliseconds / 1000.0, Version));
            }
        }
示例#2
0
 private void RemoveExpiredTask()
 {
     if (start == 1)
     {
         return;
     }
     YmatouLoggingService.Debug("注册自动移除缓存项task");
     timer = new System.Threading.Timer(_ =>
     {
         var _cache = _ as ConcurrentDictionary <TKey, CacheItem <TValue> >;
         if (_cache == null)
         {
             return;
         }
         var expiredKey = _cache.AsParallel().Where(e => CheckExpired(e.Value.Expired)).Select(k => k.Key);
         //YmatouLoggingService.Debug("过期key , {0}", string.Join<TKey>(",", expiredKey.ToArray()));
         expiredKey.Each(k =>
         {
             CacheItem <TValue> val;
             _cache.TryRemove(k, out val);
         });
     }, cache, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(10));
     //timer.Change(System.Threading.Timeout.Infinite, 10000);
     System.Threading.Interlocked.CompareExchange(ref start, 1, 0);
 }
示例#3
0
        public PriorityQueueWrapper <K, V> Enqueue(K k, V v)
        {
            if (!init || PQStatus != Status.RunIng)
            {
                YmatouLoggingService.Debug("优先队列,{0}", !init || PQStatus != Status.RunIng);
                return(this);
            }
            //while (PQ.Count >= pqSetting.MaxItems)
            //{
            //    if (pqSetting.PQS == PQItemOverflowMaxStrategy.DeleteOldItem)
            //        PQ.Dequeue();
            //    if (pqSetting.PQS == PQItemOverflowMaxStrategy.ExpandMaxCount)
            //        break;
            //    if (pqSetting.PQS == PQItemOverflowMaxStrategy.WaitConsumerItem)
            //    {

            //    }
            //}
            var pqObj = new PQValue <K, V>
            {
                ConsumerCount  = 0,
                ConsumerResult = QueueItemConsumerStats.NoConsumer,
                Key            = k,
                Value          = v
            };

            PQ.Enqueue(k, pqObj);
            return(this);
        }
示例#4
0
 protected override ExecuteResult <int> DoCommit(int retry = 0)
 {
     if (Committed)
     {
         return(ExecuteResult <int> .CreateFaile(0, "已提交"));
     }
     try
     {
         var result = this.Context.SaveChanges();
         this.Committed = true;
         return(ExecuteResult <int> .CreateSuccess(result, "提交成功"));
     }
     catch (Exception ex)
     {
         YmatouLoggingService.Warning("执行失败 {0},重试 {1} 次", ex.ToString(), retry);
         //重试
         //return RetryUtility.RetryAction2(() =>
         // {
         //     var result = this.Context.SaveChanges();
         //     this.Committed = true;
         //     return Tuple.Create<ExecuteResult<int>, bool>(new ExecuteResult<int>(true, null, result), result > 0);
         // }, retry, 3000).Item1;
         //RetryUtility.RetryAction2(() => System.Tuple.Create<ExecuteResult<int>, bool>(new ExecuteResult<int>(true, null, 0), true), 1000);
         return(new ExecuteResult <int>(true, null, 0));
     }
 }
示例#5
0
 public void Start()
 {
     if (init || PQStatus == Status.RunIng)
     {
         YmatouLoggingService.Warning("PQ 队列已启动");
         return;
     }
     if (thList.Count <= 0)
     {
         lock (thList)
         {
             if (thList.Count <= 0)
             {
                 YmatouLoggingService.Debug("启动队列开始 {0}", pqSetting.QName);
                 //thList = new List<Thread>();
                 for (var i = 0; i < pqSetting.ThreadCount; i++)
                 {
                     var th = new Thread(Consumer)
                     {
                         IsBackground = true, Name = "PQ_" + i
                     };
                     th.Start();
                     thList.Add(th);
                 }
                 init     = true;
                 PQStatus = Status.RunIng;
                 YmatouLoggingService.Debug("启动队列结束 {0}", pqSetting.QName);
             }
         }
     }
 }
示例#6
0
 public void Info()
 {
     YmatouLoggingService.InitLogService();
     YmatouLoggingService.Debug("{0}", "test log4net debug");
     YmatouLoggingService.Info("{0}", "test log4net debug");
     YmatouLoggingService.Error("{0}", "test log4net debug");
 }
示例#7
0
        public bool Execute()
        {
            bool successful = true;
            var  tasks      = container.ResolveAll <BootstrapperTask>().OrderBy(t => t.Order).ToList();

            foreach (var task in tasks)
            {
                YmatouLoggingService.Debug("YmatouFramework.Bootstrapper 开始执行 '{0}' ({1})", task.GetType().FullName, task.Description);
                try
                {
                    if (task.Execute() == TaskContinuation.Break)
                    {
                        YmatouLoggingService.Error("YmatouFramework.Bootstrapper 执行中断 '{0}' ({1})", task.GetType().FullName, task.Description);
                        successful = false;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    successful = false;
                    YmatouLoggingService.Error("YmatouFramework.Bootstrapper 执行出错 '{0}',异常信息:{1}", task.GetType().FullName, ex.ToString());
                }
            }
            ;
            return(successful);
        }
        public void Start()
        {
            if (_isInit && BQStatus == Status.RunIng)
            {
                return;
            }

            if (_thList.Count <= 0)
            {
                lock (lockObj)
                {
                    if (_thList.Count <= 0)
                    {
                        YmatouLoggingService.Debug("启动队列,{0}", _queueSettings.QueueName);
                        for (var i = 0; i < this._queueSettings.ConsumerThreadCount; i++)
                        {
                            var _th = new Thread(Consumer)
                            {
                                IsBackground = true, Name = string.Format("{0}{1}", _queueSettings.QueueName, i)
                            };
                            //_th.Start();
                            _thList.Add(_th);
                        }
                        _isInit  = true;
                        BQStatus = Status.RunIng;
                        _thList.ForEach(e => e.Start());
                        YmatouLoggingService.Debug("启动完成,{0}", _queueSettings.QueueName);
                    }
                }
            }
        }
        public static void Stop()
        {
            Status = BootstrapperStatus.Ending;

            YmatouLoggingService.Debug("YmatouFramework开始清理");
            var watch = Stopwatch.StartNew();

            bootstrapper.Dispose();
            Status = BootstrapperStatus.Ended;

            YmatouLoggingService.Debug("YmatouFramework清理完成!耗时 :{0}秒 ", watch.ElapsedMilliseconds / 1000.0);
        }
示例#10
0
        public static T GetConfig <T>(string fileName, T defVal)
        {
            object instance     = null;
            string fileFullName = GetConfigFileFullName(fileName);

            if (ConfigCache.TryGetValue(fileFullName, out instance))
            {
                return((T)instance);
            }
            lock (Locker)
            {
                if (ConfigCache.TryGetValue(fileFullName, out instance))
                {
                    return((T)instance);
                }
                if (!File.Exists(fileFullName))
                {
                    TryCreateConfig(fileName, defVal);
                    return(defVal);
                }
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(fileFullName);
                }
                catch (Exception ex)
                {
                    string errMsg = string.Format("加载配置文件 {0} 失败!使用默认配置文件!,异常信息:{1}", fileFullName, ex);
                    YmatouLoggingService.Error(errMsg);
                    return(defVal);
                }
                ConfigFileWatcher configFileWatcher = null;
                if (!ConfigFileWatcherCache.TryGetValue(fileFullName, out configFileWatcher))
                {
                    ConfigFileWatcherCache.Add(fileFullName, new ConfigFileWatcher(fileFullName, OnConfigChanged));
                }
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                using (StringReader sr = new StringReader(doc.OuterXml))
                {
                    try
                    {
                        instance = (T)xmlSerializer.Deserialize(sr);
                        ConfigCache.Add(fileFullName, instance);
                        return((T)instance);
                    }
                    catch (Exception ex)
                    {
                        YmatouLoggingService.Debug("反序列化异常,类型名称:{0},异常信息:{1}", typeof(T).Name, ex.ToString());
                        return(defVal);
                    }
                }
            }
        }
示例#11
0
 private void Log <TResult>(
     DbCommand command, DbCommandInterceptionContext <TResult> interceptionContext, string sqlType = null, string desc = null)
 {
     if (interceptionContext.Exception != null)
     {
         YmatouLoggingService.Error("sql {0},{1}, {2} -> failed with exception {3}", sqlType, desc,
                                    command.CommandText.Replace(Environment.NewLine, ""), interceptionContext.Exception);
     }
     else
     {
         YmatouLoggingService.Info("sql {0} ,{1},{2}", sqlType, desc, command.CommandText.Replace(Environment.NewLine, ""));
     }
 }
 public void Stop()
 {
     lock (this)
     {
         try
         {
             if (!_isInit)
             {
                 YmatouLoggingService.Debug("服务未初始化无需清理");
                 return;
             }
             YmatouLoggingService.Debug("开始清理");
             //清除队列数据
             if (_queue != null)
             {
                 _queue.Clear();
             }
             //暴力终止
             if (_thList.Count > 0)
             {
                 foreach (var thObj in _thList)
                 {
                     thObj.Join(10);
                 }
                 foreach (var thObj in _thList)
                 {
                     try
                     {
                         thObj.Abort();
                     }
                     catch (ThreadAbortException e)
                     {
                     }
                     catch (ThreadInterruptedException ex)
                     {
                     }
                 }
                 _thList.Clear();
             }
             _consumerStats = false;
             _isInit        = false;
             BQStatus       = Status.Stop;
             YmatouLoggingService.Debug("清理完成");
         }
         catch (QueueException e)
         {
             YmatouLoggingService.Debug("终止服务错误:{0},{1},{2}", "BBQW", e.Message, e.StackTrace);
         }
     }
 }
示例#13
0
 protected override void InternalDispose()
 {
     container.ResolveAll <BootstrapperTask>().OrderByDescending(t => t.Order).Each(task =>
     {
         try
         {
             YmatouLoggingService.Debug("YmatouFramework.Bootstrapper 开始清理 '{0}' ({1})", task.GetType().FullName, task.Description);
             task.Dispose();
         }
         catch (Exception ex)
         {
             YmatouLoggingService.Error("YmatouFramework.Bootstrapper 清理出错 '{0}',异常信息:{1}", task.GetType().FullName, ex.ToString());
         }
     });
 }
        private void ReisterDbContext()
        {
            #region [ PerResolveLifetimeManager ]
            //PerResolveLifetimeManager 解决循环引用而重复引用的生命周期,
            //似于TransientLifetimeManager,但是其不同在于,如果应用了这种生命周期管理器,则在第一调用的时候会创建一个新的对象,
            //而再次通过循环引用访问到的时候就会返回先前创建的对象实例(单件实例)

            /**
            **
            *  public interface IPresenter { }
            *   public class MockPresenter : IPresenter
            *   {
            *       public IView View { get; set; }
            *
            *       public MockPresenter(IView view)
            *       {
            *           View = view;
            *       }
            *   }
            *
            *   public interface IView
            *   {
            *       IPresenter Presenter { get; set; }
            *   }
            *
            *   public class View : IView
            *   {
            *       [Dependency]
            *       public IPresenter Presenter { get; set; }
            *   }
            *
            * container.RegisterType<YmtSystemDbContext, YmtSystemDbContext>(new PerResolveLifetimeManager());
            * return
            ****/
            //PerThreadLifetimeManager 对每个线程都是唯一的
            #endregion

            if (HttpContext.Current != null)
            {
                YmatouLoggingService.Debug("DbContext HttpContextLifetimeManager");
                container.RegisterType <YmtSystemDbContext, YmtSystemDbContext>(new HttpContextLifetimeManager <YmtSystemDbContext>());
            }
            else
            {
                YmatouLoggingService.Debug("DbContext PerThreadLifetimeManager");
                container.RegisterType <YmtSystemDbContext, YmtSystemDbContext>(new PerThreadLifetimeManager());
            }
        }
示例#15
0
 public void Stop()
 {
     lock (this)
     {
         try
         {
             if (!init || PQStatus == Status.NoInit)
             {
                 return;
             }
             //清除队列数据
             if (PQ != null)
             {
                 PQ.Clear();
             }
             //暴力终止
             if (PQ.Count > 0)
             {
                 foreach (var thObj in thList)
                 {
                     thObj.Join(10);
                 }
                 foreach (var thObj in thList)
                 {
                     try
                     {
                         thObj.Abort();
                     }
                     catch (ThreadAbortException e)
                     {
                     }
                     catch (ThreadInterruptedException ex)
                     {
                     }
                 }
                 thList.Clear();
             }
             PQStatus = Status.Stop;
             init     = false;
         }
         catch (QueueException e)
         {
             YmatouLoggingService.Error("终止服务错误:{0},{1},{2}", "BBQW", e.Message, e.StackTrace);
         }
     }
 }
示例#16
0
 public static DbConnection Builder(DbConfigure cfg)
 {
     try
     {
         YmtSystemAssert.AssertArgumentNotNull(cfg, "DbConfigure 不能为空");
         var factory    = DbProviderFactories.GetFactory(cfg.Provider);
         var connection = factory.CreateConnection();
         connection.ConnectionString = cfg.Connection;
         //LocalFileCfg.GetLocalCfgValueByFile<string>("db.cfg", ValueFormart.CJsv, @"Data Source=YMT-LIGUO\LIGUO;Initial Catalog=Ymt_Test_7;Persist Security Info=True;User ID=sa;Password=123@#$123asd");
         //TODO:这里可以设置其他链接属性
         return(connection);
     }
     catch (Exception ex)
     {
         YmatouLoggingService.Error("创建链接对象异常 {0}", ex.ToString());
         throw;
     }
 }
示例#17
0
 private static bool EnsureConfigDirectoryExists()
 {
     if (Directory.Exists(LocalConfigDirectory))
     {
         return(true);
     }
     try
     {
         Directory.CreateDirectory(LocalConfigDirectory);
         return(true);
     }
     catch (Exception ex)
     {
         string errMsg = string.Format("创建目录 {0} 失败!异常信息:{1}", LocalConfigDirectory, ex);
         YmatouLoggingService.Error(errMsg);
         return(false);
     }
 }
示例#18
0
 private static T GetInstance <T>(string name = "")
 {
     try
     {
         if (string.IsNullOrEmpty(name))
         {
             return(YmatouBootstrapperFramework.Container.Resolve <T>());
         }
         else
         {
             return(YmatouBootstrapperFramework.Container.Resolve <T>(name));
         }
     }
     catch (Exception ex)
     {
         YmatouLoggingService.Error("YmatouFramework.LocalServiceLocator 不能解析 '{0}',异常信息:{1}", typeof(T).FullName, ex.ToString());
         throw;
     }
 }
示例#19
0
        private static void TryCreateConfig <T>(string fileName, T defVal)
        {
            if (!EnsureConfigDirectoryExists())
            {
                return;
            }
            string fileFullName = GetConfigFileFullName(fileName);

            if (File.Exists(fileFullName))
            {
                return;
            }
            if (defVal == null)
            {
                return;
            }
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Encoding = Encoding.UTF8;
            settings.Indent   = true;
            XmlSerializer xs = new XmlSerializer(typeof(T), (string)null);

            using (FileStream fs = new FileStream(fileFullName, FileMode.Create))
            {
                using (XmlWriter xw = XmlWriter.Create(fs, settings))
                {
                    try
                    {
                        xs.Serialize(xw, defVal);
                    }
                    catch (Exception ex)
                    {
                        YmatouLoggingService.Error("序列化异常,类型名称:{0},异常信息:{1}", typeof(T).Name, ex.ToString());
                    }
                }
            }
        }
示例#20
0
        public PriorityQueueWrapper <K, V> Init(Action <List <Object> > action, string qName, PQSettings setting, bool isStart = true)
        {
            if (string.IsNullOrEmpty(qName))
            {
                throw new ArgumentNullException("队列名称不能空");
            }
            if (!init)
            {
                lock (lockObj)
                {
                    if (!init)
                    {
                        YmatouLoggingService.Debug("开始初始化队列->{0}", qName);
                        if (setting == null)
                        {
                            pqSetting = new PQSettings
                            {
                                CFS               = ConsumerFailStrategy.ClientHandleFail,
                                PQS               = PQItemOverflowMaxStrategy.DeleteOldItem,
                                QName             = qName,
                                ScanningTime      = TimeSpan.FromMilliseconds(20),
                                ThreadCount       = Environment.ProcessorCount,
                                ConsumerItemCount = 3,
                                ConsumeAction     = action,
                                MaxItems          = 100000
                            };
                        }
                        else
                        {
                            if (setting.ThreadCount > Environment.ProcessorCount || setting.ThreadCount <= 0)
                            {
                                setting.ThreadCount = Environment.ProcessorCount;
                            }
                            if (setting.ScanningTime == new TimeSpan(0, 0, 0, 0))
                            {
                                setting.ScanningTime = TimeSpan.FromSeconds(1);
                            }
                            if (setting.MaxItems > 100000 || setting.MaxItems <= 0)
                            {
                                setting.MaxItems = 100000;
                            }
                            if (setting.ConsumerItemCount > setting.MaxItems || setting.ConsumerItemCount <= 0)
                            {
                                setting.ConsumerItemCount = 3;
                            }
                            setting.ConsumeAction = action;
                            setting.QName         = qName;
                            pqSetting             = setting;
                        }

                        thList = new List <Thread>(pqSetting.ThreadCount);
                        PQ     = new ConcurrentPriorityQueueThreadManager <K, PQValue <K, V> >(pqSetting.MaxItems);
                        if (isStart)
                        {
                            Start();
                            PQStatus = Status.RunIng;
                        }
                        else
                        {
                            PQStatus = Status.Stop;
                        }
                        init = true;
                        YmatouLoggingService.Debug("完成初始化队列->{0}", qName);
                    }
                }
            }
            return(this);
        }
示例#21
0
 public static void Handler(this Exception ex)
 {
     YmatouLoggingService.Error("Handler ", ex);
 }
示例#22
0
 public static void Handler(this Exception ex, string formart, params object[] args)
 {
     YmatouLoggingService.Error(formart + "," + ex.ToString(), args);
 }
示例#23
0
 public static void Handler(this Exception ex, string message = "")
 {
     YmatouLoggingService.Error(message, ex);
 }
示例#24
0
        public ExecuteResult <int> Commit(int retry = 0, bool lockd = false)
        {
            var tmpRetry = retry;
            var fail     = false;
            var result   = 0;

            do
            {
                try
                {
                    result = base.SaveChanges();
                    fail   = false;
                    YmatouLoggingService.Debug("Db Context Commit {0}", result);
                    return(ExecuteResult <int> .CreateSuccess(result, "提交成功"));
                }
                catch (DbEntityValidationException ex)
                {
                    ex.EntityValidationErrors.Each(_e =>
                    {
                        var errs = _e.Entry.GetValidationResult().ValidationErrors.ValidationErrorAppendToString();
                        YmatouLoggingService.Error("entity:{0},Validation fail:{1}", _e.Entry.Entity.GetType().Name,
                                                   errs);
                    });
                    return(ExecuteResult <int> .CreateFaile(0, "Db Context Commit fail"));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    fail = true;
                    ex.Entries
                    .Each(entry =>
                    {
                        if (entry != null)
                        {
                            if (entry.State == EntityState.Added || entry.State == EntityState.Modified)
                            {
                                //Context中实体的值覆盖db中实体的值
                                var currentVal = entry.CurrentValues;     //当前值
                                var dbVal      = entry.GetDatabaseValues();
                                //获取数据库中的值
                                entry.OriginalValues.SetValues(dbVal);
                                //entry.CurrentValues.SetValues(dbVal.Clone());
                                entry.State = EntityState.Modified;
                            }
                            else
                            {
                                //数据库中的覆盖entity中的值
                                entry.Reload();
                            }
                        }
                    }, errorHandler: err =>
                    {
                        YmatouLoggingService.Error("重试异常 {0}", err.ToString());
                    });
                    var _ex = ex.GetBaseException() as SqlException;
                    if (_ex != null && (_ex.Number == 2627 || _ex.Number == 515))
                    {
                        YmatouLoggingService.Debug("EFUnitOfWork DbUpdateConcurrencyException repeat insert,{0}",
                                                   _ex.Message);
                    }
                    else
                    {
                        YmatouLoggingService.Error(
                            "EFUnitOfWork DbUpdateConcurrencyException entity : {0},err:{1},重试 {2} 次",
                            ex.Entries.TrySerializeEntity(),
                            ex.ToString(), tmpRetry);
                    }
                }
//                catch (DbUpdateException ex)
//                {
//                    fail = true;
//                    result = -1;
//                    var _ex = ex.GetBaseException() as SqlException;
//                    if (_ex != null && (_ex.Number == 2627 || _ex.Number == 515))
//                        YmatouLoggingService.Debug("EFUnitOfWork DbUpdateException repeat insert,{0}", _ex.Message);
//                    else
//                        YmatouLoggingService.Error("EFUnitOfWork DbUpdateException entity : {0},err:{1},重试 {2} 次",
//                            ex.Entries.TrySerializeEntity(),
//                            ex.ToString(), tmpRetry);
//                    throw;
//                }
            } while (fail && tmpRetry-- > 0);
            return(new ExecuteResult <int>(fail == false, fail == false ? "执行成功" : "更新失败", result));
        }
示例#25
0
        public async Task <ExecuteResult <int> > AsyncCommit(int retry = 0)
        {
            var  tmpRetry = retry;
            bool fail;
            var  result = 0;

            do
            {
                try
                {
                    result = await base.SaveChangesAsync().ConfigureAwait(false);

                    fail = false;
                    return(await Task.Factory.StartNew(() => ExecuteResult <int> .CreateSuccess(result, "已提交")));
                }
                catch (DbEntityValidationException ex)
                {
                    ex.EntityValidationErrors.Each(_e =>
                    {
                        var errs = _e.Entry.GetValidationResult().ValidationErrors.ValidationErrorAppendToString();
                        YmatouLoggingService.Error("实体:{0},验证错误:{1}", _e.Entry.Entity.GetType().Name, errs);
                    });
                    return(ExecuteResult <int> .CreateFaile(0, "提交失败"));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    fail = true;
                    ex.Entries.Each(entry =>
                    {
                        if (entry != null)
                        {
                            if (entry.State == EntityState.Added || entry.State == EntityState.Modified)
                            {
                                //Context中实体的值覆盖db中实体的值
                                var currentVal = entry.CurrentValues;       //当前值
                                var dbVal      = entry.GetDatabaseValues(); //获取数据库中的值
                                entry.OriginalValues.SetValues(dbVal);
                                entry.CurrentValues.SetValues(dbVal.Clone());
                                entry.State = EntityState.Modified;
                            }
                            else
                            {
                                //数据库中的覆盖entity中的值
                                entry.Reload();
                            }
                        }
                    });
                    var _ex = ex.GetBaseException() as SqlException;
                    if (_ex != null && (_ex.Number == 2627 || _ex.Number == 515))
                    {
                        YmatouLoggingService.Debug("EFUnitOfWork DbUpdateConcurrencyException  repeat insert,{0}", _ex.Message);
                    }
                    else
                    {
                        YmatouLoggingService.Error("EFUnitOfWork DbUpdateConcurrencyException entity : {0},err:{1},重试 {2} 次", ex.Entries.TrySerializeEntity(),
                                                   ex.ToString(), tmpRetry);
                    }
                }
//                catch (DbUpdateException ex)
//                {
//                    fail = true;
//                    var _ex = ex.GetBaseException() as SqlException;
//                    if (_ex != null && (_ex.Number == 2627 || _ex.Number == 515))
//                        YmatouLoggingService.Debug("EFUnitOfWork DbUpdateException  repeat insert,{0}", _ex.Message);
//                    else
//                        YmatouLoggingService.Error("EFUnitOfWork DbUpdateException entity : {0},err:{1},重试 {2} 次",
//                            ex.Entries.TrySerializeEntity(),
//                            ex.ToString(), tmpRetry);
//                }
            } while (fail && tmpRetry-- > 0);
            return(await Task.Factory.StartNew(() => new ExecuteResult <int>(fail == false, fail == false ? "执行成功" : "提交失败", result)));
        }
        private void Consumer()
        {
            while (BQStatus == Status.RunIng)
            {
                _consumerStats = true;
                var _consumerTime      = _queueSettings.ConsumerTime;
                var _consumerItemCount = _queueSettings.ConsumerItemCount;

                //记录一次消费的状态
                var _currentQueueStats = new QueueStats();
                _currentQueueStats.QueueName          = _queueSettings.QueueName;
                _currentQueueStats.ConsumerThreadName = Thread.CurrentThread.Name;

                var _listConsumerItem = new List <Object>(_consumerItemCount);
                try
                {
                    var _queueCount = _queue.Count;

                    _currentQueueStats.CurrentQueueCount = _queueCount;
                    lock (_queue)
                    {
                        //批量消费策略
                        if (_queueCount > 0)
                        {
                            if (_queueCount >= _consumerItemCount)
                            {
                                for (var i = 0; i < _consumerItemCount; i++)
                                {
                                    var v = _queue.Dequeue();
                                    if (v != null)
                                    {
                                        _listConsumerItem.Add(v);
                                    }
                                }
                            }
                            else
                            {
                                //TODO:生产不足策略:等待数据到来,或消费掉不足的所有数据
#if DEBUG
                                YmatouLoggingService.Debug("生产数据不足:{0},{1},{2}", _queueSettings.QueueName, _consumerItemCount, _queueCount);
#endif
                                for (var i = 0; i < _queueCount; i++)
                                {
                                    var v = _queue.Dequeue();
                                    if (v != null)
                                    {
                                        _listConsumerItem.Add(v);
                                    }
                                }
                            }
                        }
                    }
                    if (_listConsumerItem.Count > 0)
                    {
                        //单个消费
                        //var item = _queue.Dequeue();
                        try
                        {
                            //监控
                            var _watch = Stopwatch.StartNew();
                            //客户端消费数据
                            try
                            {
                                YmatouLoggingService.Debug("进行一次消费 {0}-> {1} 项", _queueSettings.QueueName, _listConsumerItem.Count);
                                _queueSettings.ConsumerAction(_listConsumerItem);
                            }
                            catch (QueueException ce)
                            {
                                _currentQueueStats.ClientErrorMessage = ce.Message;
                                _currentQueueStats.ClientErrorTime    = DateTime.Now;
                                YmatouLoggingService.Debug("客户端消费错误:{0},{1},{2}", _queueSettings.QueueName, ce.Message, ce.StackTrace);
                            }
                            catch (Exception e)
                            {
                                _currentQueueStats.ClientErrorMessage = e.Message;
                                _currentQueueStats.ClientErrorTime    = DateTime.Now;
                                YmatouLoggingService.Debug("客户端消费错误:{0},{1},{2}", _queueSettings.QueueName, e.Message, e.StackTrace);
                            }
                            _currentQueueStats.ConsumerUseTime = _watch.ElapsedMilliseconds;
                            _watch.Stop();

                            //一次消费完成,状态跟踪
                            //统计总共消费
                            //Interlocked.Add(ref _beenConsumedCount, _listConsumerItem.Count);
                            var _listQv = _listConsumerItem.ConvertAll <QueueValue <T> >(e => { return(e as QueueValue <T>); });
                            if (_listQv.Count > 0)
                            {
                                _currentQueueStats.SuccessConsumedCount = _listQv.Count(e => e != null && e.ConsumerResult == QueueItemConsumerStats.Ok);

                                _currentQueueStats.FailConsumedCount        = _listQv.Count(e => e != null && e.ConsumerResult != QueueItemConsumerStats.Ok);
                                _currentQueueStats.CurrentBeenConsumedCount = _listConsumerItem.Count;
                                //一次消费完成,对消费失败的数据再处理
                                if (_queueSettings.ConsumerFS != ConsumerFailStrategy.ClientHandleFail)
                                {
                                    if (_queueSettings.ConsumerFS == ConsumerFailStrategy.Delet)
                                    {
                                    }
                                    else if (_queueSettings.ConsumerFS == ConsumerFailStrategy.PersistenceEnqueue)
                                    {
                                    }
                                    else
                                    {
                                        var _listFailItem = _listQv.Where(e => e != null && e.ConsumerResult != QueueItemConsumerStats.Ok && e.ConsumerCount <= _consumerFailCount);
                                        foreach (var item in _listFailItem)
                                        {
                                            var _errorCount = item.ConsumerCount;
                                            Interlocked.Add(ref _errorCount, 1);
                                            item.ConsumerCount = _errorCount;
                                            if (item.ConsumerCount < _consumerFailCount)
                                            {
                                                _queue.Enqueue(item);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (QueueException e)
                        {
#if !DEBUG
                            QueueLogs.ErrorFormat("队列服务错误,{0},{1},{2}", _queueSettings.QueueName, e.Message, e.StackTrace);
#endif
                            _currentQueueStats.ErrorTime    = DateTime.Now;
                            _currentQueueStats.ErrorMessage = e.Message;
                            Interlocked.Add(ref _errorCount, 1);
                            _currentQueueStats.ConsumerErrorItemCount = _errorCount;
                            var _listQv = _listConsumerItem.ConvertAll <QueueValue <T> >(_ => { return(_ as QueueValue <T>); });
                            foreach (var item in _listQv)
                            {
                                _queue.Enqueue(item);
                            }
                        }
                    }
                }
                catch (QueueException ex)
                {
#if !DEBUG
                    QueueLogs.ErrorFormat("客户端消费错误2:{0},{1},{2}", _queueSettings.QueueName, ex.Message, ex.StackTrace);
#endif
                    _currentQueueStats.ErrorMessage += "" + ex.Message;
                    _currentQueueStats.ErrorTime     = DateTime.Now;
                }
                finally
                {
                    _currentQueueStats.NextConsumerTime = DateTime.Now.Add(_consumerTime);
#if DEBUGs
                    BlockingBoundedQueueStatsHelp.QueueStatsLogs(_currentQueueStats);
#endif

                    Thread.Sleep(_consumerTime);
                }
            }
        }
        public BlockingBoundedQueueWrapper <T> Init(Action <List <Object> > consumerAction, string queueName, BlockingBoundedQueueSettings queueSettings, bool isStart = true)
        {
            if (string.IsNullOrEmpty(queueName))
            {
                throw new ArgumentNullException("队列名称不能空");
            }
            if (!_isInit)
            {
                lock (_lockInit)
                {
                    if (!_isInit)
                    {
                        YmatouLoggingService.Debug("开始初始化队列,{0}", queueName);
                        if (consumerAction == null)
                        {
                            throw new ArgumentException("必须提供消费回调方法");
                        }
                        if (queueSettings == null)
                        {
                            _queueSettings = new BlockingBoundedQueueSettings
                            {
                                ConsumerAction      = consumerAction,
                                ConsumerThreadCount = Environment.ProcessorCount,
                                ConsumerTime        = new TimeSpan(0, 0, 0, 0, 10),
                                QueueName           = queueName,
                                ConsumerItemCount   = 3,
                                QueueItemMaxCount   = 100000,
                                ConsumerFS          = ConsumerFailStrategy.AgainEnqueue
                            };
                        }
                        else
                        {
                            if (queueSettings.ConsumerThreadCount <= 0 || queueSettings.ConsumerThreadCount > Environment.ProcessorCount)
                            {
                                queueSettings.ConsumerThreadCount = Environment.ProcessorCount;
                            }
                            if (queueSettings.ConsumerTime < new TimeSpan(0, 0, 0, 10))
                            {
                                queueSettings.ConsumerTime = new TimeSpan(0, 0, 0, 0, 10);
                            }
                            if (queueSettings.ConsumerItemCount <= 0)
                            {
                                queueSettings.ConsumerItemCount = 1;
                            }
                            if (queueSettings.QueueItemMaxCount > 100000 || queueSettings.QueueItemMaxCount <= 0)
                            {
                                queueSettings.QueueItemMaxCount = 100000;
                            }
                            if (string.IsNullOrEmpty(queueSettings.QueueName))
                            {
                                queueSettings.QueueName = queueName;
                            }
                            if (queueSettings.ConsumerAction == null)
                            {
                                queueSettings.ConsumerAction = consumerAction;
                            }

                            _queueSettings = queueSettings;
                        }
                        _thList = new List <Thread>();
                        _queue  = new BlockingBoundedQueue <QueueValue <T> >(_queueSettings.QueueItemMaxCount);
                        if (isStart)
                        {
                            this.Start();
                            BQStatus = Status.RunIng;
                        }
                        else
                        {
                            BQStatus = Status.Stop;
                        }
                        _isInit = true;
                        YmatouLoggingService.Debug("Queue Service Version {0}", version);
                        YmatouLoggingService.Debug("结束初始化队列,{0}", queueName);
                    }
                }
            }
            return(this);
        }
示例#28
0
        private void Consumer()
        {
            while (PQStatus == Status.RunIng)
            {
                try
                {
                    var itemCount = PQ.Count;
                    if (itemCount > 0)
                    {
                        var list = new List <Object>();
                        lock (PQ)
                        {
                            //启用了消费阀值 && 当前队列项数大于等于消费阀值
                            if (pqSetting.ConsumerItemCount > 0 && itemCount >= pqSetting.ConsumerItemCount)
                            {
                                for (var i = 0; i < pqSetting.ConsumerItemCount; i++)
                                {
                                    var v = PQ.Dequeue().Value;
                                    if (v != null)
                                    {
                                        list.Add(v);
                                    }
                                }
                            }
                            else if (itemCount < pqSetting.ConsumerItemCount)
                            {
                                for (var i = 0; i < itemCount; i++)
                                {
                                    var v = PQ.Dequeue().Value;
                                    if (v != null)
                                    {
                                        list.Add(v);
                                    }
                                }
                            }
                        }
                        if (list.Count > 0)
                        {
                            var _watch = Stopwatch.StartNew();
                            YmatouLoggingService.Debug("开始消费,qNmame->{0},itemCount->{1}", pqSetting.QName, list.Count);
                            try
                            {
                                //lock(list)
                                pqSetting.ConsumeAction(list);
                            }
                            catch (QueueException e)
                            {
                                YmatouLoggingService.Debug(string.Format("PQService客户端消费错误,pqName->{0},msg->{1},stack->{2}", pqSetting.QName, e.Message, e.StackTrace));
                            }
                            YmatouLoggingService.Debug("结束消费,qNmame->{0},itemCount->{1},runTime->{2} ms", pqSetting.QName, list.Count, _watch.ElapsedMilliseconds);
                            _watch.Reset();
                            var _listQv = list.ConvertAll <PQValue <K, V> >(e => { return(e as PQValue <K, V>); });
                            if (_listQv != null && _listQv.Count > 0)
                            {
                                var consumerNotCount     = _listQv.Where(e => e != null && e.ConsumerResult == QueueItemConsumerStats.NoConsumer);
                                var consumerFailCount    = _listQv.Where(e => e != null && e.ConsumerResult == QueueItemConsumerStats.Fail);
                                var tmpConsumerNotCount  = consumerNotCount.Count();
                                var tmpconsumerFailCount = consumerFailCount.Count();
                                var consumerOkCount      = _listQv.Count - tmpConsumerNotCount - tmpconsumerFailCount;
#if DEBUG
                                YmatouLoggingService.Debug("消费结果:总共->{0},未消费->{1},成功->{2},失败->{3}", list.Count, tmpConsumerNotCount, consumerOkCount, tmpconsumerFailCount);
#else
                                QueueLogs.DebugFormat("消费结果:总共->{0},未消费->{1},成功->{2},失败->{3}", list.Count, tmpConsumerNotCount, consumerOkCount, tmpconsumerFailCount);
#endif


                                if (tmpConsumerNotCount > 0)
                                {
                                    foreach (var o in consumerNotCount)
                                    {
                                        PQ.Enqueue(o.Key, o);
                                    }
                                }

                                if (tmpconsumerFailCount > 0)
                                {
                                    if (pqSetting.CFS == ConsumerFailStrategy.Delet)
                                    {
                                    }
                                    else if (pqSetting.CFS == ConsumerFailStrategy.PersistenceEnqueue)
                                    {
                                    }
                                    else if (pqSetting.CFS == ConsumerFailStrategy.ClientHandleFail)
                                    {
                                    }
                                    else if (pqSetting.CFS == ConsumerFailStrategy.AgainEnqueue)
                                    {
                                        foreach (var o in consumerFailCount)
                                        {
                                            //消费失败次数累计
                                            var tmpCount = o.ConsumerCount;
                                            Interlocked.Add(ref tmpCount, 1);
                                            o.ConsumerCount = tmpCount;
                                            if (o.ConsumerCount <= 3)
                                            {
                                                PQ.Enqueue(o.Key, o);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (QueueException ex)
                {
                    YmatouLoggingService.Error(string.Format("PQService 服务端错误,pqName->{0},msg->{1},stack->{2}", pqSetting.QName, ex.Message, ex.StackTrace));
                }
                finally
                {
                    Thread.Sleep(pqSetting.ScanningTime);
                }
            }
        }