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();
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM