Microsoft Message Queuing

✍ dations ◷ 2024-09-20 10:42:16 #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例子:

相关

  • 选择性缄默症选择性缄默症(英语:Selective mutism)是一种社交焦虑症,患者有正常说话的能力,但在特定情境下就是说不出口。《精神疾病诊断与统计手册》把选择性缄默症描述为儿童的罕见心理失调
  • 袋鼹南方袋鼹 Notoryctes typhlops 托氏袋鼹 Notoryctes caurinus袋鼹属(学名:Notoryctes),属于哺乳纲,袋鼹目下只有袋鼹科一科,袋鼹科只有袋鼹属一属。只有两个种,但目前对其所知仍然
  • Cu铜(原子质量单位:63.546(3))共有36个同位素,其中有2个是稳定的。备注:画上#号的数据代表没有经过实验的证明,只是理论推测而已,而用括号括起来的代表数据不确定性。
  • 李孟谚李孟谚(1966年12月12日-)彰化县人,现任中华民国行政院秘书长兼各联合服务中心主任,台湾水利专家,曾任台南市代理市长。在台北县长苏贞昌任内担任水利局长,协助台北县(今新北市)脱离水
  • BenQ明基电通(英语:BenQ Corporation)为佳世达科技的一间子公司,其英文简称是BenQ,为该公司的核心品牌事业。佳世达科技隶属于明基友达集团。明基电通集团原本是属于宏碁集团内的一分
  • 南十字龙南十字龙属(属名:Staurikosaurus,意为“南十字座蜥蜴”)是恐龙总目的一属。南十字龙生活于三叠纪晚期的巴西。根据2009年的新研究,南十字龙被归类于艾雷拉龙科。南十字龙的唯一标
  • 双杂合系统酵母菌双杂合系统,英文为Yeast two-hybrid system,亦简称Two-hybrid screening,是一种研究蛋白质交互作用 或蛋白质和DNA交互作用的实验技术。此系统的基本原理是报导基因上游
  • 齐柏林飞船齐柏林飞艇(Zeppelin)是一种或一系列硬式飞艇的总称,是著名的德国飞艇设计家斐迪南·冯·齐柏林伯爵在20世纪初期以大卫·舒瓦兹(英语:David Schwarz)所设计的飞艇为蓝本,进一步发
  • 艾玛·沃森《哈利·波特》系列电影《美女与野兽》艾玛·夏洛特·杜尔热·沃森(英语:Emma Charlotte Duerre Watson,1990年4月15日-),英国女演员、模特儿兼社会活动家。她生于法国巴黎,在英格
  • 移动城市系列《移動城市系列》(英语:)是一套由英国作家菲利普·雷夫创作的奇幻小说三部曲。其中包括2001年出版的《致命引擎》、2003年出版的《獵食賞金》、2005年出版的《煉獄裝置》和2006