【轉】MSMQ 微軟訊息佇列 簡單 樣本

來源:互聯網
上載者:User

標籤:

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;            }        }    }
View Code

 

 

以上是一個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();        }    }
View Code

 

原文連結:http://www.cnblogs.com/isdavid/archive/2012/08/16/2642867.html

 

【轉】MSMQ 微軟訊息佇列 簡單 樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.