Beispiel #1
0
        public DefaultParallelQueueConsumer(
            ParallelQueueConsumerOptions <TEntity, TState> options,
            IServiceProvider serviceProvider,
            string namePrefix,
            TState state,
            ILoggerFactory loggerFactory)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            _options         = options;
            _loggerFactory   = loggerFactory;
            _serviceProvider = serviceProvider;
            _namePrefix      = namePrefix;
            _state           = state;

            int executorCount = _options.ExecutorCount;

            if (_options.ExecutorCount < 1)
            {
                executorCount = 1;
            }

            var _logger = _loggerFactory.CreateLogger <ParallelQueueExecutor <TEntity, TState> >();

            List <BlockingCollection <TEntity> > queueList = new List <BlockingCollection <TEntity> >();

            for (int i = 0; i < executorCount; i++)
            {
                string name     = $"{namePrefix}{i}";
                var    executor = new ParallelQueueExecutor <TEntity, TState>(
                    _serviceProvider,
                    _options,
                    _logger,
                    name,
                    state);
                _executorList.Add(executor);
                queueList.Add(executor.Queue);
            }
            _queueList = queueList.ToArray();
        }
        public ParallelQueueExecutor(
            IServiceProvider serviceProvider,
            ParallelQueueConsumerOptions <TEntity, TState> options,
            ILogger <ParallelQueueExecutor <TEntity, TState> > logger,
            string name,
            TState state)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            if (options.ExecuteDelegate == null)
            {
                throw new ArgumentNullException(nameof(options.ExecuteDelegate));
            }
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            _serviceProvider = serviceProvider;

            int capacity = options.ExecutorQueueCapacity;

            if (capacity <= 0)
            {
                capacity = 1;
            }

            IProducerConsumerCollection <TEntity> innerQueue = null;

            switch (options.ExecutorQueueType)
            {
            case QueueType.FIFO:
                innerQueue = new ConcurrentQueue <TEntity>();
                break;

            case QueueType.LIFO:
                innerQueue = new ConcurrentStack <TEntity>();
                break;

            case QueueType.Bag:
                innerQueue = new ConcurrentBag <TEntity>();
                break;

            default:
                innerQueue = new ConcurrentQueue <TEntity>();
                break;
            }

            Queue   = new BlockingCollection <TEntity>(innerQueue, capacity);
            Options = options;
            Name    = name;
            Counter = 0;
            State   = state;
            _logger = logger;

            var period = options.StatisticalPeriod;

            if (period.TotalSeconds < 10)
            {
                period = TimeSpan.FromSeconds(10);
            }
            // 最长时间1小时
            if (period.TotalHours > 1)
            {
                period = TimeSpan.FromHours(1);
            }
            StatisticalPeriod = period;
        }