Microsoft Message Queuing

✍ dations ◷ 2025-04-27 03:40:14 #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例子:

相关

  • 双胎输血综合征双胎输血综合征(TTTS,又称为胎儿-胎儿输血综合征(FFTS)及双胞胎羊水过多-羊水过少症候群(TOPS))是一个因不成比例的血供产生的并发症,由此导致高发病率及高死亡率。它可影响单绒毛膜
  • 游隼17–19, see text夏季侯鸟繁殖区  留鸟繁殖区  冬季侯鸟  过境侯鸟Falco atriceps HumeFalco kreyenborgi Kleinschmidt, 1929Falco pelegrinoides madens Ripley & W
  • 平底烧瓶平底烧瓶是实验室中使用的一种烧瓶类玻璃器皿,主要用来盛液体物质,可以轻度受热。加热时可不使用石棉网。强烈加热则应使用圆底烧瓶。底部为半球形,上部有一个长颈以便容物出入
  • 我们相信上帝“我们信仰上帝”(英语:In God We Trust)是美利坚合众国及其佛罗里达州的官方格言。这句格言首先出现于南北战争期间。因着基督教的影响,这一格言首次出现在美国于1864年发行的
  • 台湾桥头地方法院高雄市桥头区经武路911号(07)6110030 冈山简易庭:高雄市冈山区冈山南路57号 旗山简易庭:台湾桥头地方法院,简称桥头地方法院、桥头地院,是台湾的地方法院之一,位于高雄市桥头
  • The Hollywood Reporter《好莱坞报道》(英语:The Hollywood Reporter,简称为《THR》,又译为好莱坞报道者)是一本美国数码及实体印刷的杂志及网站,重点关注好莱坞电影、电视节目及娱乐界。它由威廉·威尔
  • 觉罗禅塔海觉罗禅塔海(1685年-1735年),禅塔海,清朝政治人物。雍正十二年,升任翰林院侍读学士、协理镶蓝旗蒙古事务。后兼任刑部郎中。
  • 2008年美国次贷危机name = 'Transport', description = '交通', content = {{ type = 'text', text = [[]] }, { type = 'item', original = 'articulated bus', rule = 'zh-cn:铰接客车;zh-tw
  • 印第安领地印第安领地(英语:Indian Territory)是指美国政府搬迁美洲原住民后为其预留的仍拥有所有权的土地。简单说来就是,部落割让他们原来拥有的土地,所得到的美国联邦政府从法国新获得的
  • 巴尔萨斯河巴尔萨斯河是墨西哥的河流,位于该国中南部,河道全长771公里,流域面积112,320平方公里,发源自普埃布拉,最终在拉萨罗卡德纳斯附近注入太平洋。坐标:17°56′25″N 102°08′10″W /