.net msmq消息隊(duì)列實(shí)例詳解
本文為大家分享了.net msmq消息隊(duì)列實(shí)例代碼,供大家參考,具體內(nèi)容如下
1.msmq消息隊(duì)列windows環(huán)境安裝
控制面板----》程序和功能----》啟用或關(guān)閉Windows程序----》Microsoft Message Queue(MSMQ)服務(wù)器
選中如圖所示功能點(diǎn)擊“確認(rèn)”進(jìn)行安裝,安裝好后可在 “計(jì)算機(jī)管理”中進(jìn)行查看
2.創(chuàng)建消息隊(duì)列實(shí)體對(duì)象
/// <summary> /// 消息實(shí)體 /// </summary> [Serializable] public class MsmqData { public int Id { get; set; } public string Name { get; set; } }
實(shí)體對(duì)象必須可序列化,即需添加[Serializable]
3.創(chuàng)建消息隊(duì)列管理對(duì)象
/// <summary> /// 消息隊(duì)列管理對(duì)象 /// </summary> public class MSMQManager { /// <summary> /// 消息隊(duì)列地址 /// </summary> public string _path; /// <summary> /// 消息隊(duì)列對(duì)象 /// </summary> public MessageQueue _msmq; /// <summary> /// 構(gòu)造函數(shù)并初始化消息隊(duì)列對(duì)象 /// </summary> /// <param name="path"></param> public MSMQManager(string path = null) { if (string.IsNullOrEmpty(path)) { _path = ConfigurationManager.AppSettings["MsmqPath"].ToString(); } else { _path = path; } if (MessageQueue.Exists(_path)) { _msmq = new MessageQueue(_path); } else { _msmq = MessageQueue.Create(_path); } } /// <summary> /// 發(fā)送消息隊(duì)列 /// </summary> /// <param name="body"></param> public void Send(object body) { _msmq.Send(new Message(body, new XmlMessageFormatter(new Type[] { typeof(MsmqData) }))); } /// <summary> /// 接受隊(duì)列中第一個(gè)消息后刪除 /// </summary> /// <returns></returns> public object ReceiveMessage() { var msg = _msmq.Receive(); if (msg != null) { //msg.Formatter = new BinaryMessageFormatter(); msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(MsmqData) }); var body = (MsmqData)msg.Body; Console.WriteLine("消息內(nèi)容:{0},{1}", body.Id, body.Name); return msg.Body; } return null; } /// <summary> /// 遍歷消息隊(duì)列中的消息并刪除 /// </summary> public void WriteAllMessage() { var enumerator = _msmq.GetMessageEnumerator2(); while (enumerator.MoveNext()) { Message msg = (Message)(enumerator.Current); //msg.Formatter = new BinaryMessageFormatter(); msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(MsmqData) }); var body = (MsmqData)msg.Body; Console.WriteLine("消息內(nèi)容:{0},{1}", body.Id, body.Name); //根據(jù)消息ID查詢并刪除消息隊(duì)列 _msmq.ReceiveById(msg.Id); } } }
此例中使用XML格式(XmlMessageFormtter)對(duì)消息進(jìn)行格式化
4.主程序添加調(diào)用消息隊(duì)列
static void Main(string[] args) { var msmqManager = new MSMQManager(); for (int i = 1; i <= 10; i++) { MsmqData data = new MsmqData() { Id = i, Name = string.Format("Name{0}", i) }; //發(fā)送消息 msmqManager.Send(data); } var msg = msmqManager.ReceiveMessage(); msmqManager.WriteAllMessage(); Console.ReadLine(); }
添加消息隊(duì)列地址配置,本例使用私有隊(duì)列
<appSettings> <add key="MsmqPath" value=".\private$\myQueue"/> </appSettings>
5.運(yùn)行程序查看結(jié)果
可以在發(fā)送完消息后打上斷點(diǎn)查看消息隊(duì)列消息正文
最后運(yùn)行結(jié)果
6.常見消息隊(duì)列類型路徑的語法
隊(duì)列類型
路徑中使用的語法
公共隊(duì)列
MachineName\QueueName
專用隊(duì)列
MachineName\Private$\QueueName
日志隊(duì)列
MachineName\QueueName\Journal$
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery+Asp.Net實(shí)現(xiàn)省市二級(jí)聯(lián)動(dòng)功能的方法
這篇文章主要介紹了jQuery+Asp.Net實(shí)現(xiàn)省市二級(jí)聯(lián)動(dòng)功能的方法,涉及asp.net數(shù)據(jù)庫讀取與字符串轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-05-05asp.net+ajax的Post請(qǐng)求實(shí)例
這篇文章主要介紹了asp.net+ajax的Post請(qǐng)求實(shí)現(xiàn)方法,實(shí)例分析了Ajax的發(fā)送post數(shù)據(jù)的原理與技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01ASP.NET Core MVC/WebApi基礎(chǔ)系列1
這篇文章主要介紹了ASP.NET Core MVC/WebApi基礎(chǔ)系列,后續(xù)會(huì)穿插講EF Core和ASP.NET Core,雖說是基礎(chǔ)系列但也是也有你不知道的。2019-04-04.net中string類型可以作為lock的鎖對(duì)象嗎
lock 關(guān)鍵字是用于在多線程編程中實(shí)現(xiàn)同步和互斥訪問的關(guān)鍵字,它的作用是確保共享資源在任意時(shí)刻只能被一個(gè)線程訪問,從而避免出現(xiàn)競(jìng)態(tài)條件(race condition)和數(shù)據(jù)不一致的問題,這篇文章主要介紹了string類型可以作為lock的鎖對(duì)象嗎,需要的朋友可以參考下2023-06-06ASP.NET中實(shí)時(shí)圖表的實(shí)現(xiàn)方法分享
這篇文章介紹了ASP.NET中實(shí)時(shí)圖表的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-11-11