zoukankan      html  css  js  c++  java
  • MSMQ之一:基本应用

    1 MSMQ介绍和安装消息队列

         Message Queue(微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间 中的任一位置。它的实现原理是:消息的发送者把自己想要发送的信息放入一个容器中(我们称之为Message),然后把它保存至一个系统公用空间的消息队 列(Message Queue)中;本地或者是异地的消息接收程序再从该队列中取出发给它的消息进行处理

       消息队列是发送和接收消息的公用存储空间,它可以存在于内存中或者是物理文件中。消息可以以两种方式发送,即快递方式(express)和可恢复模式 (recoverable),它们的区别在于,快递方式为了消息的快速传递,把消息放置于内存中,而不放于物理磁盘上,以获取较高的处理能力;可恢复模式 在传送过程的每一步骤中,都把消息写入物理磁盘中,以得到较好的故障恢复能力。

      对消息队列有了简单的了解后,使用MSMQ进行软件开发需要安装MSMQ,安装完后就该进入实际的开发阶段。具体的安装过程就是在控制面板里“添加/删除程序”下“添加/删除Windows组件”,完成添加就OK。安装完成后就可以通过交互界添加新的消息队列。

    2 创建消息队列

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

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

    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对象。该对象构造方法有三种重载:

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

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

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


    四、读取和接收消息

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

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

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

    2 连接和发送消息队列

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

    3 连接和接受消息

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

    4 连接并且清空队列

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

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

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

    6 复杂对象的消息队列使用

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

    以上选自 Bēniaǒ 博文

    其中如果没有写入队列就去取 多次读取 没有创建就写入 还需要优化



    Top
    收藏
    关注
    评论
  • 相关阅读:
    图与链表的深拷贝
    Codeforces Round #686(Div.3) [C- Sequence
    前缀和
    递归改非递归
    STL源码剖析笔记
    第六章 进程
    C++ 设计模式--模板模式、策略模式、观察者模式
    宏定义方式 进行枚举类型和枚举类型的相互转换
    Linux常见信号介绍
    git rebase 操作
  • 原文地址:https://www.cnblogs.com/zjwei55/p/2160149.html
Copyright © 2011-2022 走看看