一、理论准备
MSMQ(MicroSoft Message Queue,微软消息队列)官方的解释是:在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任一位置。MSMQ通过发送和接受消息使得应用程序之间的通信变的更快和更可靠。
它的实现原理是:消息的发送者把自己想要发送的信息放入一个容器中(我们称之为Message),然后把它保存至一个系统公用空间的消息队列(Message Queue)中;本地或者是异地的消息接收程序再从该队列中取出发给它的消息进行处理。它可以存在于内存中或者是物理文件中。消息可以以两种方式发送,即快递方式(express)和可恢复模式(recoverable),它们的区别在于,快递方式为了消息的快速传递,把消息放置于内存中,而不放于物理磁盘上,以获取较高的处理能力;可恢复模式在传送过程的每一步骤中,都把消息写入物理磁盘中,以得到较好的故障恢复能力。默认应该是快递模式。
在消息传递机制中,有两个比较重要的概念。一个是消息,一个是队列。消息是由通信的双方所需要传递的信息,说白了它可以是各式各样的媒体,如文本、声音、图象,在我们编程的时候,它一般是一个类的对象或字符串等,消息最终的理解方式,为消息传递的双方事先商定,这样做的好处是,一是相当于对数据进行了简单的加密,二则采用自己定义的格式可以节省通信的传递量。队列是发送和接收消息的公用存储空间,它可以存在于内存中或者是物理文件中。
采用MSMQ带来的好处是:由于是异步通信,无论是发送方还是接收方都不用等待对方返回成功消息,就可以执行余下的代码,因而大大地提高了事物处理的能力;当信息传送过程中,信息发送机制具有一定功能的故障恢复能力;MSMQ的消息传递机制使得消息通信的双方具有不同的物理平台成为可能。在微软的.net平台上利用其提供的MSMQ功能,可以轻松创建或者删除消息队列、发送或者接收消息、甚至于对消息队列进行管理。
可创建的MSMQ类型:
“公共队列”在整个“消息队列”网络中复制,并且有可能由网络连接的所有站点访问。 路径格式为:机器名称队列名称
“专用队列”不在整个网络中发布。相反,它们仅在所驻留的本地计算机上可用。专用队列只能由知道队列的完整路径名或标签的应用程序访问。 路径格式为:机器名称Private$队列名称
“管理队列”包含确认在给定“消息队列”网络中发送的消息回执的消息。指定希望 MessageQueue 组件使用的管理队列(如果有的话)。
“响应队列”包含目标应用程序接收到消息时返回给发送应用程序的响应消息。指定希望 MessageQueue 组件使用的响应队列(如果有的话)。
二、MSMQ安装及查看
三、例子

1 namespace MSMQDemo 2 { 3 public class MsmqHelper : IDisposable 4 { 5 private readonly string path; 6 private MessageQueue mq; 7 public MsmqHelper() 8 { 9 path = @".private$YQ2"; //这是本机实例的方式(专用队列) 10 if (!MessageQueue.Exists(path)) 11 MessageQueue.Create(path); 12 mq = new MessageQueue(path); 13 mq.Formatter = new BinaryMessageFormatter(); 14 mq.Label = "description";//description 15 } 16 //析构函数 17 ~MsmqHelper() 18 { 19 mq.Dispose(); 20 //Dispose(false); 21 } 22 //发送消息 23 public void SendMessage(object message) 24 { 25 mq.Send(new Message 26 { 27 Body = message, 28 Formatter = new BinaryMessageFormatter(), 29 Recoverable = true//在消息传递过程中将消息保存到磁盘上来保证消息的传递,默认为false。 30 }); 31 } 32 //接收消息 33 public object ReceiveMessage() 34 { 35 try 36 { 37 Message receivedMsg = new Message(); 38 receivedMsg.Formatter = new BinaryMessageFormatter(); 39 receivedMsg = mq.Receive(); 40 return receivedMsg == null ? null : receivedMsg.Body; 41 } 42 catch (Exception e) 43 { 44 //log 45 return null; 46 } 47 } 48 //释放 49 public void Dispose() 50 { 51 //调用带参数的Dispose方法,释放托管和非托管资源 52 Dispose(true); 53 //手动调用了Dispose释放资源,那么析构函数就是不必要的了,这里阻止GC调用析构函数 54 System.GC.SuppressFinalize(this); 55 } 56 protected virtual void Dispose(bool disposing) 57 { 58 if (disposing) 59 { 60 //TODO:在这里加入清理"托管资源"的代码,应该是xxx.Dispose(); 61 mq.Dispose(); 62 } 63 else 64 //TODO:在这里加入清理"非托管资源"的代码 65 return;//GC.Collect(); 66 67 } 68 } 69 }

1 namespace MSMQDemo.Controllers 2 { 3 public class HomeController : Controller 4 { 5 private MsmqHelper mqHelper; 6 private static string msg = string.Empty; 7 public HomeController() 8 { 9 mqHelper = new MsmqHelper(); 10 } 11 12 public ActionResult Index() 13 { 14 new System.Threading.Thread(() => { SendMess(); }).Start(); 15 while (true) 16 { 17 var msg1 = (Message)mqHelper.ReceiveMessage(); 18 msg = (msg1.Sender + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "偷偷发送了一则电报,已被截获:" + msg1.Content); 19 Response.Write(msg + "<br />"); 20 if (Response.IsClientConnected) 21 Response.Flush(); 22 } 23 } 24 //发送消息进程 25 public void SendMess() 26 { 27 while (true) 28 { 29 mqHelper.SendMessage(new Message { Content = Guid.NewGuid().ToString("N"), Sender = "张三虫" }); 30 System.Threading.Thread.Sleep(200); 31 } 32 } 33 34 } 35 [Serializable] 36 public class Message 37 { 38 public string Sender { get; set; } 39 public string Content { get; set; } 40 } 41 }
四、效果展示
五、demo下载