MSMQ 微软消息队列 示例

MSMQ它的实现原理是:消息的发送者把自己想要发送的信息放入一个容器中(我们称之为Message),然后把它保存至一个系统公用空间的消息队列(Message Queue)中;本地或者是异地的消息接收程序再从该队列中取出发给它的消息进行处理。

我个人的理解,你可以把他当做一种,把数据打包后,发送到一个地方,程序也可以去取到这个打包的程序,队列的机制就不讲了,并发问题荡然无存。呵呵。

上代码:

首先using System.Messaging;

    public class MsmqManagerHelper
    {
        private readonly string _path;
        private MessageQueue _msmq;
        public MsmqManagerHelper()
        {
            _path = @".\private$\给服务实例起个名字";  //这是本机实例的方式
            if (!MessageQueue.Exists(_path))
            {
                MessageQueue.Create(_path);
            }
            _msmq = new MessageQueue(_path);
        }

        /// <summary>
        /// 发送消息队列
        /// </summary>
        /// <param name="msmqIndex">消息队列实体</param>
        /// <returns></returns>
        public void Send(object msmqIndex)
        {
            _msmq.Send(new Message(msmqIndex, new BinaryMessageFormatter()));
        }

        /// <summary>
        /// 接收消息队列,删除队列
        /// </summary>
        /// <returns></returns>
        public object ReceiveAndRemove()
        {
            object msmqIndex = null;
            _msmq.Formatter = new BinaryMessageFormatter();
            Message msg = null;
            try
            {
                msg = _msmq.Receive(new TimeSpan(0, 0, 1));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                //做日志记录和发送邮件报警
            }
            if (msg != null)
            {
                msmqIndex = msg.Body;
            }
            return msmqIndex;
        }
        

        /// <summary>
        /// 释放消息队列实例
        /// </summary>
        public void Dispose()
        {
            if (_msmq != null)
            {
                _msmq.Close();
                _msmq.Dispose();
                _msmq = null;
            }
        }
    }

 

以上是一个helper类,下面介绍调用方法。

    class Program
    {
        static void Main()
        {
            var mmh = new MsmqManagerHelper();
            Console.WriteLine("测试MSMQ工作");
            Console.WriteLine("发送消息");
            string receiveKey = Console.ReadLine();
            try
            {
                    var m=new Model();
                    m.ID = 1;
                    m.Name = receiveKey;
                    mmh.Send(m);      
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
            Console.WriteLine("开始接受消息...");
            while (true)
            {
                object obj = mmh.ReceiveAndRemove();
                if (obj != null)
                {
                    var item = (Model) obj;
                    Console.WriteLine(item.ID+ ":" + item.Name);
                }
                else
                {
                    break;
                }
                Thread.Sleep(200);
            }
            Console.ReadLine();
        }
    }

posted on 2012-08-16 18:24  张燕伟  阅读(3220)  评论(0编辑  收藏  举报