您的位置:首页 > 其它

.net 消息队列MSMQ

2016-07-06 17:25 232 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Messaging;

using System.Configuration;  

namespace Test

{

    /// <summary>

    /// 消息队列管理器

    /// </summary>

    public class MSMQManager<T>:IDisposable where T : class, new()

    {

        #region 字段与属性

        private MessageQueue _msmq = null;

        private string _path;

        private static MSMQManager<T> _instanceLocalComputer = new MSMQManager<T>(true);

        /// <summary>

        /// 本机消息队列实例

        /// </summary>

        public static MSMQManager<T> InstanceLocalComputer

        {

            get { return MSMQManager<T>._instanceLocalComputer; }

        }

        private static MSMQManager<T> _instance = new MSMQManager<T>(false);

        /// <summary>

        /// 远程消息队列实例

        /// </summary>

        public static MSMQManager<T> Instance

        {

            get { return MSMQManager<T>._instance; }

        }

        #endregion

        /// <summary>

        /// 创建队列

        /// </summary>

        /// <param name="transactional">是否启用事务</param>

        /// <returns></returns>

        public bool Create(bool transactional)

        {

            if (MessageQueue.Exists(@".\private$\MessageQueuen" ))

            {

                return true;

            }

            else

            {

                if (MessageQueue.Create(@".\private$\MessageQueuen", transactional) != null)

                {

                    return true;

                }

                else

                {

                    return false;

                }

            }

        }

        /// <summary>

        /// 实例化消息队列

        /// </summary>

        /// <param name="isLocalComputer">是否为本机</param>

        public MSMQManager(bool isLocalComputer)

        {

            if (isLocalComputer)

            {

                _path = @".\private$\MessageQueuen";

            }

            else

            {

                _path = @"FormatName:DIRECT=TCP:127.0.1.1\private$\MessageQueuen";

            }

            _msmq = new MessageQueue(_path);

        }

        /// <summary>

        /// 发送消息队列

        /// </summary>

        /// <param name="msmqIndex">消息队列实体</param>

        /// <returns></returns>

        public void Send(T msg)

        {

            if (_msmq.Transactional)

            {

                MessageQueueTransaction myTransaction = new MessageQueueTransaction();

                try

                {

                    myTransaction.Begin();

                    _msmq.Send(new Message(msg, new BinaryMessageFormatter()), myTransaction);

                    myTransaction.Commit();

                }

                catch (Exception e)

                {

                    myTransaction.Abort();

                    //异常处理

                }

            }

            else

            {

                try

                {

                    _msmq.Send(new Message(msg, new BinaryMessageFormatter()));

                }

                catch (Exception e)

                {

                    //异常处理

                }

            }

        }

        /// <summary>

        /// 接收消息队列,删除队列

        /// </summary>

        /// <returns></returns>

        public T Receive()

        {

            T result = null;

            _msmq.Formatter = new BinaryMessageFormatter();

            Message msg = null;

            if (_msmq.Transactional)

            {

                MessageQueueTransaction myTransaction = new MessageQueueTransaction();

                try

                {

                    myTransaction.Begin();

                    msg = _msmq.Receive(new TimeSpan(0, 0, 3), myTransaction);

                    myTransaction.Commit();

                }

                catch (Exception e)

                {

                    myTransaction.Abort();

                    //异常处理

                }

            }

            else

            {

                try

                {

                    msg = _msmq.Receive(new TimeSpan(0, 0, 3));

                }

                catch (Exception ex)

                {

                    //异常处理

                }

            }

            if (msg != null)

            {

                result = msg.Body as T;

            }

            return result;

        }

        /// <summary>

        /// 接收消息队列,但不删除

        /// </summary>

        /// <returns></returns>

        public T Peek()

        {

            T result = null;

            _msmq.Formatter = new BinaryMessageFormatter();

            Message msg = null;

            try

            {

                msg = _msmq.Peek(new TimeSpan(0, 0, 3));

            }

            catch (Exception ex)

            {

                //异常处理

            }

            if (msg != null)

            {

                result = msg.Body as T;

            }

            return result;

        }

        public List<T> GetAllMessage()

        {

            _msmq.Formatter = new BinaryMessageFormatter();

            List<T> msgList = new List<T>();

            T model = null;

            Message[] allMessage = null;

            try

            {

                allMessage = _msmq.GetAllMessages();

                if (allMessage != null)

                {

                    for (int i = 0; i < allMessage.Length; i++)

                    {

                        model = new T();

                        model = allMessage[i].Body as T;

                        msgList.Add(model);

                    }

                }

            }

            catch (Exception e)

            {

                //异常处理

            }

            return msgList;

        }

        /// <summary>

        /// 清空指定队列的消息

        /// </summary>

        public void ClearMessage()

        {

            _msmq.Purge();

        }

        #region IDisposable Members

        public void Dispose()

        {

            if (_msmq != null)

            {

                _msmq.Close();

                _msmq.Dispose();

                _msmq = null;

            }

        }

        #endregion

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MSMQ