Microsoft Message Queuing

Microsoft Message Queuing或MSMQ微軟公司實現的一種訊息佇列，始於Windows NT 4與Windows 95。Windows Server 2016與Windows 10仍然包含這種元件。1999年起，Microsoft Embedded平臺以及Windows CE 3.0也開始支援這一元件。簡介.MSMQ作為一種訊息協議，允許多伺服器/多程序通訊，即使不總是保持互聯。而sockets與其他網路協議要求直連總是成立。MSMQ從1997年開始可用。MSMQ是可靠分發訊息。分發失敗的訊息儲存在佇列中直到目標可達時重發該訊息。還支援安全與優先順序的訊息機制。可以建立用於除錯。MSMQ支援可持續性與不可持續性訊息，使得效能與訊息是否寫到磁碟的一致性上可以權衡。不可持續性訊息只能用於向非事務性佇列傳送快速訊息。MSMQ支援事務處理。允許多個動作發給多個佇列中包裝為單個事務。微軟分散式事務協調器 (MSDTC) 支援對MSMQ或其他資源的事務訪問。MSMQ使用下述埠:使用.C#例子：using System;using System.Collections.Generic;using System.Linq;using System.Messaging;using System.Text;using System.Threading.Tasks;namespace Testpublic class QueueManger////// 建立MSMQ佇列/////////public static void Createqueue(string queuePath, bool transactional = false)try//判斷佇列是否存在if (!MessageQueue.Exists(queuePath))MessageQueue.Create(queuePath);Console.WriteLine(queuePath + "已成功建立！");elseConsole.WriteLine(queuePath + "已經存在！");catch (MessageQueueException e)Console.WriteLine(e.Message);////// 刪除佇列//////public static void Deletequeue(string queuePath)try//判斷佇列是否存在if (MessageQueue.Exists(queuePath))MessageQueue.Delete(@".\private$\myQueue");Console.WriteLine(queuePath + "已刪除！");elseConsole.WriteLine(queuePath + "不存在！");catch (MessageQueueException e)Console.WriteLine(e.Message);////// 傳送訊息//////////////////public static bool SendMessage(T target, string queuePath, MessageQueueTransaction tran = null)try//連線到本地的佇列MessageQueue myQueue = new MessageQueue(queuePath);System.Messaging.Message myMessage = new System.Messaging.Message();myMessage.Body = target;myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });//傳送訊息到佇列中if (tran == null)myQueue.Send(myMessage);elsemyQueue.Send(myMessage, tran);Console.WriteLine("訊息已成功傳送到"+queuePath + "佇列！");return true;catch (ArgumentException e)Console.WriteLine(e.Message);return false;////// 接收訊息////////////public static T ReceiveMessage(string queuePath,MessageQueueTransaction tran=null)//連線到本地佇列MessageQueue myQueue = new MessageQueue(queuePath);myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });try//從佇列中接收訊息System.Messaging.Message myMessage = tran == null ? myQueue.Receive() : myQueue.Receive(tran);return (T)myMessage.Body; //獲取訊息的內容catch (MessageQueueException e)Console.WriteLine(e.Message);catch (InvalidCastException e)Console.WriteLine(e.Message);return default(T);////// 採用Peek方法接收訊息////////////public static T ReceiveMessageByPeek(string queuePath)//連線到本地佇列MessageQueue myQueue = new MessageQueue(queuePath);myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });try//從佇列中接收訊息System.Messaging.Message myMessage = myQueue.Peek();return (T)myMessage.Body; //獲取訊息的內容catch (MessageQueueException e)Console.WriteLine(e.Message);catch (InvalidCastException e)Console.WriteLine(e.Message);return default(T);////// 獲取佇列中的所有訊息////////////public static List(string queuePath)MessageQueue myQueue = new MessageQueue(queuePath);myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });tryMessage[] msgArr= myQueue.GetAllMessages();List();msgArr.ToList().ForEach((o) =&gt;list.Add((T)o.Body);return list;catch(Exception e)Console.WriteLine(e.Message);return null;namespace Testpublic class Student////// 年齡///////// 姓名///namespace Testclass Programstatic void Main(string[] args)string queuepath = @".\private$\myQueue";//QueueManger.Createqueue(queuepath);//Student stu = new Student() { Name="shaoshun",Age=18};//QueueManger.SendMessage(stu, queuepath);//Student stu= QueueManger.ReceiveMessageByPeek(queuepath);//Student stu = QueueManger.ReceiveMessage(queuepath);//Console.WriteLine(stu.Name);QueueManger.Deletequeue(queuepath);QueueManger.Createqueue(queuepath);MessageQueueTransaction tran = new MessageQueueTransaction();tran.Begin();tryStudent stu;for (int i = 0; i = dwFormatNameBufferLength)wcsncpy_s(wszOutFormatName, *pdwOutFormatNameLength - 1, wszFormatNameBuffer, _TRUNCATE);// You must copy wszFormatNameBuffer into the// wszOutFormatName buffer.wszOutFormatName[*pdwOutFormatNameLength - 1] = L'\0';*pdwOutFormatNameLength = dwFormatNameBufferLength;elsewprintf(L"The queue was created, but its format name cannot be returned.\n");}return hr;