WizardWu 編程網

一位台灣的工程師,接觸 .NET 逾十年,近年研究 SQL Server、Performance Tuning、手機應用

博客园 首页 新随笔 联系 订阅 管理

本帖透过 .NET 代码和 Windows 的图形界面,简单测试 Windows Server 2008 R2 中的 MSMQ (Microsoft Message Queue)。

------------------------------------------------------------------------------

消息队列 (也称为 MQ),让在不同时间运行的应用程序,可在异类网络和可能暂时脱机的系统之间通讯。我们撰写的应用程序,可向队列发送消息,或从队列读取消息。

消息队列提供了以下好处:

  • 保证消息的传递
  • 高效路由
  • 增强的安全性
  • 基于优先级的消息传递

------------------------------------------------------------------------------

与 Windows Server 2008 R2 / Windows 7 一起发布的 Message Queuing 5.0 中,引入了下列的新功能:

  • 处理大量队列的能力
    Message Queuing 5.0 提供了处理大量队列的能力。尽管 Message Queuing 4.0 未对可创建的队列数目实行特定限制,但当有数千个队列时,还是会对性能造成负面影响。特别是将队列加载到内存中时,因队列查找的算法,而大大增加了消息队列服务的启动时间。而对于 Windows Server 2008 R2、Windows 7,已对消息队列在启动时使用的队列查找算法进行了优化,当系统上承载了大量队列时,会显著增加消息队列的启动性能。


  • 更安全的身份验证算法
    Message Queuing 5.0 支持安全哈希算法 2.0 (SHA2),和 Windows Server 2008 R2 支持的所有高级哈希算法。默认设置为 SHA-2,摘要长度为 512 位。由于 SHA1、消息摘要版本 2 (MD2)、MD4、MD5 和消息验证代码 (MAC) 等算法,被认为不够安全,因此默认情况下,对这些算法的支持在 Message Queuing 5.0 中处于禁用状态。若要启用不够安全的算法,必須自行添加 WeakHashAlgorithms 注册表项。

------------------------------------------------------------------------------

安装消息队列

执行用户必须要有本地 Administrators 组中的成员身份,或等效身份。

在 Windows 7 上安装消息队列的步骤:

  1. 打开“控制面板”。
  2. 单击“程序”,然后在“程序和功能”下,单击“打开或关闭 Windows 功能”。
    -或者-
    单击“经典视图”,双击“程序和功能”,然后在任务窗格中单击“打开或关闭 Windows 功能”。
  3. 依次展开“Microsoft Message Queue (MSMQ) 服务器”、“Microsoft Message Queue (MSMQ) 服务器核心”,然后选中要安装的消息队列功能的复选框。
  4. 单击“确定”。
  5. 如果系统提示您重新启动计算机,请单击“确定”以完成安装。


在 Windows Server 2008 R2 上安装消息队列的步骤:

  1. 单击“开始”,依次指向“程序”、“管理工具”,然后单击“服务器管理器”显示服务器管理器。
  2. 单击“添加功能”启动“添加功能向导”。
  3. 依次展开 MSMQ、“MSMQ 服务”,然后选中要安装的消息队列功能的复选框。
  4. 单击“下一步”,然后单击“安装”。
  5. 如果系统提示您重新启动计算机,请单击“确定”以完成安装。

 


圖 1 安装过程,以 Windows Server 2008 R2 为例



圖 2 安装过程,以 Windows Server 2008 R2 为例

 

不同版本的 Windows 支持不同的消息队列功能,下表列出了各版 Windows 7 和 Windows Server 2008 R2 所支持的功能:

功能 Windows 7
简易版
Windows 7
家庭普通版
Windows 7
家庭高级版
Windows 7
旗舰版
Windows 7
专业版
Windows 7
企业版
Windows Server 2008 R2 所有版本

Microsoft Message Queue (MSMQ)
服务器核心/MSMQ 服务

支持

支持

支持

支持

支持

支持

支持

消息队列管理控制台

支持

支持

支持

支持

支持

支持

支持

Microsoft Active Directory 域服务
集成/目录服务集成

不支持

不支持

不支持

支持

支持

支持

支持

MSMQ HTTP 支持/HTTP 支持

不支持

不支持

支持

支持

支持

支持

支持

MSMQ 触发器/消息队列触发器

支持

支持

支持

支持

支持

支持

支持

MSMQ DCOM 代理/消息队列
DCOM 代理

支持

支持

支持

支持

支持

支持

支持

多播支持/多播支持

支持

支持

支持

支持

支持

支持

支持

路由服务

不支持

不支持

不支持

不支持

不支持

不支持

支持

 

 ------------------------------------------------------------------------------

只允许队列中经过验证的消息

创建队列时,默认的权限是每个人都可以向该队列发送消息。若要获取更高的安全性,可更改队列的默认安全权限。也可以在创建队列时指定队列的属性。例如,可以指定只接受经过身份验证的消息。


以下为只允许在队列中,放置经过身份验证消息的步骤:

  1. 打开“计算机管理”。
  2. 在控制台树中,右键单击队列。
     位置如下:
      计算机管理/服务和应用程序/消息队列/YourQueueFolder(如“公用队列”或“专用队列”)/YourQueue
  3. 单击“属性”。
  4. 在“常规”选项卡上,选中“已验证”复选框。


圖 3

------------------------------------------------------------------------------

创建和使用队列

在 .NET 中,提供了方便我们操作 MSMQ 的 API - System.Messaging,而创建队列可用其中的 MessageQueue.Create。

Create方法有两个重载:

1、Message.Create(string path):创建非事务性队列。

2、Message.Create(string path, bool transactional):指定创建事务性或者非事务性队列。


在创建队列时先判断是否已经存在队列,还有就是权限问题。


创建各种类别的队列,是通过 path 属性不同的表现来实现:

1、公用队列:MachineName\QueueName

2、专用队列:MachineName\Private$\QueueName

3、日记队列:MachineName\QueueName\Journal$

4、计算机日记队列:MachineName\Journal$

5、计算机死信队列:MachineName\Deadletter$

6、计算机事务性死信队列:MachineName\XactDeadletter$

 

完整测试代码如下 (VS 2010 / ASP.NET 4.0 + Windows Server 2008 R2):

 

测试代码
using System;
using System.Messaging;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    {
    }

    
//通过 Create 方法创建新的消息队列
    protected void Button1_Click(object sender, EventArgs e)
    {
        
if (!MessageQueue.Exists(@".\private$\myQueue"))
        {
            
using (MessageQueue mq = MessageQueue.Create(@".\private$\myQueue"))
            {
                Response.Write(mq.Label 
+ "<br>");
                Response.Write(mq.Path 
+ "<br>");
                Response.Write(mq.QueueName 
+ "<br>");
                Response.Write(mq.Authenticate 
+ "<br>");
                
//在 MSMQ 中消息的大小,默認不能超过 4 MB
                Response.Write(mq.MaximumQueueSize + "<br>");
            }
        }
        
else
        {
            Response.Write(
"myQueue已经存在!");
        }
    }

    
//发送消息到队列
    protected void Button2_Click(object sender, EventArgs e)
    {
        
//连接到本地的队列
        MessageQueue myQueue = new MessageQueue(@".\private$\myQueue");

        Message myMessage 
= new Message();
        myMessage.Body 
= "「iPad」日本では4月後半発売";    //消息内容
        myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
        
        
//设置消息发送的优先级别
        
//myMessage.Priority = MessagePriority.Highest; //最高消息优先级

        
//发送消息到队列中
        myQueue.Send(myMessage);

        myMessage.Dispose();
        myQueue.Dispose();        
    }

    
//client-side: 从队列中接收指定的消息
    protected void Button3_Click(object sender, EventArgs e)
    {
        
//连接到本地队列
        MessageQueue myQueue = new MessageQueue(@".\private$\myQueue");
        myQueue.Formatter 
= new XmlMessageFormatter(new Type[] { typeof(string) });        
        Message myMessage 
= null;
        
try
        {
            
//从队列中接收消息
            
//Peek: 返回队列中第一条消息的副本,而不从队列中移除该消息
            
//Receive: 接收队列中的第一条消息,但不将它从队列中移除
            
//PeekById: 返回具有指定消息标识符的消息的副本,但不从队列中移除消息
            
//ReceiveById: 接收匹配给定标识符的消息,并将其从队列中移除
            myMessage = myQueue.Peek();

            
string strContext = (string)myMessage.Body; //获取消息的内容
            Response.Write("消息内容为:" + strContext + "<br>");
        }
        
catch // (MessageQueueException e)
        {
            Response.Write(
"error4");
        }
        
finally
        {
            myMessage.Dispose();
            myQueue.Dispose();
        }
    }
    
    
//获取队列的全部消息
    protected void Button4_Click(object sender, EventArgs e)
    {
        MessageQueue myQueue 
= new MessageQueue(".\\private$\\myQueue");
        
//GetAllMessages: 得到队列中的所有消息
        Message[] myMessage = myQueue.GetAllMessages();

        XmlMessageFormatter formatter 
= new XmlMessageFormatter(new Type[] { typeof(string) });
        
for (int i = 0; i < myMessage.Length; i++)
        {
            myMessage[i].Formatter 
= formatter;
            Response.Write(myMessage[i].Body.ToString() 
+ "<br>");
        }
                
        myQueue.Dispose();
    }

    
//清空指定队列的消息
    protected void Button5_Click(object sender, EventArgs e)
    {
        MessageQueue myQueue 
= new MessageQueue(@".\private$\myQueue");
        myQueue.Purge();        
//删除此队列中包含的所有消息

        
//删除服务器上的队列
        
//MessageQueue.Delete(@".\private$\myQueue");

        myQueue.Dispose();
    }

}

 

 

图 4 执行结果


------------------------------------------------------------------------------

事务性消息处理

在 MSMQ 中利用事务性处理,可以确保事务中的消息按照顺序传送,只传送一次,并且从目的队列成功地被检索。

若要发送或接收消息时加入事务,可以使用 MessageQueueTransaction 类以创建事务,并将其传递到 MessageQueue.Send 方法或 MessageQueue.Receive 方法 [3]。

 

启动了事务后的消息发送代码: 

MessageQueueTransaction myTransaction = new MessageQueueTransaction();
myTransaction.Begin();
//发送消息到队列中
myQueue.Send(myMessage, myTransaction);
myTransaction.Commit();

  

启动了事务后的消息读取代码:  

if (myQueue.Transactional)
{
    MessageQueueTransaction myTransaction 
= new MessageQueueTransaction();
    myTransaction.Begin();

    
//从队列中接收消息
    Message myMessage = myQueue.Receive(myTransaction);
    
string context = myMessage.Body as string//获取消息的内容
    myTransaction.Commit();
}

 

------------------------------------------------------------------------------

参考文章:

[1] 使用消息组件
http://msdn.microsoft.com/zh-cn/library/be74twsx.aspx

[2] MessageQueue 成员
http://msdn.microsoft.com/zh-cn/library/system.messaging.messagequeue_members.aspx

[3] MessageQueueTransaction 类
http://msdn.microsoft.com/zh-cn/library/system.messaging.messagequeuetransaction.aspx

[4] 博客园里数十篇文章
http://www.cnblogs.com/beniao/archive/2008/06/26/1229934.html
http://www.cnblogs.com/beniao/archive/2008/06/28/1230311.html
http://www.cnblogs.com/frank_xl/archive/2009/02/09/1387125.html
http://www.cnblogs.com/Henllyee/archive/2009/02/28/1400582.html
http://www.cnblogs.com/jiekeng/articles/511303.html
http://www.cnblogs.com/neozhu/category/18481.html
http://www.cnblogs.com/rickie/category/13595.html

其他高手的文章...

------------------------------------------------------------------------------

 
 

posted on 2010-03-06 02:56  WizardWu  阅读(5873)  评论(6编辑  收藏  举报