Microsoft Message Queuing

✍ dations ◷ 2025-06-29 07:56:04 #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例子:

相关

  • 公顷公顷(hectare)为面积的公制单位(国际单位),为国际单位制并用单位之一。国际上,缩写为ha;在中国大陆的法定缩写为hm²。一块面积一公顷的土地,大约与标准足球场近似,此外,公顷也称为(HA)
  • 正子断层造影正电子发射计算机断层扫描(英语:Positron emission tomography,简称PET)是一种核医学临床检查的成像技术。PET技术是当前唯一的用解剖形态方式进行功能、代谢和受体显像的技术,具
  • FAA美国联邦航空管理局(英语:Federal Aviation Administration,缩写:FAA)是美国运输部下属、负责民用航空管理的机构;其管辖范围包括机场的建设与运行、航空交通管制、飞行员及飞机资
  • 国家元首政治主题本表纪录中华人民共和国自1949年开国至今的国家元首,在不同时期分别为建国初期的中央人民政府委员会(集体)、《五四宪法》和《八二宪法》下的中华人民共和国主席、废除
  • 湖南2019冠状病毒病湖南省疫情,介绍2019冠状病毒病疫情中,在中华人民共和国湖南省发生的情况。2020年1月21日,国家卫健委确认湖南省长沙市出现首例输入性新型冠状病毒肺炎确诊病例
  • 奥乔科奥乔科国家森林(英语:Ochoco National Forest)位于美国俄勒冈州中部、普赖恩维尔(英语:Prineville, Oregon)东北方向。森林面积850,000英亩(3,440平方千米),内有崖岩(rimrock)、峡谷、
  • 平城京平城京是日本奈良时代的京城,地处今奈良市西郊。710年(和铜三年),元明天皇迁都于此。选择定都于此,是受到道教思想“藏风得水”的风水观念影响。平城京模仿唐朝都城长安和北魏都
  • 水飞蓟水飞蓟(学名:),又名乳蓟,为菊科水飞蓟属下的一个植物种,是种中药材和食材。水飞蓟一般可以长到0.3至2.0米(1.0至6.7英尺)高,呈锥形外观。最大基底直径约为1.6米(5.3英尺)。茎带坑纹且略
  • 甘尼森国家森林甘尼森国家森林(英语:Gunnison National Forest)是美国的一座国家森林,面积6,766平方公里,坐落于美国西部科罗拉多州的德尔塔县、甘尼森县、欣斯代尔县、萨沃奇县和蒙特罗斯县五
  • 凯莱·麦肯阿尼凯莱·麦肯阿尼(英语:Kayleigh McEnany,1988年4月18日-)是美国的政治人物,在佛罗里达州坦帕长大。她的父亲是凯莱·麦肯阿尼,母亲是凯莱·麦肯阿尼,丈夫是职业棒球投手尚恩·吉尔马