zoukankan      html  css  js  c++  java
  • NetMQ 消息队列

    忘记是看到哪个博客写的了,如有侵权,请见谅!!

    1.辅助Helper类 (添加System.Messaging引用)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Messaging;
    namespace ToolService
    {
        public class MQHelp
        {
            /// <summary>
            /// 创建一个消息队列
            /// </summary>
            /// <param name="name">消息队列的名称</param>
            /// <returns>是否创建成功</returns>
            public static bool CreateNewMQ(string name)
            {
                try
                {
                    if (!MessageQueue.Exists(".\private$\" + name))
                    {
                        MessageQueue mq = MessageQueue.Create(".\private$\" + name);
                        mq.Label = "private$\" + name;
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
    
                    throw ex;
                }
    
            }
            /// <summary>
            /// 删除一个消息队列
            /// </summary>
            /// <param name="name">消息队列的名称</param>
            /// <returns>是否删除成功</returns>
            public static bool DeleteNewMQ(string name)
            {
                try
                {
                    if (!MessageQueue.Exists(".\private$\" + name))
                    {
                        MessageQueue.Delete(".private$\" + name);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
    
                    throw ex;
                }
    
            }
            /// <summary>
            /// 发送消息到指定消息队列
            /// </summary>
            /// <param name="mq_name">消息队列名</param>
            /// <param name="msg_lable">消息头</param>
            /// <param name="msg_body">消息体</param>
            public static void SendMssage(string mq_name, string msg_lable, string msg_body)
            {
                try
                {
                    MessageQueue mq = new MessageQueue(@".private$"+mq_name);
                    Message message = new Message();
                    message.Label = msg_lable;
                    message.Body = msg_body;
                    mq.Send(message);
                }
                catch (Exception ex)
                {
    
                    throw ex;
                }
            }
            /// <summary>
            /// 从指定消息队列获取第一条消息
            /// </summary>
            /// <param name="mq_name">消息队列名称</param>
            /// <returns>消息内容</returns>
            public static Message ReceiveMessage(string mq_name)
            {
                try
                {
                    MessageQueue mq = new MessageQueue(@".private$" + mq_name);
                    if (mq.GetAllMessages().Length > 0)
                    {
                        Message message = mq.Receive();
                        if (message != null)
                        {
                            message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
                        }
                        return message;
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception ex)
                {
    
                    throw ex;
                }
               
            }
    
        }
    }
    

    2.执行

    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 ToolService;
    using System.Timers;
    using System.Messaging;
    using System.Windows;
    namespace WindowsMQSend
    {
        public partial class FromShow : Form
        {
            private static readonly string mq_name = "textMQ";
            private System.Timers.Timer timer;
            public FromShow()
            {
                InitializeComponent();
                MQHelp.CreateNewMQ(mq_name);
                timer = new System.Timers.Timer();
                timer.Interval = 1000;
                timer.Elapsed += new System.Timers.ElapsedEventHandler(flash);
                timer.Start();
            }
    
            private void btnSend_Click(object sender, EventArgs e)
            {
                MQHelp.SendMssage(mq_name,"test",txtInput.Text);
            }
            private void flash(object sender, ElapsedEventArgs e)
            {
                var message = MQHelp.ReceiveMessage(mq_name);
                if (message != null)
                {
                    Action hide = delegate () {
                        txtResult.Text += message.Id + " " + message.Label + " " + message.Body + "
    ";
                    };
                    this.BeginInvoke(hide);
                }
            }
        }
    }
    

     

  • 相关阅读:
    数论-剩余类、完全剩余系、缩系、欧拉函数
    数论-同余式
    计算机科研项目中的重点项目、重大项目、重大研究计划项目,重点研发计划有什么区别和联系?
    VFS虚拟文件系统
    git中文名转义带来的麻烦;git配置之core.quotepath;git中文乱码
    nodejs 与 npm 配置
    mongodb 部署 安装 使用 记录
    GPG error: https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 656408E390CFB1F5
    slurm 网路监控软件使用
    Win10 64位,北航研究生教务系统文件打印办法(旧版GSMIS),只要三步就能解决;
  • 原文地址:https://www.cnblogs.com/eimers/p/9086682.html
Copyright © 2011-2022 走看看