微软公司的两大主流的通讯技术之一消息队列:MessageQueue(另一个是SQL Server Service Broker)

微软公司的两大主流的通讯技术之一消息队列:MessageQueue(另一个是SQL Server Service Broker).

windows相当强悍的一个东东,具体能用来做什么,这里不啰嗦了,只贴实现的代码了,哈哈。

[c-sharp] view plaincopy
  1. using System;  
  2.     using System.Messaging;  
  3.     /// <summary>  
  4.     /// 消息队列  
  5.     /// </summary>  
  6.     public class Queue : IDisposable  
  7.     {  
  8.         protected MessageQueue queue;  
  9.   
  10.         protected TimeSpan timeout;  
  11.   
  12.         protected MessageQueueTransactionType msmqTranType = MessageQueueTransactionType.Automatic;  
  13.         /// <summary>  
  14.         /// 生成消息队列的实例对象  
  15.         /// </summary>  
  16.         /// <param name="path">消息队列路径</param>  
  17.         /// <param name="timeoutSeconds">接收消息的超时时间,以"秒"为单位</param>  
  18.         public Queue(string path, int timeoutSeconds)  
  19.         {  
  20.             //下面代码可以自动创建消息队列,无需在服务器上事先存在消息队列。  
  21.             //if (!MessageQueue.Exists(path))  
  22.             //{  
  23.             //    queue = MessageQueue.Create(path);  
  24.             //}  
  25.             //else  
  26.             //{  
  27.             //    queue = new MessageQueue(path);  
  28.             //}  
  29.             queue = new MessageQueue(path);  
  30.             timeout = TimeSpan.FromSeconds(Convert.ToDouble(timeoutSeconds));  
  31.   
  32.             queue.DefaultPropertiesToSend.AcknowledgeType = AcknowledgeTypes.None;  
  33.             queue.DefaultPropertiesToSend.AttachSenderId = false;  
  34.             queue.DefaultPropertiesToSend.Priority = MessagePriority.High;  
  35.             queue.DefaultPropertiesToSend.Recoverable = true;  
  36.             queue.DefaultPropertiesToSend.UseAuthentication = false;  
  37.             queue.DefaultPropertiesToSend.UseDeadLetterQueue = true;  
  38.             queue.DefaultPropertiesToSend.UseEncryption = false;  
  39.             queue.DefaultPropertiesToSend.UseJournalQueue = false;  
  40.             queue.DefaultPropertiesToSend.UseTracing = false;  
  41.         }  
  42.         /// <summary>  
  43.         /// 发送消息  
  44.         /// </summary>  
  45.         /// <param name="message"></param>  
  46.         protected virtual void Send(object message)  
  47.         {  
  48.             queue.Send(message, msmqTranType);  
  49.         }  
  50.         /// <summary>  
  51.         /// 接收消息  
  52.         /// </summary>  
  53.         /// <returns></returns>  
  54.         protected virtual object Receive()  
  55.         {  
  56.             try  
  57.             {  
  58.                 using (Message msg = queue.Receive(timeout, msmqTranType))  
  59.                 {  
  60.                     return msg;  
  61.                 }  
  62.             }  
  63.             catch (MessageQueueException ex)  
  64.             {  
  65.                 if (ex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)  
  66.                 {  
  67.                     throw new TimeoutException();  
  68.                 }  
  69.                 throw ex;  
  70.             }  
  71.         }  
  72.         /// <summary>  
  73.         /// 释放资源  
  74.         /// </summary>  
  75.         public void Dispose()  
  76.         {  
  77.             queue.Dispose();  
  78.         }  
  79.     }  

下面啰嗦哈消息队列的路径配置的问题(即 上面的类中的path参数):偷了个懒,哈哈

举个简单的例子来说明哈吧:

[c-sharp] view plaincopy
  1. //本机消息队列  
  2. /*公共队列,消息队列名为myQueue*/  
  3. ./myQueue  
  4. /*专用队列,消息队列名为myQueue*/  
  5. ./private$/myQueue  
  6. /*Format格式,计算机名为zhang,消息队列名为myQueue*/  
  7. FormatName:DIRECT=OS:zhang/Private$/myQueue  
  8.   
  9.   
  10. //远程消息队列  
  11. /*配置为Ip为192.168.101.130的服务器的专用消息队列,队列名为myQueue*/  
  12. FormatName:DIRECT=TCP:192.168.101.130/Private$/myQueue  

OK,下面的小demo来测试哈我们的消息队列是否可用:

[c-sharp] view plaincopy
  1. class Order  
  2.     {  
  3.         public string Id { getset; }  
  4.         public DateTime Time { getset; }  
  5.     }  
  6.     class Program  
  7.     {  
  8.         string path="FormatName:DIRECT=OS:zhang/Private$/myQueue";//队列路径  
  9.         int timeout=20;//超时时间  
  10.          Queue mq = new Queue(path,timeout);//生成消息队列对象  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Program p = new Program();  
  14.             p.SendQueue();  
  15.             p.Recive();  
  16.         }  
  17.   
  18.         void SendQueue()  
  19.         {  
  20.             Order o = new Order  
  21.             {  
  22.                 Id = "123456",  
  23.                 Time = DateTime.Now  
  24.             };  
  25.             mq.Send(o, MessageQueueTransactionType.Automatic);  
  26.         }  
  27.   
  28.         void Recive()  
  29.         {  
  30.             Message m = mq.Receive();  
  31.             Order o = (Order)m.Body;  
  32.             Console.WriteLine(o.Id);  
  33.         }  
  34.     }  

注:myQueue消息队列必须事先存在,具体的创建过程如下:

1、安装消息队列,这是windows组件,可在组件安装中找到,如图,本人系统为windows7,其他版本的安装可能会有些区别:

2、启用服务:

 

无法安装或无法启动该服务的,可以google一哈,该服务要依赖于其他的某些服务,这里不啰嗦了。

 3、创建消息队列:

计算机->管理->服务和应用程序->消息队列

 还是看图吧:

 4、OK,准备工作完成,可以运行测试了,HOHO

原文地址:https://www.cnblogs.com/Leo_wl/p/2434844.html