示例#1
0
        /// <summary>
        /// 将单个数据插入队列
        /// </summary>
        /// <param name="item"></param>
        public void Enqueue(SimpleQueueData <T> item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }
            if (item.Data == null)
            {
                throw new ArgumentNullException($"{nameof(item)}.{nameof(item.Data)}");
            }

            ExceptionHelper.TryCatchFinal(() =>
            {
                if (_options.QueueTriggerType == QueueTriggerType.Immediate)
                {
                    // 不走队列,直接处理数据
                    ExecuteConsume(() =>
                    {
                        Consume(new List <SimpleQueueData <T> > {
                            item
                        }, QueueTriggerType.Immediate);
                    }, false);
                    return;
                }

                _queue.Enqueue(item);

                if ((_options.QueueTriggerType & QueueTriggerType.Count) == QueueTriggerType.Count && Count >= _options.CountLimit)
                {
                    // 数量达到上限
                    ExecuteConsume(() =>
                    {
                        ExecuteConsume(Math.Max(1, _options.CountLimit), QueueTriggerType.Count);
                    });
                }
            }, exception =>
            {
                if (OnException != null)
                {
                    OnException(this, new EventArgs <Exception>(exception));
                    return(false);
                }
                return(true);
            });
        }
示例#2
0
 public DataConsumedEventArgs(SimpleQueueData <T> queueData, QueueTriggerType triggerType)
 {
     QueueData   = queueData;
     TriggerType = triggerType;
 }