• 消息队列MSMQ的使用


    1.MSMQ安装

    控制面板-程序和功能-打开或关闭Windows功能-Microsoft Message Queue(MSMQ)服务器,选中所有,点击确定。

    2.消息队列的应用场景(转载自http://www.cnblogs.com/stopfalling/p/5375492.html

    ①异步处理

    ②应用解耦

    ③流量削锋

    ④日志处理

    ⑤消息通讯

    3.MSMQ消息分为事务性和非事务性

    非事务性的消息保存在内存中,机器重启后丢失消息队列中的消息。

    事务性的消息经过了持久化,保存在硬盘中,机器重启后队列中的消息不会丢失。

    4.测试代码

    消息发送方

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Messaging;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                MessageQueue MSMQ = CreateMessageQueue(@".private$	pmsmq");
                MSMQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
    
                Console.WriteLine("是否继续发送消息:Y/N?");
                string cmd = Console.ReadLine();
    
                while (cmd.Equals("Y"))
                {
                    Sender(MSMQ);
    
                    Console.WriteLine("是否继续发送消息:Y/N?");
                    cmd = Console.ReadLine();
                }
    
                Console.WriteLine("按任意键以停止...");
                Console.ReadKey();
            }
            private static void Sender(MessageQueue MSMQ)
            {
                try
                {
                    string random = GenerateRandom();
                    string obj = string.Format("{0} 发送方:{1}",
                        DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), random);
    
                    MSMQ.Send(obj, MessageQueueTransactionType.Single);
    
                    Console.WriteLine(obj);
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("{0} 发送方:{1}",
                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message));
                }
            }
    
            public static MessageQueue CreateMessageQueue(string path)
            {
                MessageQueue mq = null;
    
                if (MessageQueue.Exists(path))
                {
                    mq = new MessageQueue(path);
                }
                else
                {
                    mq = MessageQueue.Create(path, true);
                }
    
                return mq;
            }
    
            public static string GenerateRandom()
            {
                int seed = GetRandomSeed();
                return new Random(seed)
                    .Next(Int32.MaxValue).ToString();
            }
    
            /// <summary>
            /// 创建加密随机数生成器 生成强随机种子
            /// </summary>
            /// <returns></returns>
            private static int GetRandomSeed()
            {
                byte[] bytes = new byte[4];
                System.Security.Cryptography.RNGCryptoServiceProvider rng
                    = new System.Security.Cryptography.RNGCryptoServiceProvider();
                rng.GetBytes(bytes);
                return BitConverter.ToInt32(bytes, 0);
            }
        }
    }

    消息接收方

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Messaging;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace MessageQueueReceiver
    {
        class Program
        {
            static void Main(string[] args)
            {
                MessageQueue MSMQ = CreateMessageQueue(@".private$	pmsmq");
                MSMQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
    
                Receiver(MSMQ);
            }
    
            private static void Receiver(MessageQueue MSMQ)
            {
                while (true)
                {
                    try
                    {
                        Message m = MSMQ.Receive(MessageQueueTransactionType.Single);
                        Console.WriteLine(string.Format("{0} 接收方:[{1}]",
                            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), m.Body.ToString()));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format("{0} 接收方:{1}",
                        DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message));
                    }
                }
            }
    
            public static MessageQueue CreateMessageQueue(string path)
            {
                MessageQueue mq = null;
    
                if (MessageQueue.Exists(path))
                {
                    mq = new MessageQueue(path);
                }
                else
                {
                    mq = MessageQueue.Create(path, true);
                }
    
                return mq;
            }
        }
    }

    MessageQueue.Receive()方法是同步执行的,如果队列中没有消息,则会造成阻塞,程序会停止在这块,等待消息的产生。

  • 相关阅读:
    Nullable<T>、Nullable、null、?修饰符的区别
    使用EF CodeFirst连接MySql数据库
    C# 在项目中配置Log4net
    我的Visual Studio必用工具
    面向对象原则之一 接口隔离原则
    面向对象原则之一 依赖倒置原则
    C# ABP
    面向对象原则之一 开放封闭原则(开闭原则)
    面向对象原则之一 单一职责原则
    C# Owin初探 概念理解(一)
  • 原文地址:https://www.cnblogs.com/sxjljj/p/11440442.html
Copyright © 2020-2023  润新知