Click here to Skip to main content
15,896,502 members
Articles / Programming Languages / C# 4.0

Extended Thread Pool

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
6 Apr 2013Ms-PL3 min read 81.9K   1.8K   119  
Your own extensible and configurable Thread Pool.
using System;
using System.Messaging;
using log4net;

namespace Zulu.Threading.ThreadPools.TaskItems
{
    public abstract class TransactionalMsmqTaskItem<T> : ITaskItem
        where T : class
    {
        private readonly ILog _log;
        private readonly MessageQueue _queue;

        protected TransactionalMsmqTaskItem(MessageQueue queue)
        {
            if (queue == null)
            {
                throw new ArgumentNullException();
            }
            _queue = queue;
            _log = LogManager.GetLogger(GetType());
        }

        public void DoWork()
        {
            try
            {
                T message = null;
                TimeSpan messageReceiveTimeout = TimeSpan.FromMilliseconds(500);
                using (var transaction = new MessageQueueTransaction())
                {
                    try
                    {
                        transaction.Begin();
                        Message msmqMessage = _queue.Receive(messageReceiveTimeout, transaction);

                        if (msmqMessage != null)
                        {
                            message = msmqMessage.Body as T;

                            if (message == null)
                            {
                                string messageType = msmqMessage.Body != null
                                    ? msmqMessage.Body.GetType().Name
                                    : "<null>";
                                _log.Warn(
                                    string.Format(
                                        "Message of a wrong type was received (expected {0} but was {1}",
                                        typeof(T).Name,
                                        messageType));
                            }
                            else
                            {
                                Perform(message);
                            }
                        }

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Abort();
                        _log.Error(ex);
                        AbortDoWork(transaction, message);
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
            }
        }

        protected virtual void AbortDoWork(MessageQueueTransaction transaction, T message)
        {
            transaction.Abort();
        }

        protected abstract void Perform(T value);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
United States United States
B.Sc. in Computer Science.

Comments and Discussions