代码改变世界

.net中的消息队列的使用

2012-05-09 11:51  田志良  阅读(909)  评论(0编辑  收藏  举报
以下示例讲解如何简单使用“消息队列”(Microsoft Message Queuing(MSMQ))中的“专用队列”

        .net中与“消息队列”相关的类型存在于System.Messaging命名空间中,该命名空间存在于System.Messaging.dll中,在使用时需先添加到项目的引用中。

一、安装windows消息对列
        消息队列(MSMQ)是Windows 2000、Windows XP、Windows Server 2003的一个组件,可利用”控制面板“中的”添加/删除程序“安装该组件。如下图:


二、创建队列

        在”消息队列“组件安装完成之后,可以创建所要使用的队列。创建的方式既可以在”计算机管理“MMC中手动创建,也可以通过代码创建。

(手动创建队列)
        利用代码创建队列时,要使用的是类型MessageQueue的Create()方法。

三、发送信息

    1、发送简单消息

    在消息发送前,首先要实例化MessageQueue的一个实例并指定其所对应的队列。如下代码

            //实例化MessageQueue,并指向现有的一个名称为VideoQueue队列
            MessageQueue MQ = new MessageQueue(@".\private$\VideoQueue");
            MQ.Send("消息测试","测试消息");
(代码片断1)

    2、发送复杂消息

    VideoPath是将要被当作消息的Boby来进行传递的类型,其定义如下:
    using System;
    namespace Message.Bussiness
    {
        [Serializable]
        public class VideoPath
        {
            string _sourceFilePath = string.Empty;
            string _targetFilePath = string.Empty;

            public VideoPath()
            {
            }

            public VideoPath(string sourceFilePath,string targetFilePath)
            {
                this._sourceFilePath = sourceFilePath;
                this._targetFilePath = targetFilePath;
            }

            public string SourceFilePath
            {
                get
                {
                    return this._sourceFilePath;
                }
                set
                {
                    this._sourceFilePath= value;    
                }
            }

            public string TargetFilePath
            {
                get
                {
                    return this._targetFilePath;
                }
                set
                {
                    this._targetFilePath = value;    
                }
            }
        }
    }
(代码片断2)
       对该类的要求是,该类必须要有一个默认无参的公共构造函数,所有属性必须是可读写的,且该类必须可以被序列化。利用Message将消息发送到队列中。

            MessageQueue MQ = new MessageQueue(@".\private$\VideoQueue");
            VideoPath VPath = new VideoPath(Path.Combine(Server.MapPath("."),"Video\\output.wmv"),Path.Combine(Server.MapPath("."),"Flv\\output.flv"));
            System.Messaging.Message message = new System.Messaging.Message();
            message.Label = "视频转换所用消息";
            message.Body = VPath;
            MQ.Send(message);
(代码片断3)
四、接收消息

1、接收简单类型的消息

2、接收复杂类型的消息
        对于“代码片断3”所示的消息,在接收时可按如下方法:
            MessageQueue MQ = new MessageQueue(@".\private$\VideoQueue");
             //调用MessageQueue的Receive方法接收消息
            System.Messaging.Message message = MQ.Receive( TimeSpan.FromSeconds(5));
            if ( message != null )
            {
                message.Formatter = new System.Messaging.XmlMessageFormatter(new string[]{"Message.Bussiness.VideoPath,Message"});
                VideoPath Vpath = (VideoPath)message.Body;
                Response.Write(Vpath.SourceFilePath);
            }
            else
            {
                Response.Write("没有找到消息!");
            }

       需要注意的是,如果消息是一个自定义类型对象时,必须指定Message的序列化器,即Formatter属性。不合适的Formatter是无法正确反序列化消息的。