.net 消息队列MSMQ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
using System.Configuration;  


namespace Test
{
    ///
    /// 消息队列管理器
    ///

    public class MSMQManager:IDisposable where T : class, new()
    {
        #region 字段与属性
        private MessageQueue _msmq = null;
        private string _path;


        private static MSMQManager _instanceLocalComputer = new MSMQManager(true);
        ///
        /// 本机消息队列实例
        ///

        public static MSMQManager InstanceLocalComputer
        {
            get { return MSMQManager._instanceLocalComputer; }
        }


        private static MSMQManager _instance = new MSMQManager(false);
        ///
        /// 远程消息队列实例
        ///

        public static MSMQManager Instance
        {
            get { return MSMQManager._instance; }
        }
        #endregion


        ///
        /// 创建队列
        ///

        /// 是否启用事务
        ///
        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;
                }
            }
        }


        ///
        /// 实例化消息队列
        ///

        /// 是否为本机
        public MSMQManager(bool isLocalComputer)
        {
            if (isLocalComputer)
            {
                _path = @".\private$\MessageQueuen";
            }
            else
            {
                _path = @"FormatName:DIRECT=TCP:127.0.1.1\private$\MessageQueuen";
            }


            _msmq = new MessageQueue(_path);
        }




        ///
        /// 发送消息队列
        ///

        /// 消息队列实体
        ///
        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)
                {
                    //异常处理
                }
            }
        }


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

        ///
        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;
        }


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

        ///
        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 GetAllMessage()
        {
            _msmq.Formatter = new BinaryMessageFormatter();


            List msgList = new List();
            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;


        }


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



        public void ClearMessage()
        {
            _msmq.Purge();


        }


        #region IDisposable Members
        public void Dispose()
        {
            if (_msmq != null)
            {
                _msmq.Close();
                _msmq.Dispose();
                _msmq = null;
            }
        }




        #endregion
    }
}

你可能感兴趣的:(.Net)