首页 php教程 PHP源码 消息队列MSMQ的使用实例

消息队列MSMQ的使用实例

May 26, 2016 am 08:19 AM

消息队列:是在消息传输过程中保存消息的容器。MSMQ是Microsoft的消息处理技术,运行平台为Microsoft Windows操作系统。它分为用户队列和系统队列。在用户队列中又分为专用队列:不在整个网络中发布,仅在所驻留的本地计算机上可用。专用队列只能由知道队列的完整路径名或标签的用户程序访问。
  关于消息队列的安装,可以通过控制面板->添加/删除程序->添加/删除 Windows 组件。

  选择“消息队列”并单击“详细信息”。

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

  确保包括“MSMQ HTTP 支持”在内的所有选项在详细信息页上均处于选定状态。

  单击“确定”退出详细信息页,然后单击“下一步”。完成安装。

  

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

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

实例化消息队列

        /// <summary>
        /// </summary>
        /// <param name="isLocalComputer">是否为本机</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>
        /// 创建队列
        /// </summary>
        /// <param name="transactional">是否启用事务</param>
        /// <returns></returns>
        public bool Create(bool transactional)
        {
            if (MessageQueue.Exists(@".\private$\" + (ConfigurationManager.AppSettings["MSMQName"] ?? "CSMSMQ")))
            {
                return true;
            }
            else
            {
                if (MessageQueue.Create(@".\private$\" + (ConfigurationManager.AppSettings["MSMQName"] ?? "CSMSMQ"), transactional) != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
登录后复制

发送消息队列

        /// <summary>
        /// 发送消息队列
        /// </summary>
        /// <param name="msmqIndex">消息队列实体</param>
        /// <returns></returns>
        public void Send(MSMQIndex msmqIndex)
        {
            _msmq.Send(new Message(msmqIndex, new BinaryMessageFormatter()));
        }
登录后复制

接收消息队列,删除队列

        /// <summary>
        /// 接收消息队列,删除队列
        /// </summary>
        /// <returns></returns>
        public MSMQIndex ReceiveAndRemove()
        {
            MSMQIndex msmqIndex = null;
            _msmq.Formatter = new BinaryMessageFormatter();
            Message msg = null;
            try
            {
                msg = _msmq.Receive(new TimeSpan(0, 0, 1));
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            if (msg != null)
            {
                msmqIndex = msg.Body as MSMQIndex;
            }
            return msmqIndex;
        }
登录后复制

以上代码大致说明了消息队列的创建、发送与接收,下面给出消息队列管理器的全部代码。并在附件中给出DEMO,在您安装完成消息队列后,此DEMO可以正常运行,希望对有这方面需求的人有所帮助。

消息队列管理器

using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
using System.Configuration;
namespace MSMQTest.Model
{    
    /// 
    /// 消息队列管理器
    /// 作者:心海巨澜 xinhaijulan@gmail.com
    /// 
    public class MSMQManager : IDisposable
    {
        #region 字段与属性
        private MessageQueue _msmq = null;
        private string _path;
     
        private static MSMQManager _instanceLocalComputer = new MSMQManager(true);
        /// 
        /// 本机消息队列实例
        /// 
        public static MSMQManager InstanceLocalComputer
        {
            get { return MSMQManager._instanceLocalComputer; }
        }
        private static MSMQManager _instance = new MSMQManager(false);
        /// 
        /// 远程消息队列实例
        /// 
        public static MSMQManager Instance
        {
            get { return MSMQManager._instance; }
        }
        #endregion
        /// <summary>
        /// 创建队列
        /// </summary>
        /// <param name="transactional">是否启用事务</param>
        /// <returns></returns>
        public bool Create(bool transactional)
        {
            if (MessageQueue.Exists(@".\private$\" + (ConfigurationManager.AppSettings["MSMQName"] ?? "CSMSMQ")))
            {
                return true;
            }
            else
            {
                if (MessageQueue.Create(@".\private$\" + (ConfigurationManager.AppSettings["MSMQName"] ?? "CSMSMQ"), transactional) != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        /// 
        /// 实例化消息队列
        /// 
        /// 是否为本机
        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>
        /// 发送消息队列
        /// </summary>
        /// <param name="msmqIndex">消息队列实体</param>
        /// <returns></returns>
        public void Send(MSMQIndex msmqIndex)
        {
            _msmq.Send(new Message(msmqIndex, new BinaryMessageFormatter()));
        }
        /// <summary>
        /// 接收消息队列,删除队列
        /// </summary>
        /// <returns></returns>
        public MSMQIndex ReceiveAndRemove()
        {
            MSMQIndex msmqIndex = null;
            _msmq.Formatter = new BinaryMessageFormatter();
            Message msg = null;
            try
            {
                msg = _msmq.Receive(new TimeSpan(0, 0, 1));
            }
            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
    }
}
登录后复制

此DEMO只是消息队列的介绍与使用,还有一些消息队列概念未加说明,比如:

  消息队列的创建方式:通过路径、通过格式名、通过标签;

  消息队列接收方式:同步与异步;

  消息队列序列化:字符、XML、二进制;

  以及是否使用事务等。


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1670
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1274
29
C# 教程
1256
24