Next, you can set the number of concurrent versions.

Source: Internet
Author: User

Some minor optimizations were made and concurrency control was added:

    public abstract class MessageQueueConcurrentHandlerBase<T> : IMessageQueueHandler    {        public MessageQueueConcurrentHandlerBase(string queueName, int maxConcurrency, Action<string> logDelegate)        {            if (!MessageQueue.Exists(queueName))                throw new Exception(string.Format("No such a queue: {0}", queueName));            if (maxConcurrency < 1)                throw new ArgumentOutOfRangeException("maxConcurrency");            this._queueName = queueName;            this._poolForConsumer = new Semaphore(0, maxConcurrency);            this._producerAutoResetEvent = new AutoResetEvent(false);            this._maxConcurrency = maxConcurrency;            this._logDelegate = logDelegate;        }        public void StartRead()        {            this._queue = new MessageQueue(this._queueName) { Formatter = new XmlMessageFormatter(new Type[] { typeof(long) }) };            this._queue.PeekCompleted += new PeekCompletedEventHandler(Produce);            this._producerAutoResetEvent.Set();            this._poolForConsumer.Release(this._maxConcurrency);            this._queue.BeginPeek();        }        public override string ToString()        {            return string.Format("{0}_{1}", this._queueName, this.ProcessName);        }        public int WorkerCount { get { return Thread.VolatileRead(ref this._workerCount); } }        public int MaxConcurrency { get { return _maxConcurrency; } }        protected abstract string ProcessName { get; }        protected abstract void MainProcess(T backThreadId);        protected Action<string> LogDelegate { get { return _logDelegate; } }        #region private        private void Produce(object sender, PeekCompletedEventArgs e)        {            this._producerAutoResetEvent.WaitOne();            var message = this._queue.EndPeek(e.AsyncResult);            ThreadPool.QueueUserWorkItem(new WaitCallback(this.Consume));            this._queue.BeginPeek();        }        private void Consume(object stateInfo)        {            this._poolForConsumer.WaitOne();            var message = this._queue.Receive();            this._producerAutoResetEvent.Set();            T messageItem = (T)message.Body;            this.LogDelegate(string.Format("{0} - Received a message, MessageItem = {1}", this.ProcessName, messageItem));            Interlocked.Increment(ref this._workerCount);            try            {                this.LogDelegate(string.Format("{0} - Running - {1}, WorkerCount = {2}", this.ProcessName, messageItem, this._workerCount));                MainProcess(messageItem);            }            catch (Exception ex)            {                this.HandleException(ex, messageItem);            }            finally            {                Interlocked.Decrement(ref this._workerCount);                this.LogDelegate(string.Format("{0} - Over - {1}, WorkerCount = {2}", this.ProcessName, messageItem, this._workerCount));            }            this._poolForConsumer.Release();        }        private void HandleException(Exception ex, T messageItem)        {            this.LogDelegate(string.Format("Exception in {0}:[Message]={1},[StackTrace]={2},[Type]={3},[_workerCount]={4},[backThreadId]={5}", this.ProcessName, ex.Message, ex.StackTrace, ex.GetType(), this.WorkerCount, messageItem));        }        private readonly string _queueName;        private MessageQueue _queue;        private int _workerCount;        private Semaphore _poolForConsumer;        private AutoResetEvent _producerAutoResetEvent;        private int _maxConcurrency;        private Action<string> _logDelegate;        #endregion    }

I would like to praise it if I think it is good :)

Next, you can set the number of concurrent versions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.