百度空间 | 百度首页 
               
 
查看文章
 
MSMQ消息队列
2006年12月14日 星期四 下午 09:24

MSMQ消息队列
“消息”是在两台计算机间传送的数据单位。消息可以非常简单,例如只包含文本字符串;也可以更复杂,可能包含嵌入对象。

消息被发送到队列中。“消息队列”是在消息的传输过程中保存消息的容器。消息队列管理器在将消息从它的源中继到它的目标时充当中间人。队列的主要目的是提供路由并保证消息的传递;如果发送消息时接收者不可用,消息队列会保留消息,直到可以成功地传递它。

“消息队列”是 Microsoft 的消息处理技术,它在任何安装了 Microsoft Windows 的计算机组合中,为任何应用程序提供消息处理和消息队列功能,无论这些计算机是否在同一个网络上或者是否同时联机。

“消息队列网络”是能够相互间来回发送消息的任何一组计算机。网络中的不同计算机在确保消息顺利处理的过程中扮演不同的角色。它们中有些提供路由信息以确定如何发送消息,有些保存整个网络的重要信息,而有些只是发送和接收消息。

“消息队列”安装期间,管理员确定哪些服务器可以互相通信,并设置特定服务器的特殊角色。构成此“消息队列”网络的计算机称为“站点”,它们之间通过“站点链接”相互连接。每个站点链接都有一个关联的“开销”,它由管理员确定,指示了经过此站点链接传递消息的频率。

“消息队列”管理员还在网络中设置一台或多台作为“路由服务器”的计算机。路由服务器查看各站点链接的开销,确定经过多个站点传递消息的最快和最有效的方法,以此决定如何传递消息。 

消息队列使应用程序开发人员能够方便地通过发送和接收消息来与应用程序进行快捷、可靠的通讯。消息处理为您提供有保证的消息传送,以及一种可靠的、防故障的方法来实现许多业务处理。

MessageQueue 组件使您可以方便地将基于消息的通讯合并到应用程序中。通过该组件及其关联的语言功能,可以使用简单的编程模型来发送和接收消息,浏览现有队列,创建和删除队列以及执行各种其他操作。

如何向消息队列发送消息

using System;
using System.Messaging;

public class MQSend
{
    public static void Main(String[] args)
    {
        string appName = Environment.GetCommandLineArgs()[0];

        if(args.Length != 2) {
            Console.WriteLine("用法:" + appName +" <queue> <message>");
        } else {
            string mqPath = ".\\" + args[0];
            if(!MessageQueue.Exists(mqPath)) {
                MessageQueue.Create(mqPath);
            }

            MessageQueue mq = new MessageQueue(mqPath);
            mq.Send(args[1]);
        }

        Console.WriteLine();
        Console.WriteLine("按 Enter 键继续...");
        Console.ReadLine();
    }
}

如何从消息队列接收消息

using System;
using System.Messaging;
using System.IO;
using System.Runtime.Serialization;

public class MQReceive {
    public static void Main(String[] args)
    {
        string appName = Environment.GetCommandLineArgs()[0];

        if ( args.Length != 1 ) {
            Console.WriteLine("用法:{0} <queue>", appName);
        } else {
            string mqPath = ".\\" + args[0];

            if ( !MessageQueue.Exists(mqPath) ) {
                Console.WriteLine("队列“{0}”不存在!", mqPath);
                return;
            }

            MessageQueue mq = new MessageQueue(mqPath);
            ((XmlMessageFormatter)mq.Formatter).TargetTypeNames = new string[]{"System.String,mscorlib"};

            try {
                Message m = mq.Receive(new TimeSpan(0,0,3));
                Console.WriteLine("消息:{0}", (string)m.Body);
            }
            catch ( MessageQueueException ) {
                Console.WriteLine("队列中没有消息");
                return;
            }
            catch ( InvalidOperationException ) {
                Console.WriteLine("从队列中移除的消息不是字符串");
                return;
            }
        }

        Console.WriteLine();
        Console.WriteLine("按 Enter 键继续...");
        Console.ReadLine();
    }
}
如何通过消息队列传递复杂类型 
using System;

public class Customer
{
    protected string lastName;
    protected string firstName;
    
    public string LastName{
        get{
            return this.lastName;
        }
        set{
            this.lastName = value;
        }
    }

    public string FirstName{
        get{
            return this.firstName;
        }
        set{
            this.firstName = value;
        }
    }    

    public string FullName{
        get{
            return (FirstName + LastName);
        }
    }
}

using
 System; using System.Messaging;      public class SendObject {         public static void Main(String[] args)      {         string mqPath = ".\\MQSendSample";         if(!MessageQueue.Exists(mqPath))         {             MessageQueue.Create(mqPath);         }                  MessageQueue mq = new MessageQueue(mqPath);         Customer customer = new Customer();         customer.LastName = "Copernicus";         customer.FirstName = "Nicolaus";                 mq.Send(customer);             } }








如何异步接收
using System;
using System.Messaging;
using System.Threading;

public class MQReceive {
    public static void Main(String[] args)
    {
        string appName = Environment.GetCommandLineArgs()[0];

        if ( args.Length != 1 ) {
            Console.WriteLine("用法:{0} <queue>", appName);
            return;
        }

        string mqPath = ".\\" + args[0];

        if ( !MessageQueue.Exists(mqPath) ) {
            Console.WriteLine("队列“{0}”不存在!", mqPath);
        }
        else {
            MessageQueue mq = new MessageQueue(mqPath);
            ((XmlMessageFormatter)mq.Formatter).TargetTypeNames = new string[]{"System.String,mscorlib"};

            mq.ReceiveCompleted += new ReceiveCompletedEventHandler(OnReceiveCompleted);
            mq.BeginReceive();
        }

        Console.WriteLine("按 Enter 键退出该示例");
        Console.ReadLine();
    }

    public static void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult){
        MessageQueue mq = (MessageQueue)source;
        Message m = mq.EndReceive(asyncResult.AsyncResult);
        m.Formatter = new XmlMessageFormatter(new string[]{"System.String, mscorlib"});
        Console.WriteLine("消息:{0}", (string)m.Body);
        mq.BeginReceive();
    }
}


类别:技术 | 添加到搜藏 | 浏览() | 评论 (2)
 
最近读者:
 
网友评论:
1
2006年12月29日 星期五 下午 11:46 | 回复
由于公司的需要,我正在学习这方面的知道,有很多不明白的地方,希望您能够指点少许,谢谢! MSN:leolws@hotmail.com e-mail: allen_lws@163.com
 
2
2007年04月12日 星期四 下午 09:22 | 回复
不错
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu