65.9K
CodeProject 正在变化。 阅读更多。
Home

消息队列

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.94/5 (37投票s)

2018年9月14日

CPOL

2分钟阅读

viewsIcon

46423

downloadIcon

1095

本文介绍了 MSMQ 技术(发布者和订阅者),以便在异构网络和平台中的分布式系统之间进行通信。

消息队列

如今,我们需要更多地在分布式系统之间进行通信。这是因为设备的种类繁多,并且为了增强便利性和便利性而扩展了通信。最大的问题是选择最佳和最合适的技术来连接每个生态系统和环境。

消息队列是一种在整个平台发布消息的技术,它可靠、可扩展、简单、线程安全且方便调试。 MSMQ 允许我们在异构网络和平台上发布消息。

它的一个应用是在物联网中使用,其中存在异构环境中高度解耦的设备。

在这种情况下,有发送者和接收者,如下图所示

消息队列的另一个竞争对手是 Web 服务。在 Web 服务中,如果我们丢失消息,大部分错误处理都由客户端承担,而消息队列中的持久性更高。

如何使用 MSMQ

MSMQ 基于 Windows 功能。可以安装 Microsoft 消息队列
控制面板 -> 打开或关闭 Windows 功能 -> (选择) Microsoft Message Queue (MSMQ) 服务器

简单 MSMQ

现在是编写代码的时候了

步骤 1:创建 Web 站点作为发布者

选择项目类型是可选的,例如,它可以是 Windows 应用程序或 WPF 作为发布者

文件 -> 新建 -> Web 站点

步骤 2:添加 System.Messaging 引用 

右键单击“引用” -> 选择“添加引用”

步骤 3:创建消息实体

我们可以将消息作为对象传递给发送者,该对象具有以下结构,并具有 Serializable 属性,以便在发送方使用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MSMQWebApp
{
    [Serializable]
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

步骤 4:创建队列并发布消息

为了访问 Windows 队列

控制面板 -> 管理工具 -> 计算机管理 -> 服务和应用程序 -> 消息队列

重要的部分是创建一个路径,该路径与发布和订阅系统的主题相同。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Messaging;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        protected void Button1_Click(object sender, EventArgs e)
        {
            System.Messaging.Message msg = new System.Messaging.Message();
            msg.Label = "Hello From The Web Application";
            msg.Body = "This is body";
            
            MessageQueue mq = new MessageQueue(".\\private$\\WebWinMsg");
            
            mq.Path = @".\private$\WebWinMsg";
            

            if (MessageQueue.Exists(mq.Path) == false)
            {
                MessageQueue.Create(mq.Path);
            }
            else
            {
                mq = new MessageQueue(mq.Path);
            }
            mq.Send(msg, "For Windows Application");
        }

        protected void SendObj_Click(object sender, EventArgs e)
        {
            System.Messaging.Message msg = new System.Messaging.Message();
            msg.Label = "Hello From The Web Application Object";
           
            List<product> product = new List<product>()
            {
                new Product{ Id=1, Name="Product A" },
                new Product{ Id=2, Name="Product B" }
            };
            msg.Body = product;

            MessageQueue mq = new MessageQueue(".\\private$\\WebWinMsgObj");
            
            mq.Path = @".\private$\WebWinMsgObj";


            if (MessageQueue.Exists(mq.Path) == false)
            {
                //Queue does not exist so create it
                MessageQueue.Create(mq.Path);
            }
            else
            {
                mq = new MessageQueue(mq.Path);
            }
            mq.Send(product);
        }
    }
}

步骤 5:使用队列并接收消息

我们应该创建另一个应用程序,以便在任何平台上使用消息。我使用了 Windows 应用程序,如下所示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Messaging;

namespace MSMQWinApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Receive_Click(object sender, EventArgs e)
        {
            MessageQueue mq = new MessageQueue(".\\private$\\WebWinMsg");
            System.Messaging.Message msg = new System.Messaging.Message();

            msg = mq.Receive();
            
            msg.Formatter = new XmlMessageFormatter(new String[] { "System.String,mscorlib" });
            string m = msg.Body.ToString();

            this.listBox1.Items.Add(m.ToString());
        }

        private void Receive_Object_Click(object sender, EventArgs e)
        {
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            MessageQueue mq = new MessageQueue(".\\private$\\WebWinMsgObj");
            // Set the formatter to indicate body contains an Order.
            mq.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(List<msmqwebapp.product>)});

            try
            {
                // Receive and format the message. 
                System.Messaging.Message msg = mq.Receive();
                //string str= msg.Body.ToString();

                List<msmqwebapp.product> productObj = (List<msmqwebapp.product>)msg.Body;


                this.listBox2.Items.Add(productObj.Where(x => x.Id == 2).FirstOrDefault().Name);
            }

            catch (MessageQueueException)
            {

            }
        }
    }
}

步骤 5:如何运行

因为我们希望看到上面的两个应用程序都在运行,所以我们应该配置多个启动项。

然后我们将有

如您在上图中所见,有两种不同的发送方式:发送和发送对象。

“发送”按钮将发送带有正文的简单消息,而另一个按钮将发送可以按 linq 查询筛选的对象;

首先单击 Web 浏览器上的“发送”,然后在 Windows 应用程序上单击“接收”;这是针对简单 msmq 的。

其次,单击 Web 浏览器上的“发送对象”,然后在 Windows 应用程序上单击“接收对象”;这是用于在 msmq 中发送对象作为消息,该消息具有通过 linq 查询进行筛选的功能。

© . All rights reserved.