windows相当强悍的一个东东,具体能用来做什么,这里不啰嗦了,只贴实现的代码了,哈哈。
- using System;
- using System.Messaging;
- /// <summary>
- /// 消息队列
- /// </summary>
- public class Queue : IDisposable
- {
- protected MessageQueue queue;
- protected TimeSpan timeout;
- protected MessageQueueTransactionType msmqTranType = MessageQueueTransactionType.Automatic;
- /// <summary>
- /// 生成消息队列的实例对象
- /// </summary>
- /// <param name="path">消息队列路径</param>
- /// <param name="timeoutSeconds">接收消息的超时时间,以"秒"为单位</param>
- public Queue(string path, int timeoutSeconds)
- {
- //下面代码可以自动创建消息队列,无需在服务器上事先存在消息队列。
- //if (!MessageQueue.Exists(path))
- //{
- // queue = MessageQueue.Create(path);
- //}
- //else
- //{
- // queue = new MessageQueue(path);
- //}
- queue = new MessageQueue(path);
- timeout = TimeSpan.FromSeconds(Convert.ToDouble(timeoutSeconds));
- queue.DefaultPropertiesToSend.AcknowledgeType = AcknowledgeTypes.None;
- queue.DefaultPropertiesToSend.AttachSenderId = false;
- queue.DefaultPropertiesToSend.Priority = MessagePriority.High;
- queue.DefaultPropertiesToSend.Recoverable = true;
- queue.DefaultPropertiesToSend.UseAuthentication = false;
- queue.DefaultPropertiesToSend.UseDeadLetterQueue = true;
- queue.DefaultPropertiesToSend.UseEncryption = false;
- queue.DefaultPropertiesToSend.UseJournalQueue = false;
- queue.DefaultPropertiesToSend.UseTracing = false;
- }
- /// <summary>
- /// 发送消息
- /// </summary>
- /// <param name="message"></param>
- protected virtual void Send(object message)
- {
- queue.Send(message, msmqTranType);
- }
- /// <summary>
- /// 接收消息
- /// </summary>
- /// <returns></returns>
- protected virtual object Receive()
- {
- try
- {
- using (Message msg = queue.Receive(timeout, msmqTranType))
- {
- return msg;
- }
- }
- catch (MessageQueueException ex)
- {
- if (ex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
- {
- throw new TimeoutException();
- }
- throw ex;
- }
- }
- /// <summary>
- /// 释放资源
- /// </summary>
- public void Dispose()
- {
- queue.Dispose();
- }
- }
下面啰嗦哈消息队列的路径配置的问题(即 上面的类中的path参数):偷了个懒,哈哈
举个简单的例子来说明哈吧:
- //本机消息队列
- /*公共队列,消息队列名为myQueue*/
- ./myQueue
- /*专用队列,消息队列名为myQueue*/
- ./private$/myQueue
- /*Format格式,计算机名为zhang,消息队列名为myQueue*/
- FormatName:DIRECT=OS:zhang/Private$/myQueue
- //远程消息队列
- /*配置为Ip为192.168.101.130的服务器的专用消息队列,队列名为myQueue*/
- FormatName:DIRECT=TCP:192.168.101.130/Private$/myQueue
OK,下面的小demo来测试哈我们的消息队列是否可用:
- class Order
- {
- public string Id { get; set; }
- public DateTime Time { get; set; }
- }
- class Program
- {
- string path="FormatName:DIRECT=OS:zhang/Private$/myQueue";//队列路径
- int timeout=20;//超时时间
- Queue mq = new Queue(path,timeout);//生成消息队列对象
- static void Main(string[] args)
- {
- Program p = new Program();
- p.SendQueue();
- p.Recive();
- }
- void SendQueue()
- {
- Order o = new Order
- {
- Id = "123456",
- Time = DateTime.Now
- };
- mq.Send(o, MessageQueueTransactionType.Automatic);
- }
- void Recive()
- {
- Message m = mq.Receive();
- Order o = (Order)m.Body;
- Console.WriteLine(o.Id);
- }
- }
注:myQueue消息队列必须事先存在,具体的创建过程如下:
1、安装消息队列,这是windows组件,可在组件安装中找到,如图,本人系统为windows7,其他版本的安装可能会有些区别:
2、启用服务:
无法安装或无法启动该服务的,可以google一哈,该服务要依赖于其他的某些服务,这里不啰嗦了。
3、创建消息队列:
计算机->管理->服务和应用程序->消息队列
还是看图吧:
4、OK,准备工作完成,可以运行测试了,HOHO