学习通过MSMQ发送简单类型的消息和复杂类型的消息

看代码:

namespace MSMQ
{
class Program
{
static void Main(string[] args)
{
const string path = @".\private$\myQueue";
MyQueue.Createqueue(path);
MyQueue.SendMessage(path, "OK1");//队列,先进先出
MyQueue.SendMessage(path, "Ok2");
MyQueue.SendMessage(path, "Ok3");
MyQueue.ReceiveMessage(path);

MyQueue.SendMessage(path, new Book { BookId = 1, BookName = "Code Complete", BookPrice = 98, BookAuthor = "zzl" });
MyQueue.SendMessage(path, new Book { BookId = 2, BookName = "Move Method", BookPrice = 47, BookAuthor = "zzl" });

Console.WriteLine(MyQueue.ReceiveEntityMessage(path));
Console.ReadKey();
}

}

/// <summary>
/// MSMQ消息队列
/// </summary>
public static class MyQueue
{
/// <summary>
/// 通过Create方法创建使用指定路径的新消息队列
/// </summary>
/// <param name="queuePath"></param>
public static void Createqueue(string queuePath)
{
try
{
if (!MessageQueue.Exists(queuePath))
{
MessageQueue.Create(queuePath);
}
else
{
Console.WriteLine(queuePath + "已经存在!");
}
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}
}

/// <summary>
/// 连接消息队列并发送消息到队列
/// </summary>
public static void SendMessage(string path, string msg)
{
try
{
//连接到本地的队列
MessageQueue myQueue = new MessageQueue(path);

Message myMessage = new Message();
myMessage.Body = msg;
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
//发送消息到队列中
myQueue.Send(myMessage);
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
}


/// <summary>
/// 连接消息队列并从队列中接收消息
/// </summary>
public static void ReceiveMessage(string path)
{
//连接到本地队列
MessageQueue myQueue = new MessageQueue(path);
myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
try
{
//从队列中接收消息
Message myMessage = myQueue.Receive();
string context = (string)myMessage.Body; //获取消息的内容
Console.WriteLine("消息内容为:" + context);
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}
catch (InvalidCastException e)
{
Console.WriteLine(e.Message);
}
}

/// <summary>
/// 清空指定队列的消息
/// </summary>
public static void ClearMessage(string path)
{
MessageQueue myQueue = new MessageQueue(path);
myQueue.Purge();
}

/// <summary>
/// 连接队列并获取队列的全部消息
/// </summary>
public static void GetAllMessage(string path)
{
//连接到本地队列
MessageQueue myQueue = new MessageQueue(path);
Message[] message = myQueue.GetAllMessages();
XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
foreach (Message t in message)
{
t.Formatter = formatter;
Console.WriteLine(t.Body.ToString());
}
}

/// <summary>
/// 连接消息队列并发送消息到队列
/// </summary>
public static bool SendMessage(string path, Book book)
{
bool flag = false;
try
{
//连接到本地的队列
MessageQueue myQueue = new MessageQueue(path);

System.Messaging.Message myMessage = new System.Messaging.Message(book);
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(Book) });
//发送消息到队列中
myQueue.Send(myMessage);
flag = true;
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
return flag;
}

/// <summary>
/// 连接消息队列并从队列中接收消息
/// </summary>
public static string ReceiveEntityMessage(string path)
{
//连接到本地队列
MessageQueue myQueue = new MessageQueue(path)
{
Formatter = new XmlMessageFormatter(new Type[] { typeof(Book) })
};
try
{
//从队列中接收消息
System.Messaging.Message myMessage = myQueue.Peek();
Book book = myMessage.Body as Book; //获取消息的内容
return string.Format("编号:{0},书名:{1},作者:{2},定价:{3}",
book.BookId,
book.BookName,
book.BookAuthor,
book.BookPrice);
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}
catch (InvalidCastException e)
{
Console.WriteLine(e.Message);
}
return null;
}


}
public interface IEntity { }
public class Book : IEntity
{
public int BookId { get; set; }
public string BookName { get; set; }
public string BookAuthor { get; set; }
public double BookPrice { get; set; }
}

}

作者:仓储大叔,张占岭,
荣誉:微软MVP