Microsoft Message Queuing

✍ dations ◷ 2025-11-24 00:32:47 #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例子:

相关

  • 丰塔纳丰塔纳 (马耳他语:Il-Fontana)是马耳他戈佐岛上的一个村庄,2014年3月时人口为985人。丰塔纳源于维多利亚的城郊区,临近Rabat-Xlendi路。它在当地被称为"It-Triq tal-Għajn",即“
  • 闵桂荣闵桂荣(1933年6月2日-),福建莆田人,中国工程热物理学及空间技术专家、中国空间技术研究院研究员。曾任全国政协第八、第九届委员,中国科协第三、第四、第五届常委。闵桂荣1952年毕
  • 总统府中华民国总统府厅舍位于台湾台北市博爱特区,为中华民国总统、副总统以及总统府幕僚单位的办公场所,也是每年元旦及国庆日举行升旗典礼的地点。厅舍建于1919年,建筑风格属辰野金
  • 克劳斯-玛菲克劳斯-玛菲(德语:Krauss-Maffei)是德国一家机械工程企业,总部设于慕尼黑。克劳斯-玛菲是全球领先的塑料及天然橡胶机械设备企业,并在该领域拥有全球唯一的机械技术:注塑和反应发
  • 郑板桥《清代学者像传》第一集《郑燮像》,叶衍兰绘郑燮(1693年11月22日-1766年1月22日),字克柔,号板桥、板桥道人,江苏省淮扬海道扬州府兴化县(今江苏省泰州市兴化市大垛镇)人,祖籍苏州阊门,
  • 龙马白龙马(又叫小白龙)即白色的龙马,是中国古代的传说生物。在《西游记》中,唐僧的座骑白马,被龙王三太子所误食,后因龙王三太子其实已为观音菩萨所点化,原是要保助唐僧上西天取经,因此
  • 汤姆森集团汤森路透公司(英语:Thomson Reuters Corporation,简称:汤森路透)是一家跨国大众媒体及资讯提供商,由汤森公司于2008年4月17日并购路透集团而成立。总部位于加拿大多伦多,并且在全球
  • 阿里埃勒·鲁宾斯坦阿里埃勒·鲁宾斯坦(希伯来语:אריאל רובינשטיין‎,1951年4月13日-),以色列经济学家,研究方向为博弈论。1972 - 1979年就读于耶路撒冷希伯来大学。2006年起任特拉维
  • 秆锈病秆锈病,又称柄锈病、麦锈病、黑锈病,是由真菌锈菌(学名:)所引发的疾病,以谷类作物为感染大宗。小麦的秆锈传染病则是由名为“”的变种秆锈菌所引起,这种秆锈菌普遍在非洲、亚洲蔓延
  • 电台广播电台广播,又称无线电广播(英语:Radio broadcasting)、声音广播或收音机广播,是以无线电波单向传递声音信息的方式,一般是以高频广播,透过大气电波发送广播频率后,听众透过收音机来接