MSMQ是微软消息队列的英文缩写。那么什么是消息队列?这些介绍网上一大片这里就不多说了。本文对于大虾级的人物来说这只是小玩意而已,对于初学者来说这文章还是有一定的帮助,希望路过的大虾们别笑话我班门弄斧。

一、MSMQ介绍和安装消息队列
      关于MSMQ详细的介绍请大家向http://www.baidu.com/http://www.g.cn/等专家咨询。
      使用消息队列的优点:稳定、消息优先级、脱机能力以及安全性。
      消息队列分为用户创建的队列(专用队列)和系统队列,用户队列分为,。我是Windows XP,看下图所示(myQueue为自己创建的消息队列,msmqtriggersnotifiations为通用队列):
               

对消息队列有了简单的了解后,使用MSMQ进行软件开发需要安装MSMQ,安装完后就该进入实际的开发阶段。具体的安装过程就是在控制面板里“添加/删除程序”下“添加/删除Windows组件”,完成添加就OK。安装完成后就可以通过交互界添加新的消息队列,详细如下图:
          
      出了上面这种交互界面来创建MSMQ外,也可以通过编程来完成,.NET框架里的MessageQueue类下有一静态方法Create,用来完成消息队列的创建,其定义如下:

  1.  
  2.  1//
  3.  2// 摘要:
  4.  3//    在指定的路径中创建非事务性“消息队列”队列。
  5.  4//
  6.  5// 参数:
  7.  6//   path:
  8.  7//     要创建的队列的路径。
  9.  8//
  10.  9// 返回结果:
  11. 10//     表示新队列的 System.Messaging.MessageQueue。
  12. 11public static MessageQueue Create(string path);
  13. 12//
  14. 13// 摘要:
  15. 14//     在指定的路径中创建事务性或非事务性“消息队列”队列。
  16. 15//
  17. 16// 参数:
  18. 17//   transactional:
  19. 18//     如果创建事务性队列,为 true;如果创建非事务性队列,则为 false。
  20. 19//
  21. 20//   path:
  22. 21//     要创建的队列的路径。
  23. 22//
  24. 23// 返回结果:
  25. 24//     表示新队列的 System.Messaging.MessageQueue。
  26. 25public static MessageQueue Create(string path, bool transactional);

实现消息队列的创建简单代码(C#),创建一个名为"myQueue"的非事务性"消息队列",如下:

  1. MessageQueue.Create(@".\private$\myQueue");

二、创建、删除和管理队列
      在.NET环境下编写Message Queue程序的前提就是需要先安装MSMQ,本文之前已经作了详细的介绍。要开发MSMQ程序就必须学习一个很重要的类(MessageQueue),该类位于名称空间System.Messageing下。其中有几个常用的方法必须掌握:
  --Create方法:创建使用指定路径的新消息队列。
  --Delete方法:删除现有的消息队列。
  --Existe方法:查看指定消息队列是否存在。
  --GetAllMessages()方法:得到队列中的所有消息。
  --GetPublicQueues方法:在“消息队列”网络中定位消息队列。
  --Peek/BeginPeek方法:查看某个特定队列中的消息队列,但不从该队列中移出消息。
  --Receive/BeginReceive方法:检索指定消息队列中最前面的消息并将其从该队列中移除。
  --Send方法:发送消息到指定的消息队列。
  --Purge方法:清空指定队列的消息。

上述列举的方法在此就不作详细介绍,大家可以通过下面的示例程序中来体会他们各自的功能。

三、发送和序列化消息
     MSMQ消息队列中定义的消息由一个主体(body)和若干属性构成。消息的主体可以由文本、二进制构成,根据需要还可以被加密。在MSMQ 中消息的大小不能够超过4MB。发送消息是通过Send方法来完成的,需要一个Message参数。
1、发送消息:
     步骤:连接队列-->指定消息格式-->提供要发送的数据(主体)-->调用Send()方法将消息发送出去。详细见后面的示例程序。
     
2、序列化消息:
     消息序列化可以通过.NET Framework附带的三个预定义格式化程序来完成:
    --  XMLMessageFormatter对象----MessageQueue组件的默认格式化程序设置。
    --  BinaryMessageFormatter对象;
    --  ActiveXMessageFormatter对象; 
    由于后两者格式化后的消息通常不能为人阅读,所以我们经常用到的是XMLMessageFormatter对象。该对象构造方法有三种重载:

  1. 1public XmlMessageFormatter();
  2. 2public XmlMessageFormatter(string[] targetTypeNames);
  3. 3public XmlMessageFormatter(Type[] targetTypes);

如我们后面的示例程序中用到的序列化语句:

  1. 1//序列化为字符串
  2. 2XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(string) });

四、读取和接收消息
1、读取消息:
    也就是从指定队列中获取消息,详细请查看本文前面的关于消息操作的方法介绍。
2、接收消息有两种方式:
    --> 通过Receive方法--具体功能请返回本文前面有详细介绍。
    --> 通过Peek方法--具体功能请返回本文前面有详细介绍。

五、消息使用实例
     通过上面一系列的介绍,了解了MessageQueue类和常用的方法后,下面我们通过一个简单的示例程序来分析消息队列的创建、发送消息以及接收消息等相关知识点:
1、通过Create方法创建使用指定路径的新消息队列

  1.  
  2.  1/// <summary>
  3.  2/// 通过Create方法创建使用指定路径的新消息队列
  4.  3/// </summary>
  5.  4/// <param name="queuePath"></param>
  6.  5public static void Createqueue(string queuePath)
  7.  6{
  8.  7    try
  9.  8    {
  10.  9        if (!MessageQueue.Exists(queuePath))
  11. 10        {
  12. 11            MessageQueue.Create(@".\private$\myQueue");
  13. 12        }
  14. 13        else
  15. 14        {
  16. 15            Console.WriteLine(queuePath + "已经存在!");
  17. 16        }
  18. 17    }
  19. 18    catch (MessageQueueException e)
  20. 19    {
  21. 20        Console.WriteLine(e.Message);
  22. 21    }
  23. 22}

2、连接消息队列并发送消息到队列

  1.  
  2.  1/// <summary>
  3.  2/// 连接消息队列并发送消息到队列
  4.  3/// </summary>
  5.  4public static void SendMessage()
  6.  5{
  7.  6    try
  8.  7    {
  9.  8        //连接到本地的队列
  10.  9        MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
  11. 10        
  12. 11        Message myMessage = new Message();
  13. 12        myMessage.Body = "消息内容";
  14. 13        myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
  15. 14        //发送消息到队列中
  16. 15        myQueue.Send(myMessage);
  17. 16    }
  18. 17    catch (ArgumentException e)
  19. 18    {
  20. 19        Console.WriteLine(e.Message);
  21. 20    }
  22. 21}

3、连接消息队列并从消息队列中接收消息

  1.  
  2.  1/// <summary>
  3.  2/// 连接消息队列并从队列中接收消息
  4.  3/// </summary>
  5.  4public static void ReceiveMessage()
  6.  5{
  7.  6    //连接到本地队列
  8.  7    MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
  9.  8    myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
  10.  9    try
  11. 10    {
  12. 11        //从队列中接收消息
  13. 12        Message myMessage = myQueue.Receive();
  14. 13        string context = (string)myMessage.Body; //获取消息的内容
  15. 14        Console.WriteLine("消息内容为:" + context);
  16. 15    }
  17. 16    catch (MessageQueueException e)
  18. 17    {
  19. 18        Console.WriteLine(e.Message);
  20. 19    }
  21. 20    catch (InvalidCastException e)
  22. 21    {
  23. 22        Console.WriteLine(e.Message);
  24. 23    }
  25. 24}

4、连接队列并清空队列的全部消息

  1.  
  2. 1/// <summary>
  3. 2/// 清空指定队列的消息
  4. 3/// </summary>
  5. 4public static void ClearMessage()
  6. 5{
  7. 6    MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
  8. 7    myQueue.Purge();
  9. 8}

5、连接队列并获取队列的全部消息

  1.  
  2.  1/// <summary>
  3.  2/// 连接队列并获取队列的全部消息
  4.  3/// </summary>
  5.  4public static void GetAllMessage()
  6.  5{
  7.  6    //连接到本地队列
  8.  7    MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
  9.  8    Message[] message = myQueue.GetAllMessages();
  10.  9    XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
  11. 10    for (int i = 0; i < message.Length; i++)
  12. 11    {
  13. 12        message[i].Formatter = formatter;
  14. 13        Console.WriteLine(message[i].Body.ToString());
  15. 14    }
  16. 15}

上面依次的列举出来5个方法,这里我就不做测试了。上述方法全部通过测试的,我在后面提供个连接,没弄清楚的朋友可下载源程序自己去运行调试下。

  1.   1using System;
  2.   2using System.Collections.Generic;
  3.   3using System.Text;
  4.   4using System.Messaging;
  5.   5
  6.   6namespace MSMQ
  7.   7{
  8.   8    class Program
  9.   9    {
  10.  10        static void Main(string[] args)
  11.  11        {
  12.  12            Createqueue(".\\myQueue");
  13.  13            SendMessage();
  14.  14            GetAllMessage();
  15.  15            //ReceiveMessage();
  16.  16        }
  17.  17
  18.  18
  19.  19        /// <summary>
  20.  20        /// 通过Create方法创建使用指定路径的新消息队列
  21.  21        /// </summary>
  22.  22        /// <param name="queuePath"></param>
  23.  23        public static void Createqueue(string queuePath)
  24.  24        {
  25.  25            try
  26.  26            {
  27.  27                if (!MessageQueue.Exists(queuePath))
  28.  28                {
  29.  29                    MessageQueue.Create(@".\private$\myQueue");
  30.  30                }
  31.  31                else
  32.  32                {
  33.  33                    Console.WriteLine(queuePath + "已经存在!");
  34.  34                }
  35.  35            }
  36.  36            catch (MessageQueueException e)
  37.  37            {
  38.  38                Console.WriteLine(e.Message);
  39.  39            }
  40.  40        }
  41.  41
  42.  42        /// <summary>
  43.  43        /// 连接消息队列并发送消息到队列
  44.  44        /// </summary>
  45.  45        public static void SendMessage()
  46.  46        {
  47.  47            try
  48.  48            {
  49.  49                //连接到本地的队列
  50.  50                MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
  51.  51                
  52.  52                Message myMessage = new Message();
  53.  53                myMessage.Body = "消息内容";
  54.  54                myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
  55.  55                //发送消息到队列中
  56.  56                myQueue.Send(myMessage);
  57.  57            }
  58.  58            catch (ArgumentException e)
  59.  59            {
  60.  60                Console.WriteLine(e.Message);
  61.  61            }
  62.  62        }
  63.  63
  64.  64        /// <summary>
  65.  65        /// 连接消息队列并从队列中接收消息
  66.  66        /// </summary>
  67.  67        public static void ReceiveMessage()
  68.  68        {
  69.  69            //连接到本地队列
  70.  70            MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
  71.  71            myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
  72.  72            try
  73.  73            {
  74.  74                //从队列中接收消息
  75.  75                Message myMessage = myQueue.Receive();
  76.  76                string context = (string)myMessage.Body; //获取消息的内容
  77.  77                Console.WriteLine("消息内容为:" + context);
  78.  78            }
  79.  79            catch (MessageQueueException e)
  80.  80            {
  81.  81                Console.WriteLine(e.Message);
  82.  82            }
  83.  83            catch (InvalidCastException e)
  84.  84            {
  85.  85                Console.WriteLine(e.Message);
  86.  86            }
  87.  87        }
  88.  88
  89.  89        /// <summary>
  90.  90        /// 清空指定队列的消息
  91.  91        /// </summary>
  92.  92        public static void ClearMessage()
  93.  93        {
  94.  94            MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
  95.  95            myQueue.Purge();
  96.  96        }
  97.  97
  98.  98        /// <summary>
  99.  99        /// 连接队列并获取队列的全部消息
  100. 100        /// </summary>
  101. 101        public static void GetAllMessage()
  102. 102        {
  103. 103            //连接到本地队列
  104. 104            MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
  105. 105            Message[] message = myQueue.GetAllMessages();
  106. 106            XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
  107. 107            for (int i = 0; i < message.Length; i++)
  108. 108            {
  109. 109                message[i].Formatter = formatter;
  110. 110                Console.WriteLine(message[i].Body.ToString());
  111. 111            }
  112. 112        }
  113. 113    }
  114. 114}
  115. 115

六、复杂消息发送实例
     通过上面一系列的介绍,对于简单消息的发送和接收及消息的管理应该都不会有什么问题了,下面我在介绍一下关于复杂的消息处理,现在有这样一个需求,要求通过消息队列将一本图书信息发送到队列里,然后从消息队列里读取出来。图书的基本信息包括图书编号、图书名称、图书作者以及图书定价,这样的一个复杂的对象类型怎么来传输呢?详细如下:

  1.  1namespace MSMQ.App
  2.  2{
  3.  3    public class Book
  4.  4    {
  5.  5        private int _BookId;
  6.  6        public int BookId
  7.  7        {
  8.  8            get { return _BookId; }
  9.  9            set { _BookId = value; }
  10. 10        }
  11. 11
  12. 12        private string _BookName;
  13. 13        public string BookName
  14. 14        {
  15. 15            get { return _BookName; }
  16. 16            set { _BookName = value; }
  17. 17        }
  18. 18
  19. 19        private string _BookAuthor;
  20. 20        public string BookAuthor
  21. 21        {
  22. 22            get { return _BookAuthor; }
  23. 23            set { _BookAuthor = value; }
  24. 24        }
  25. 25
  26. 26        private double _BookPrice;
  27. 27        public double BookPrice
  28. 28        {
  29. 29            get { return _BookPrice; }
  30. 30            set { _BookPrice = value; }
  31. 31        }
  32. 32    }
  33. 33}
  1.  
  2.  1namespace MSMQ.App
  3.  2{
  4.  3    public class MsgQueue
  5.  4    {
  6.  5        /// <summary>
  7.  6        /// 通过Create方法创建使用指定路径的新消息队列
  8.  7        /// </summary>
  9.  8        /// <param name="queuePath"></param>
  10.  9        public static void Createqueue(string queuePath)
  11. 10        {
  12. 11            try
  13. 12            {
  14. 13                if (!MessageQueue.Exists(queuePath))
  15. 14                {
  16. 15                    MessageQueue.Create(@".\private$\myQueue");
  17. 16                    MessageBox.Show("创建队列成功!");
  18. 17                }
  19. 18                else
  20. 19                {
  21. 20                    MessageBox.Show(queuePath + "已经存在!");
  22. 21                }
  23. 22            }
  24. 23            catch (MessageQueueException e)
  25. 24            {
  26. 25                MessageBox.Show(e.Message);
  27. 26            }
  28. 27        }
  29. 28
  30. 29        /// <summary>
  31. 30        /// 连接消息队列并发送消息到队列
  32. 31        /// </summary>
  33. 32        public static bool SendMessage(Book book)
  34. 33        {
  35. 34            bool flag = false;
  36. 35            try
  37. 36            {
  38. 37                //连接到本地的队列
  39. 38                MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
  40. 39
  41. 40                System.Messaging.Message myMessage = new System.Messaging.Message();
  42. 41                myMessage.Body = book;
  43. 42                myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.App.Book) });
  44. 43                //发送消息到队列中
  45. 44                myQueue.Send(myMessage);
  46. 45                flag = true;
  47. 46            }
  48. 47            catch (ArgumentException e)
  49. 48            {
  50. 49                MessageBox.Show(e.Message);
  51. 50            }
  52. 51            return flag;
  53. 52        }
  54. 53
  55. 54        /// <summary>
  56. 55        /// 连接消息队列并从队列中接收消息
  57. 56        /// </summary>
  58. 57        public static string ReceiveMessage()
  59. 58        {
  60. 59            //连接到本地队列
  61. 60            MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
  62. 61            myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.App.Book) });
  63. 62            try
  64. 63            {
  65. 64                //从队列中接收消息
  66. 65                System.Messaging.Message myMessage = myQueue.Receive();
  67. 66                Book book = (Book)myMessage.Body; //获取消息的内容
  68. 67                return string.Format("编号:{0},书名:{1},作者:{2},定价:{3}",
  69. 68                    book.BookId,
  70. 69                    book.BookName,
  71. 70                    book.BookAuthor,
  72. 71                    book.BookPrice);
  73. 72            }
  74. 73            catch (MessageQueueException e)
  75. 74            {
  76. 75                MessageBox.Show(e.Message);
  77. 76            }
  78. 77            catch (InvalidCastException e)
  79. 78            {
  80. 79                MessageBox.Show(e.Message);
  81. 80            }
  82. 81            return null;
  83. 82        }
  84. 83    }
  85. 84}

其实发送复杂的消息也就是在消息序列化上有些差别,别的地方与发送普通文本消息没什么大的变化,上面类里提供了创建队列,发送消息到队列,从队列获取消息三个方法,测试结果如下:
                                 
     上示例中,完成了一个复杂类型的消息发送到队列及从队列中读取的演义,详细请下载代码查看:
   点击这里下载本文示例代码     本文就简单介绍于此,更深入的学习MSMQ请查阅相关资料(如PetShop4里的定单处理策略)。

ASP.NET中进行消息处理(MSMQ) 一的更多相关文章

  1. ASP.NET中进行消息处理(MSMQ) 三(转)

    在本文的前两篇文章里对MSMQ的相关知识点进行了介绍,很多阅读过这前两篇文章的朋友都曾问到过这样一些问题:  1.如何把MSMQ应用到实际的项目中去呢?  2.可不可以介绍一个实际的应用实例?  3. ...

  2. ASP.NET中进行消息处理(MSMQ) 二(转)

          在我上一篇文章<ASP.NET中进行消息处理(MSMQ)一>里对MSMQ做了个通俗的介绍,最后以发送普通文本消息和复杂的对象消息为例介绍了消息队列的使用. 本文在此基础上继续介 ...

  3. ASP.NET中进行消息处理(MSMQ) 三

    在本文的前两篇文章里对MSMQ的相关知识点进行了介绍,很多阅读过这前两篇文章的朋友都曾问到过这样一些问题:  1.如何把MSMQ应用到实际的项目中去呢?  2.可不可以介绍一个实际的应用实例?  3. ...

  4. ASP.NET中进行消息处理(MSMQ) 二

    在我上一篇文章<ASP.NET中进行消息处理(MSMQ)一>里对MSMQ做了个通俗的介绍,最后以发送普通文本消息和复杂的对象消息为例介绍了消息队列的使用. 本文在此基础上继续介绍MSMQ的 ...

  5. ASP.NET中进行消息处理(MSMQ) 一(转)

    MSMQ是微软消息队列的英文缩写.那么什么是消息队列?这些介绍网上一大片这里就不多说了.本文对于大虾级的人物来说这只是小玩意而已,对于初学者来说这文章还是有一定的帮助,希望路过的大虾们别笑话我班门弄斧 ...

  6. Asp.Net中的消息处理---MSMQ系列学习(一)

    刚刚毕业一年,比较浮躁,上次面试被问到消息队列,觉得非常的惭愧因为不知道,所以下定决心一定要学会使用它.以前只是听说过有这么个东西,要说是什么,在什么场景下使用却是无从知晓,因为自己也确实没有在项目中 ...

  7. 在ASP.NET中如何运行后台任务

    from:https://blogs.msdn.microsoft.com/scott_hanselman/2014/12/21/asp-net/ [原文发表地址] How to run Backgr ...

  8. ASP.NET Web API消息处理管道:Self Host下的消息处理管道[下篇]

    ASP.NET Web API消息处理管道:Self Host下的消息处理管道[下篇] 我们知道ASP.NET Web API借助于HttpSelfHostServer以Self Host模式寄宿于当 ...

  9. 细说Asp.Net Web API消息处理管道(二)

    在细说Asp.Net Web API消息处理管道这篇文章中,通过翻看源码和实例验证的方式,我们知道了Asp.Net Web API消息处理管道的组成类型以及Asp.Net Web API是如何创建消息 ...

随机推荐

  1. 简单认识UISwitch

    以下是常用属性: self.mySwitch.layer.cornerRadius = 15;   // 边框圆角角度 self.mySwitch.layer.borderWidth = 2;  // ...

  2. C语言-07-预处理、typedef、static和extern

    预处理 1> 使用注意 以#开头 在代码被翻译成0和1之前执行 预处理指令可以出现在任何位置 作用域是从编写指令那一行开始到文件结束 2> 宏定义 基本使用 ① 使用#define定义 ② ...

  3. iOS开发之网络编程--4、NSURLSessionDataTask实现文件下载(离线断点续传下载) <进度值显示优化>

    前言:根据前篇<iOS开发之网络编程--2.NSURLSessionDownloadTask文件下载>或者<iOS开发之网络编程--3.NSURLSessionDataTask实现文 ...

  4. iOS开发之网络编程--使用NSURLConnection实现文件上传

    前言:使用NSURLConnection实现文件上传有点繁琐.    本文并没有介绍使用第三方框架上传文件. 正文: 这里先提供用于编码测试的接口:http://120.25.226.186:3281 ...

  5. 饭团(1):用NABCD大法为项目奠基

    上一篇文章:提升效率的开发工具 提到高效的开发工具就像催化剂,加速项目开发.这篇文章提到的NABCD大法,就像一份地图,指引项目往正确的方法上发展. 选择比努力更重要.一个项目成功自然离不开码农们的努 ...

  6. 页面间(窗口间)的取值赋值及获取iframe下的window对象

    ①同一个窗口中,获取某个iframe的信息 <body> <iframe id="PAID" name="PA" src="Item ...

  7. 我的Windows核心编程——完成端口+套接字 图解

    ========================声明============================ 本文原创,转载请注明作者和出处,并保证文章的完整性(包括本声明). 本文不定期修改完善,为 ...

  8. cocos2d-x之文件读写

    bool HelloWorld::init() { if ( !Layer::init() ) { return false; } auto fu=FileUtils::getInstance(); ...

  9. Culcurse

    一.简介 如果你是一个享受Linux终端的系统管理员,但同样需要一种方法来安排每天最重要的任务.约会和会议,你会发现calcurse是一个很有用的工具.calcurse包含了日历.一个待办事项管理.一 ...

  10. html,js简单保存textarea换行格式

    有时候我们在做表单提交时,往往需要把html标签保存起来,但是textarea不保存换行的信息,所以我们需要用js来实现保存textarea的换行等HTM标签.真正让HTML文本框里的换换等格式保留下 ...