Пример #1
0
 private void AfterDequeueEvent(IQueueMini queueMini, object value)
 {
     if (this.Length == 0)
     {
         _waitEnqueue.Reset();
     }
 }
Пример #2
0
        private static bool BeforDequeue(IQueueMini queueMini)
        {
            if (queueMini.Length == 0)
            {
                throw new InvalidOperationException("队列长度为0");
            }

            //TaskQueue taskQueue = queueMini.Peek() as TaskQueue;

            //if (taskQueue == null)
            //{
            //    throw new Exception("转化为TaskQueue实体失败");
            //}

            //using (var db = new MQS_DBEntities())
            //{
            //    db.Configuration.AutoDetectChangesEnabled = false;
            //    db.Configuration.ValidateOnSaveEnabled = false;

            //    int successCount = db.Database.ExecuteSqlCommand(string.Format("update dbo.TaskQueues  set status=2  where taskQueueId={0}", taskQueue.TaskQueueId));

            //    if (successCount == 0)
            //    {
            //        throw new Exception("出队失败,数据库找不到该id:" + taskQueue.TaskQueueId);
            //    }
            //}

            return(true);
        }
Пример #3
0
        private static bool BeforEnqueue(IQueueMini queueMini, object value)
        {
            TaskQueue taskQueue = value as TaskQueue;

            if (taskQueue == null)
            {
                throw new Exception("转化为TaskQueue实体失败");
            }

            using (var db = new MQS_DBEntities())
            {
                db.Configuration.AutoDetectChangesEnabled = false;
                db.Configuration.ValidateOnSaveEnabled    = false;

                string sql;

                if (Initialized)
                {
                    sql = string.Format("update dbo.TaskQueues set status=1 where status=0 and taskQueueId={0}", taskQueue.TaskQueueId);
                }
                else
                {
                    sql = string.Format("update dbo.TaskQueues set status=1 where taskQueueId={0}", taskQueue.TaskQueueId);
                }

                int successCount = db.Database.ExecuteSqlCommand(sql);

                if (successCount == 0)
                {
                    throw new Exception("入队失败,数据库找不到该id:" + taskQueue.TaskQueueId);
                }
            }
            return(true);
        }
Пример #4
0
        public bool WaitEnqueue(string queueName, int timeOut)
        {
            const int maxTimeOut = 60 * 60 * 1000;

            if (timeOut > maxTimeOut)
            {
                timeOut = maxTimeOut;
            }

            try
            {
                IQueueMini queueMini = GetQueueMiniByName(queueName);

                if (queueMini == null)
                {
                    queueMini = CreatQueueMini(queueName);
                    QueueMinis.Add(queueMini);
                }

                var value = queueMini.WaitEnqueue(timeOut);

                return(value);
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
                return(false);
            }
        }
Пример #5
0
        public QueueModel QueueLength(string queueName)
        {
            try
            {
                IQueueMini queueMini = GetQueueMiniByName(queueName);

                if (queueMini == null)
                {
                    throw new InvalidOperationException("队列不存在");
                }

                return(new QueueModel()
                {
                    Value = queueMini.Length,
                });
            }
            catch (InvalidOperationException ex)
            {
                return(new QueueModel()
                {
                    Value = null,
                    ErrorMessage = ex.ToString(),
                });
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
                return(new QueueModel()
                {
                    Value = null,
                    ErrorMessage = ex.ToString(),
                });
            }
        }
Пример #6
0
        private static void AfterDequeue(IQueueMini queueMini, object value)
        {
            try
            {
                if (value == null)
                {
                    throw new Exception("出队内容为null");
                }

                TaskQueue taskQueue = value as TaskQueue;

                if (taskQueue == null)
                {
                    throw new Exception("出队时转化为TaskQueue实体失败");
                }

                using (var db = new MQS_DBEntities())
                {
                    db.Configuration.AutoDetectChangesEnabled = false;
                    db.Configuration.ValidateOnSaveEnabled    = false;

                    int successCount = db.Database.ExecuteSqlCommand(string.Format("update dbo.TaskQueues  set status=2  where taskQueueId={0}", taskQueue.TaskQueueId));

                    if (successCount == 0)
                    {
                        throw new Exception("出队失败,数据库找不到该id:" + taskQueue.TaskQueueId);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);

                if (value != null)
                {
                    bool reTrySuccess = Convert.ToBoolean(EnqueueStatic(queueMini.Name, value.ToString()).Value);

                    if (!reTrySuccess)
                    {
                        Log.Error("尝试重复入队失败");
                    }
                }
            }
        }
Пример #7
0
        private static QueueModel EnqueueStatic(string queueName, string value)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    throw new ArgumentNullException("value");
                }

                TaskQueue taskQueue = JsonConvert.DeserializeObject <TaskQueue>(value);

                if (taskQueue == null)
                {
                    throw new Exception("反序列化为TaskQueue失败");
                }

                IQueueMini queueMini = GetQueueMiniByName(queueName);

                if (queueMini == null)
                {
                    queueMini = CreatQueueMini(queueName);

                    QueueMinis.Add(queueMini);
                }

                queueMini.Enqueue(taskQueue);

                return(new QueueModel()
                {
                    Value = true,
                });
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
                return(new QueueModel()
                {
                    Value = false,
                    ErrorMessage = ex.Message,
                });
            }
        }
Пример #8
0
        public QueueModel Dequeue(string queueName)
        {
            try
            {
                IQueueMini queueMini = GetQueueMiniByName(queueName);

                if (queueMini == null)
                {
                    throw new InvalidOperationException("队列名不存在");
                }

                var value = queueMini.Dequeue();

                return(new QueueModel()
                {
                    Value = JsonConvert.SerializeObject(value),
                    Status = 1,
                });
            }
            catch (InvalidOperationException)
            {
                return(new QueueModel()
                {
                    Value = null,
                    ErrorMessage = "队列为空或不存在",
                    Status = 10,
                });
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
                return(new QueueModel()
                {
                    Value = null,
                    ErrorMessage = ex.Message,
                });
            }
        }
Пример #9
0
 private void AfterEnqueueEvent(IQueueMini queueMini, object value)
 {
     _waitEnqueue.Set();
 }