溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ASP.NET中進行消息處理(MSMQ)

發(fā)布時間:2020-07-28 17:55:33 來源:網(wǎng)絡(luò) 閱讀:588 作者:skydxd 欄目:編程語言
MSMQ是微軟消息隊列的英文縮寫。那么什么是消息隊列?這些介紹網(wǎng)上一大片這里就不多說了。本文對于大蝦級的人物來說這只是小玩意而已,對于初學(xué)者來說這文章還是有一定的幫助,希望路過的大蝦們別笑話我班門弄斧。

一、MSMQ介紹和安裝消息隊列
      關(guān)于MSMQ詳細的介紹請大家向
http://www.baidu.com/http://www.g.cn/等專家咨詢。
      使用消息隊列的優(yōu)點:穩(wěn)定、消息優(yōu)先級、脫機能力以及安全性。
      消息隊列分為用戶創(chuàng)建的隊列(專用隊列)和系統(tǒng)隊列,用戶隊列分為,。我是Windows XP,看下圖所示(myQueue為自己創(chuàng)建的消息隊列,msmqtriggersnotifiations為通用隊列):
               ASP.NET中進行消息處理(MSMQ)

     對消息隊列有了簡單的了解后,使用MSMQ進行軟件開發(fā)需要安裝MSMQ,安裝完后就該進入實際的開發(fā)階段。具體的安裝過程就是在控制面板里“添加/刪除程序”下“添加/刪除Windows組件”,完成添加就OK。安裝完成后就可以通過交互界添加新的消息隊列,詳細如下圖:
          ASP.NET中進行消息處理(MSMQ)
      出了上面這種交互界面來創(chuàng)建MSMQ外,也可以通過編程來完成,.NET框架里的MessageQueue類下有一靜態(tài)方法Create,用來完成消息隊列的創(chuàng)建,其定義如下:
ASP.NET中進行消息處理(MSMQ)
 1ASP.NET中進行消息處理(MSMQ)//
 2ASP.NET中進行消息處理(MSMQ)// 摘要:
 3ASP.NET中進行消息處理(MSMQ)//    在指定的路徑中創(chuàng)建非事務(wù)性“消息隊列”隊列。
 4ASP.NET中進行消息處理(MSMQ)//
 5ASP.NET中進行消息處理(MSMQ)// 參數(shù):
 6ASP.NET中進行消息處理(MSMQ)//   path:
 7ASP.NET中進行消息處理(MSMQ)//     要創(chuàng)建的隊列的路徑。
 8ASP.NET中進行消息處理(MSMQ)//
 9ASP.NET中進行消息處理(MSMQ)// 返回結(jié)果:
10ASP.NET中進行消息處理(MSMQ)//     表示新隊列的 System.Messaging.MessageQueue。
11ASP.NET中進行消息處理(MSMQ)public static MessageQueue Create(string path);
12ASP.NET中進行消息處理(MSMQ)//
13ASP.NET中進行消息處理(MSMQ)// 摘要:
14ASP.NET中進行消息處理(MSMQ)//     在指定的路徑中創(chuàng)建事務(wù)性或非事務(wù)性“消息隊列”隊列。
15ASP.NET中進行消息處理(MSMQ)//
16ASP.NET中進行消息處理(MSMQ)// 參數(shù):
17ASP.NET中進行消息處理(MSMQ)//   transactional:
18ASP.NET中進行消息處理(MSMQ)//     如果創(chuàng)建事務(wù)性隊列,為 true;如果創(chuàng)建非事務(wù)性隊列,則為 false。
19ASP.NET中進行消息處理(MSMQ)//
20ASP.NET中進行消息處理(MSMQ)//   path:
21ASP.NET中進行消息處理(MSMQ)//     要創(chuàng)建的隊列的路徑。
22ASP.NET中進行消息處理(MSMQ)//
23ASP.NET中進行消息處理(MSMQ)// 返回結(jié)果:
24ASP.NET中進行消息處理(MSMQ)//     表示新隊列的 System.Messaging.MessageQueue。
25ASP.NET中進行消息處理(MSMQ)public static MessageQueue Create(string path, bool transactional);
ASP.NET中進行消息處理(MSMQ)

     實現(xiàn)消息隊列的創(chuàng)建簡單代碼(C#),創(chuàng)建一個名為"myQueue"的非事務(wù)性"消息隊列",如下:
ASP.NET中進行消息處理(MSMQ)MessageQueue.Create(@".\private$\myQueue");

二、創(chuàng)建、刪除和管理隊列
      在.NET環(huán)境下編寫Message Queue程序的前提就是需要先安裝MSMQ,本文之前已經(jīng)作了詳細的介紹。要開發(fā)MSMQ程序就必須學(xué)習(xí)一個很重要的類(MessageQueue),該類位于名稱空間System.Messageing下。其中有幾個常用的方法必須掌握:
  --Create方法:創(chuàng)建使用指定路徑的新消息隊列。
  --Delete方法:刪除現(xiàn)有的消息隊列。
  --Existe方法:查看指定消息隊列是否存在。
  --GetAllMessages()方法:得到隊列中的所有消息。
  --GetPublicQueues方法:在“消息隊列”網(wǎng)絡(luò)中定位消息隊列。
  --Peek/BeginPeek方法:查看某個特定隊列中的消息隊列,但不從該隊列中移出消息。
  --Receive/BeginReceive方法:檢索指定消息隊列中最前面的消息并將其從該隊列中移除。
  --Send方法:發(fā)送消息到指定的消息隊列。
  --Purge方法:清空指定隊列的消息。

    上述列舉的方法在此就不作詳細介紹,大家可以通過下面的示例程序中來體會他們各自的功能。

三、發(fā)送和序列化消息
     MSMQ消息隊列中定義的消息由一個主體(body)和若干屬性構(gòu)成。消息的主體可以由文本、二進制構(gòu)成,根據(jù)需要還可以被加密。在MSMQ 中消息的大小不能夠超過4MB。發(fā)送消息是通過Send方法來完成的,需要一個Message參數(shù)。
1、發(fā)送消息:
     步驟:連接隊列-->指定消息格式-->提供要發(fā)送的數(shù)據(jù)(主體)-->調(diào)用Send()方法將消息發(fā)送出去。詳細見后面的示例程序。
    
2、序列化消息:
     消息序列化可以通過.NET Framework附帶的三個預(yù)定義格式化程序來完成:
    --  XMLMessageFormatter對象----MessageQueue組件的默認格式化程序設(shè)置。
    --  BinaryMessageFormatter對象;
    --  ActiveXMessageFormatter對象;
    由于后兩者格式化后的消息通常不能為人閱讀,所以我們經(jīng)常用到的是XMLMessageFormatter對象。該對象構(gòu)造方法有三種重載:
1ASP.NET中進行消息處理(MSMQ)public XmlMessageFormatter();
2ASP.NET中進行消息處理(MSMQ)public XmlMessageFormatter(string[] targetTypeNames);
3ASP.NET中進行消息處理(MSMQ)public XmlMessageFormatter(Type[] targetTypes);
如我們后面的示例程序中用到的序列化語句:
1ASP.NET中進行消息處理(MSMQ)//序列化為字符串
2ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] ASP.NET中進行消息處理(MSMQ)typeof(string) });

四、讀取和接收消息
1、讀取消息:
    也就是從指定隊列中獲取消息,詳細請查看本文前面的關(guān)于消息操作的方法介紹。
2、接收消息有兩種方式:
    --> 通過Receive方法--具體功能請返回本文前面有詳細介紹。
    --> 通過Peek方法--具體功能請返回本文前面有詳細介紹。

五、消息使用實例
     通過上面一系列的介紹,了解了MessageQueue類和常用的方法后,下面我們通過一個簡單的示例程序來分析消息隊列的創(chuàng)建、發(fā)送消息以及接收消息等相關(guān)知識點:
1、通過Create方法創(chuàng)建使用指定路徑的新消息隊列
ASP.NET中進行消息處理(MSMQ)
 1ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)/**//// <summary>
 2ASP.NET中進行消息處理(MSMQ)/// 通過Create方法創(chuàng)建使用指定路徑的新消息隊列
 3ASP.NET中進行消息處理(MSMQ)/// </summary>
 4ASP.NET中進行消息處理(MSMQ)/// <param name="queuePath"></param>

 5ASP.NET中進行消息處理(MSMQ)public static void Createqueue(string queuePath)
 6ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ){
 7ASP.NET中進行消息處理(MSMQ)    try
 8ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)    ASP.NET中進行消息處理(MSMQ){
 9ASP.NET中進行消息處理(MSMQ)        if (!MessageQueue.Exists(queuePath))
10ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)        ASP.NET中進行消息處理(MSMQ){
11ASP.NET中進行消息處理(MSMQ)            MessageQueue.Create(@".\private$\myQueue");
12ASP.NET中進行消息處理(MSMQ)        }

13ASP.NET中進行消息處理(MSMQ)        else
14ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)        ASP.NET中進行消息處理(MSMQ){
15ASP.NET中進行消息處理(MSMQ)            Console.WriteLine(queuePath + "已經(jīng)存在!");
16ASP.NET中進行消息處理(MSMQ)        }

17ASP.NET中進行消息處理(MSMQ)    }

18ASP.NET中進行消息處理(MSMQ)    catch (MessageQueueException e)
19ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)    ASP.NET中進行消息處理(MSMQ){
20ASP.NET中進行消息處理(MSMQ)        Console.WriteLine(e.Message);
21ASP.NET中進行消息處理(MSMQ)    }

22ASP.NET中進行消息處理(MSMQ)}
ASP.NET中進行消息處理(MSMQ)

2、連接消息隊列并發(fā)送消息到隊列
ASP.NET中進行消息處理(MSMQ)
 1ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)/**//// <summary>
 2ASP.NET中進行消息處理(MSMQ)/// 連接消息隊列并發(fā)送消息到隊列
 3ASP.NET中進行消息處理(MSMQ)/// </summary>

 4ASP.NET中進行消息處理(MSMQ)public static void SendMessage()
 5ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ){
 6ASP.NET中進行消息處理(MSMQ)    try
 7ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)    ASP.NET中進行消息處理(MSMQ){
 8ASP.NET中進行消息處理(MSMQ)        //連接到本地的隊列
 9ASP.NET中進行消息處理(MSMQ)        MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
10ASP.NET中進行消息處理(MSMQ)        
11ASP.NET中進行消息處理(MSMQ)        Message myMessage = new Message();
12ASP.NET中進行消息處理(MSMQ)        myMessage.Body = "消息內(nèi)容";
13ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)        myMessage.Formatter = new XmlMessageFormatter(new Type[] ASP.NET中進行消息處理(MSMQ)typeof(string) });
14ASP.NET中進行消息處理(MSMQ)        //發(fā)送消息到隊列中
15ASP.NET中進行消息處理(MSMQ)        myQueue.Send(myMessage);
16ASP.NET中進行消息處理(MSMQ)    }

17ASP.NET中進行消息處理(MSMQ)    catch (ArgumentException e)
18ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)    ASP.NET中進行消息處理(MSMQ){
19ASP.NET中進行消息處理(MSMQ)        Console.WriteLine(e.Message);
20ASP.NET中進行消息處理(MSMQ)    }

21ASP.NET中進行消息處理(MSMQ)}
ASP.NET中進行消息處理(MSMQ)

3、連接消息隊列并從消息隊列中接收消息
ASP.NET中進行消息處理(MSMQ)
 1ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)/**//// <summary>
 2ASP.NET中進行消息處理(MSMQ)/// 連接消息隊列并從隊列中接收消息
 3ASP.NET中進行消息處理(MSMQ)/// </summary>

 4ASP.NET中進行消息處理(MSMQ)public static void ReceiveMessage()
 5ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ){
 6ASP.NET中進行消息處理(MSMQ)    //連接到本地隊列
 7ASP.NET中進行消息處理(MSMQ)    MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
 8ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)    myQueue.Formatter = new XmlMessageFormatter(new Type[] ASP.NET中進行消息處理(MSMQ)typeof(string) });
 9ASP.NET中進行消息處理(MSMQ)    try
10ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)    ASP.NET中進行消息處理(MSMQ){
11ASP.NET中進行消息處理(MSMQ)        //從隊列中接收消息
12ASP.NET中進行消息處理(MSMQ)        Message myMessage = myQueue.Receive();
13ASP.NET中進行消息處理(MSMQ)        string context = (string)myMessage.Body; //獲取消息的內(nèi)容
14ASP.NET中進行消息處理(MSMQ)        Console.WriteLine("消息內(nèi)容為:" + context);
15ASP.NET中進行消息處理(MSMQ)    }

16ASP.NET中進行消息處理(MSMQ)    catch (MessageQueueException e)
17ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)    ASP.NET中進行消息處理(MSMQ){
18ASP.NET中進行消息處理(MSMQ)        Console.WriteLine(e.Message);
19ASP.NET中進行消息處理(MSMQ)    }

20ASP.NET中進行消息處理(MSMQ)    catch (InvalidCastException e)
21ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)    ASP.NET中進行消息處理(MSMQ){
22ASP.NET中進行消息處理(MSMQ)        Console.WriteLine(e.Message);
23ASP.NET中進行消息處理(MSMQ)    }

24ASP.NET中進行消息處理(MSMQ)}
ASP.NET中進行消息處理(MSMQ)

4、連接隊列并清空隊列的全部消息
ASP.NET中進行消息處理(MSMQ)
1ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)/**//// <summary>
2ASP.NET中進行消息處理(MSMQ)/// 清空指定隊列的消息
3ASP.NET中進行消息處理(MSMQ)/// </summary>

4ASP.NET中進行消息處理(MSMQ)public static void ClearMessage()
5ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ){
6ASP.NET中進行消息處理(MSMQ)    MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
7ASP.NET中進行消息處理(MSMQ)    myQueue.Purge();
8ASP.NET中進行消息處理(MSMQ)}
ASP.NET中進行消息處理(MSMQ)

5、連接隊列并獲取隊列的全部消息
ASP.NET中進行消息處理(MSMQ)
 1ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)/**//// <summary>
 2ASP.NET中進行消息處理(MSMQ)/// 連接隊列并獲取隊列的全部消息
 3ASP.NET中進行消息處理(MSMQ)/// </summary>

 4ASP.NET中進行消息處理(MSMQ)public static void GetAllMessage()
 5ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ){
 6ASP.NET中進行消息處理(MSMQ)    //連接到本地隊列
 7ASP.NET中進行消息處理(MSMQ)    MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
 8ASP.NET中進行消息處理(MSMQ)    Message[] message = myQueue.GetAllMessages();
 9ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)    XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] ASP.NET中進行消息處理(MSMQ)typeof(string) });
10ASP.NET中進行消息處理(MSMQ)    for (int i = 0; i < message.Length; i++)
11ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)    ASP.NET中進行消息處理(MSMQ){
12ASP.NET中進行消息處理(MSMQ)        message[i].Formatter = formatter;
13ASP.NET中進行消息處理(MSMQ)        Console.WriteLine(message[i].Body.ToString());
14ASP.NET中進行消息處理(MSMQ)    }

15ASP.NET中進行消息處理(MSMQ)}
ASP.NET中進行消息處理(MSMQ)

     上面依次的列舉出來5個方法,這里我就不做測試了。上述方法全部通過測試的,我在后面提供個連接,沒弄清楚的朋友可下載源程序自己去運行調(diào)試下。
ASP.NET中進行消息處理(MSMQ)ASP.NET中進行消息處理(MSMQ)本實例完整代碼
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI