也说消息队列MSMQ

    消息队列: 是在消息传输过程中保存消息的容器。MSMQ是Microsoft的消息处理技术,运行平台为Microsoft Windows操作系统。它分为用户队列和系统队列。在用户队列中又分为专用队列:不在整个网络中发布,仅在所驻留的本地计算机上可用。专用队列只能由知道队列的完整路径名或标签的用户程序访问。

    关于消息队列的安装,可以通过控制面板->添加/删除程序->添加/删除 Windows 组件。确保包括“MSMQ HTTP 支持”在内的所有选项在详细信息页上均处于选定状态。

    如果运行的是 Windows Server 2003,请选择“应用程序服务器”来访问消息队列。

    这里我们通过实例来说明如何创建队列、发送消息、接收消息。

           创建队列:
           创建本机队列:@".\private$\队列名称"
           创建远程队列:@"FormatName:DIRECT=TCP:远程机器IP\private$\队列名称"

 

注意事项:

           发送和接受消息队列的机器都必须装MSMQ;

           在工作组模式下不能访问public 队列;

           Public 队列存在于消息网络中所有主机的消息队列中;

           Private 队列只存在于创建队列的那台机器中。

 

以下是队列的demo code:

代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
using System.Configuration;

namespace MSMQTest.Model
{
    
public class MSMQManager : IDisposable
    {
        
#region Parameters and Properties

        
private MessageQueue _msmq = null;
        
private string _path;

        
private static MSMQManager _instanceLocalComputer = new MSMQManager(true);
        
private static MSMQManager _instance = new MSMQManager(false);

        
public static MSMQManager InstanceLocalComputer
        {
            
get { return _instanceLocalComputer; }
        }

        
public static MSMQManager Instance
        {
            
get { return _instance; }
        }

        
#endregion

        
/// <summary>
        
/// Create private message query.
        
/// </summary>
        
/// <param name="transactional">Whether start transaction or not.</param>
        
/// <returns></returns>
        public bool CreatePrivateQuesry(bool transactional)
        {
            
string path = @".\private$\" + (ConfigurationManager.AppSettings["MSMQName"?? "CSMSMQ");
            
if (MessageQueue.Exists(path))
            {
                
return true;
            }
            
else
            {
                
return  (MessageQueue.Create(path, transactional) != null? true : false;
            }
        }

        
/// <summary>
        
/// Initialize message query
        
/// </summary>
        
/// <param name="isLocalComputer">Is local machine or not.</param>
        public MSMQManager(bool isLocalComputer)
        {
            
if (isLocalComputer)
            {
                _path 
= @".\private$\" + (ConfigurationManager.AppSettings["MSMQName"?? "CSMSMQ");
            }
            
else
            {
                _path 
= @"FormatName:DIRECT=TCP:192.168.1.125\private$\" + 
                    (ConfigurationManager.AppSettings[
"MSMQName"?? "CSMSMQ");
            }

            _msmq 
= new MessageQueue(_path);
        }

        
/// <summary>
        
/// Send message
        
/// </summary>
        
/// <param name="msmqIndex">The instance index.</param>
        
/// <returns></returns>
        public void Send(MSMQIndex msmqIndex)
        {
            _msmq.Send(
new Message(msmqIndex, new BinaryMessageFormatter()));
        }

        
/// <summary>
        
/// Receive message.
        
/// </summary>
        
/// <returns></returns>
        public MSMQIndex ReceiveAndRemove()
        {
            MSMQIndex msmqIndex 
= null;
            _msmq.Formatter 
= new BinaryMessageFormatter();
            Message msg 
= null;
            
try
            {
                msg 
= _msmq.Receive(new TimeSpan(001));
            }
            
catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
if (msg != null)
            {
                msmqIndex 
= msg.Body as MSMQIndex;
            }
            
return msmqIndex;
        }

        
#region IDisposable Members

        
public void Dispose()
        {
            
if (_msmq != null)
            {
                _msmq.Close();
                _msmq.Dispose();
                _msmq 
= null;
            }
        }

        
#endregion
    }
}

 

 

 

posted on 2011-02-16 11:04  Joe.W.Chen  阅读(435)  评论(0编辑  收藏  举报

导航