Try   HackMD

MSMQ Message Queuing

使用限制 : 傳送端與接收端都需要開啟MSMQ功能才能遠端傳送與接收。

WHAT IS MSMQ :

Def one

Message Queuing (MSMQ) technology enables applications running at different times to communicate across heterogeneous networks and systems that may be temporarily offline. Applications send messages to queues and read messages from queues.

Def two

MSMQ is essentially a messaging protocol that allows applications running on separate servers or processes(s) to communicate in a failsafe manner. A queue is a temporary storage location from which messages can be sent and received reliably. They can be used for communication across heterogeneous networks and between computers which may not always be connected.

Def three

訊息佇列 (也稱為 MSMQ) 是為 Microsoft Windows 建立分散式訊息應用程式的訊息基礎架構和開發工具。針對訊息佇列而開發的應用程式會將訊息傳送到佇列 (也是暫存位置),其中,可在情況允許時將訊息傳送到最後的目的地。此類的應用程式可透過性質相異的網路進行通訊,或是在暫時可能無法與另一台電腦連線的電腦之間傳送訊息。訊息佇列可提供下列好處:


  • 保證能夠傳遞訊息
  • 有效的路由
  • 改善的安全性
  • 支援在交易中傳送訊息
  • 按優先順序進行訊息傳遞

Steps to create MSMQ – Queue manually are as follows:

  1. Right click on “My Computer”
  2. Click on “Manage”
  3. Expand Services And Application
  4. Now you can see the “Message Queuing”
  5. Expand “Private Queue”, click on “New Private queue” – Enter the name “emailqueue”

This will create a new Queue, please refer to the attached document for details. Now we will see how to create it programmatically.
Please note: Namespaces required are: ‘System.Messaging’ for MSMQ and ‘System.Net.Mail' for sending mail using SMTP client. Also MSMQ must be installed on your machine.

交易式與非交易式 queue的差異

差別在於交易式的 queue 在傳送資料到 queue 時,必須經 begin、commit、end 等階段,才會正確進行傳送;非交易式的 queue 只需要 send 就可以傳送。

Coding 範例

函式與屬性說明
Reiceive收到資料後刪除佇列
Peek收到資料後保持資料在佇列

重要 : 加入參考 > 組件 > 架構 > system.messaging

發送MSMQ訊息

//遠端建立 Queue //System.Messaging.MessageQueue.Create(@".\Private$\remoteBuildQueue"); var queue = new System.Messaging.MessageQueue(@".\Private$\mailq"); System.Messaging.Message msmqMsg = new System.Messaging.Message(); // Assign email information to the Message body msmqMsg.Body = "email Content"; //set Recoverable which indicates message is guaranteed msmqMsg.Recoverable = true; //Set Formatter to serialize the object,I’m using binary msmqMsg.Formatter = new System.Messaging.BinaryMessageFormatter(); //Set Formatter to serialize the object. queue.Formatter = new System.Messaging.BinaryMessageFormatter(); //Send the message object in the created queue queue.Send(msmqMsg);

交易式發送MSMQ訊息

private void btnTranSend_Click(object sender, EventArgs e){ var mqPath = @".\Private$\MqTranTest"; var msgQueue = new System.Messaging.MessageQueue(mqPath); var mqTran = new System.Messaging.MessageQueueTransaction(); mqTran.Begin(); //沒有transaction的不會被同意 msgQueue.Send("non-transaction body", "non-transaction label"); msgQueue.Send("transaction body", "transaction label", mqTran); mqTran.Commit(); //可做一次性交易, 自動beginl, commit msgQueue.Send("single transaction body", "single transaction label", System.Messaging.MessageQueueTransactionType.Single); }

遠端刪除佇列

所有的私有佇列都會被刪除

System.Messaging.MessageQueue[] msmques = System.Messaging.MessageQueue.GetPrivateQueuesByMachine("."); foreach (var item in msmques) { try { System.Messaging.MessageQueue.Delete(".\\" + item.QueueName); } }

接收MSMQ訊息

string messageQueuePath = @".\Private$\" + path; //create message queue instance var _msmqQueue = new MessageQueue(messageQueuePath); //set formatter same as sender _msmqQueue.Formatter = new BinaryMessageFormatter(); _msmqQueue.MessageReadPropertyFilter.SetAll(); //設定timeout,逾期無法receive raise exception TimeSpan timeout = new TimeSpan(0, 0, 10); var msg = _msmqQueue.Receive(timeout); ViewBag.content = msg.Body;

使用 MSMQ 處理 Email 寄送

  1. 系統服務當有需要執行電子郵件寄送時,將信件包裝後 send to queue
  2. queue 設定 trigger
  3. trigger 自動觸發收到 MSMQ 訊息後 reiceive 訊息解讀後經 SMTP 送出郵件

參考資料 Refrence

MSDN MSMQ

Use of MSMQ for Sending Bulk Mails

Using MSMQ in mail relay

啟用Windows Server MSMQ功能(訊息佇列)

Powershell建立MSMQ(訊息佇列)設定

MSMQ學習心得分享

使用MS Message Queue進行發送/接收訊息

使用 MSMQ Tiggers

建立第一支C# MSMQ程式

MSMQ Sample