Microsoft Message Queuing

✍ dations ◷ 2025-11-01 20:11:39 #Windows组件

Microsoft Message Queuing或MSMQ微软公司实现的一种消息队列,始于Windows NT 4与Windows 95。Windows Server 2016与Windows 10仍然包含这种组件。1999年起,Microsoft Embedded平台以及Windows CE 3.0也开始支持这一组件。

MSMQ作为一种消息协议,允许多服务器/多进程通信,即使不总是保持互联。而sockets与其他网络协议要求直连总是成立。

MSMQ从1997年开始可用。

MSMQ是可靠分发消息。分发失败的消息保存在队列中直到目标可达时重发该消息。还支持安全与优先级的消息机制。可以创建死信队列(英语:Dead letter queue)用于调试。

MSMQ支持可持续性与不可持续性消息,使得性能与消息是否写到磁盘的一致性上可以权衡。不可持续性消息只能用于向非事务性队列发送快速消息。

MSMQ支持事务处理。允许多个动作发给多个队列中包装为单个事务。微软分布式事务协调器 (MSDTC) 支持对MSMQ或其他资源的事务访问。

MSMQ使用下述端口:

C#例子:

using System;using System.Collections.Generic;using System.Linq;using System.Messaging;using System.Text;using System.Threading.Tasks;namespace Test{    public class QueueManger    {        /// <summary>        /// 创建MSMQ队列        /// </summary>        /// <param name="queuePath">队列路径</param>        /// <param name="transactional">是否事务队列</param>        public static void Createqueue(string queuePath, bool transactional = false)        {            try            {                //判断队列是否存在                if (!MessageQueue.Exists(queuePath))                {                    MessageQueue.Create(queuePath);                    Console.WriteLine(queuePath + "已成功创建!");                }                else                {                    Console.WriteLine(queuePath + "已经存在!");                }            }            catch (MessageQueueException e)            {                Console.WriteLine(e.Message);            }        }        /// <summary>        /// 删除队列        /// </summary>        /// <param name="queuePath"></param>        public static void Deletequeue(string queuePath)        {            try            {                //判断队列是否存在                if (MessageQueue.Exists(queuePath))                {                    MessageQueue.Delete(@".\private$\myQueue");                    Console.WriteLine(queuePath + "已删除!");                }                else                {                    Console.WriteLine(queuePath + "不存在!");                }            }            catch (MessageQueueException e)            {                Console.WriteLine(e.Message);            }        }        /// <summary>        /// 发送消息        /// </summary>        /// <typeparam name="T">用户数据类型</typeparam>        /// <param name="target">用户数据</param>        /// <param name="queuePath">队列名称</param>        /// <param name="tran"></param>        /// <returns></returns>        public static bool SendMessage<T>(T target, string queuePath, MessageQueueTransaction tran = null)        {            try            {                //连接到本地的队列                MessageQueue myQueue = new MessageQueue(queuePath);                System.Messaging.Message myMessage = new System.Messaging.Message();                myMessage.Body = target;                myMessage.Formatter = new XmlMessageFormatter(new Type { typeof(T) });                //发送消息到队列中                if (tran == null)                {                    myQueue.Send(myMessage);                }                else                {                    myQueue.Send(myMessage, tran);                }                Console.WriteLine("消息已成功发送到"+queuePath + "队列!");                return true;            }            catch (ArgumentException e)            {                Console.WriteLine(e.Message);                return false;            }        }        /// <summary>        /// 接收消息        /// </summary>        /// <typeparam name="T">用户的数据类型</typeparam>        /// <param name="queuePath">消息路径</param>        /// <returns>用户填充在消息当中的数据</returns>        public static T ReceiveMessage<T>(string queuePath,MessageQueueTransaction tran=null)        {            //连接到本地队列            MessageQueue myQueue = new MessageQueue(queuePath);            myQueue.Formatter = new XmlMessageFormatter(new Type { typeof(T) });            try            {                //从队列中接收消息                System.Messaging.Message myMessage = tran == null ? myQueue.Receive() : myQueue.Receive(tran);                return (T)myMessage.Body; //获取消息的内容            }            catch (MessageQueueException e)            {                Console.WriteLine(e.Message);            }            catch (InvalidCastException e)            {                Console.WriteLine(e.Message);            }            return default(T);        }        /// <summary>        /// 采用Peek方法接收消息        /// </summary>        /// <typeparam name="T">用户数据类型</typeparam>        /// <param name="queuePath">队列路径</param>        /// <returns>用户数据</returns>        public static T ReceiveMessageByPeek<T>(string queuePath)        {            //连接到本地队列            MessageQueue myQueue = new MessageQueue(queuePath);            myQueue.Formatter = new XmlMessageFormatter(new Type { typeof(T) });            try            {                //从队列中接收消息                System.Messaging.Message myMessage = myQueue.Peek();                return (T)myMessage.Body; //获取消息的内容            }            catch (MessageQueueException e)            {                Console.WriteLine(e.Message);            }            catch (InvalidCastException e)            {                Console.WriteLine(e.Message);            }            return default(T);        }        /// <summary>        /// 获取队列中的所有消息        /// </summary>        /// <typeparam name="T">用户数据类型</typeparam>        /// <param name="queuePath">队列路径</param>        /// <returns>用户数据集合</returns>        public static List<T> GetAllMessage<T>(string queuePath)        {            MessageQueue myQueue = new MessageQueue(queuePath);            myQueue.Formatter = new XmlMessageFormatter(new Type { typeof(T) });            try            {                Message msgArr=  myQueue.GetAllMessages();                List<T> list=new List<T>();                msgArr.ToList().ForEach((o) =>                 {                    list.Add((T)o.Body);                });                return list;            }            catch(Exception e)            {                Console.WriteLine(e.Message);            }            return null;        }    }}namespace Test{    public class Student    {        /// <summary>        /// 年龄        /// </summary>        public int Age { get; set; }        /// <summary>        /// 姓名        /// </summary>        public string Name { get; set; }    }}namespace Test{    class Program    {        static void Main(string args)        {            string queuepath = @".\private$\myQueue";            //QueueManger.Createqueue(queuepath);            //Student stu = new Student() { Name="shaoshun",Age=18};            //QueueManger.SendMessage<Student>(stu, queuepath);            //Student stu=  QueueManger.ReceiveMessageByPeek<Student>(queuepath);            //Student stu = QueueManger.ReceiveMessage<Student>(queuepath);            //Console.WriteLine(stu.Name);            QueueManger.Deletequeue(queuepath);            QueueManger.Createqueue(queuepath);            MessageQueueTransaction tran = new MessageQueueTransaction();            tran.Begin();            try            {                Student stu;                for (int i = 0; i < 4; i++)                {                    stu=new Student(){Name="shaoshun"+i,Age=i};                    QueueManger.SendMessage<Student>(stu, queuepath, tran);                    if (i == 3)                    {                        throw new Exception();                    }                }                tran.Commit();            }            catch            {                tran.Abort();            }            Console.ReadKey();        }    }}

C语言调用Windows API例子:

相关

  • 自然选择自然选择(英语:natural selection,传统上也译为天择)指生物的遗传特征在生存竞争中,由于具有某种优势或某种劣势,因而在生存能力上产生差异,并进而导致繁殖能力的差异,使得这些特征
  • 禽类鸟是鸟纲(学名:Aves)动物的通称,是唯一存活至今的恐龙,现代所有鸟类在生物学上也被分类为鸟形恐龙(即鸟翼类)的一部分;鸟纲的全体成员均为两足、恒温、卵生、身披羽毛且色彩鲜艳各异
  • 生物统计生物统计学(有时也称生物计量学)是统计学的原理和方法在生物学研究中的应用,是一门应用数学,最常见的是应用于医学。在生物学、医学、农学等的研究中,合理地进行调查或实验设计,科
  • 木瓜木瓜可以指以下事物:
  • 罗兰弗兰克·舍伍德·罗兰(英语:Frank Sherwood Rowland,1927年6月28日-2012年3月10日),美国化学家,因“他们对大气化学的研究工作,特别是臭氧的形成与分解”,与马里奥·莫利纳、保罗·克
  • 维堡维堡(Viborg)是位于丹麦中日德兰大区的一个城市。维堡是中日德兰大区的行政中心所在地。此外维堡还是日德兰半岛高等法院、西部高等法院的所在地。维堡是丹麦最大的城市之一,辖
  • 燕巢总机厂燕巢总机厂是台湾高铁位于高雄市燕巢区的总维修机厂,占地约58公顷,负责高铁列车的四至五级维修以及接收和组装新列车。此机厂设有一条测试线,作为高铁列车大修完成后的测试轨道
  • 吉隆建设吉隆建股份有限公司(简称吉隆建设)是一家总部设立于高雄市的建筑营造商,1982年8月23日成立,多次获得国家建筑金奖,曾经多次获得台湾诚信建商。吉隆礼邻、汤之园、湖滨艺墅、名人
  • 硼矿硼是一种非金属元素,在地壳中分散状态的硼矿非常多,而且是地表水、地下水、岩浆喷气、矿泉水和所有岩层的气液包中所具有的元素,硼矿物几乎在地质里的所有阶段都可以形成,从岩浆
  • 游廓游廓(日语:遊廓/ゆうかく )是江户时代集中官方认可的游女屋(妓院),以围墙、水沟等所包围的区画。集中成一区画的目的是便于治安、风纪的管理。成立于安土桃山时代。有游里、色町、