.net 消息队列MSMQ

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
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值