• MSMQ 微软消息队列 示例


    MSMQ它的实现原理是:消息的发送者把自己想要发送的信息放入一个容器中(我们称之为Message),然后把它保存至一个系统公用空间的消息队列(Message Queue)中;本地或者是异地的消息接收程序再从该队列中取出发给它的消息进行处理。

    我个人的理解,你可以把他当做一种,把数据打包后,发送到一个地方,程序也可以去取到这个打包的程序,队列的机制就不讲了,并发问题荡然无存。呵呵。

    上代码:

    首先using System.Messaging;

        public class MsmqManagerHelper
        {
            private readonly string _path;
            private MessageQueue _msmq;
            public MsmqManagerHelper()
            {
                _path = @".\private$\给服务实例起个名字";  //这是本机实例的方式
                if (!MessageQueue.Exists(_path))
                {
                    MessageQueue.Create(_path);
                }
                _msmq = new MessageQueue(_path);
            }

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

            /// <summary>
            /// 接收消息队列,删除队列
            /// </summary>
            /// <returns></returns>
            public object ReceiveAndRemove()
            {
                object 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;
                }
                return msmqIndex;
            }
            

            /// <summary>
            /// 释放消息队列实例
            /// </summary>
            public void Dispose()
            {
                if (_msmq != null)
                {
                    _msmq.Close();
                    _msmq.Dispose();
                    _msmq = null;
                }
            }
        }

    以上是一个helper类,下面介绍调用方法。

        class Program
        {
            static void Main()
            {
                var mmh = new MsmqManagerHelper();
                Console.WriteLine("测试MSMQ工作");
                Console.WriteLine("发送消息");
                string receiveKey = Console.ReadLine();
                try
                {
                        var m=new Model();
                        m.ID = 1;
                        m.Name = receiveKey;
                        mmh.Send(m);      
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                Console.ReadLine();
                Console.WriteLine("开始接受消息...");
                while (true)
                {
                    object obj = mmh.ReceiveAndRemove();
                    if (obj != null)
                    {
                        var item = (Model) obj;
                        Console.WriteLine(item.ID+ ":" + item.Name);
                    }
                    else
                    {
                        break;
                    }
                    Thread.Sleep(200);
                }
                Console.ReadLine();
            }
        }

  • 相关阅读:
    jmeter入门----安装及简单使用
    python的学习路线
    利用selenium模块来编写网络爬虫
    软件测试的一些建议
    作为面试官的一些心得
    测试中常用的python第三方库
    Bootstrap:Bootstrap_table第一篇:快速用bootstrap_table(支持参数)筛选并展示数据,固定表格前几列,实现表格单元格编辑
    JS插件:fullCalendar图解
    Python脚本:实现对象集合List导入到excel表格,支持mysql,postergrsql,MongoDB
    Python脚本:实现excel表格导入到数据库,支持mysql,postgresql,MongoDB
  • 原文地址:https://www.cnblogs.com/isdavid/p/2642867.html
Copyright © 2020-2023  润新知