MSMQ微軟訊息佇列的學習(先進先出)
學習通過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; }
}
}
本文轉自部落格園張佔嶺(倉儲大叔)的部落格,原文連結:MSMQ微軟訊息佇列的學習(先進先出),如需轉載請自行聯絡原博主。
相關文章
- win7怎麼安裝訊息佇列 MSMQWin7佇列MQ
- 訊息佇列學習腦圖佇列
- 訊息佇列學習基礎佇列
- 訊息佇列Kafka學習總結佇列Kafka
- 微服務學習計劃——訊息佇列微服務佇列
- 訊息佇列系列一:訊息佇列應用佇列
- 訊息佇列佇列
- RabbitMQ學習(三)之 “訊息佇列高階使用”MQ佇列
- 訊息佇列(MQ)佇列MQ
- Kafka訊息佇列Kafka佇列
- RabbitMQ訊息佇列MQ佇列
- kafka 訊息佇列Kafka佇列
- POSIX訊息佇列佇列
- 訊息佇列(一)佇列
- 訊息佇列(二)佇列
- 訊息佇列二佇列
- [Redis]訊息佇列Redis佇列
- [訊息佇列]rocketMQ佇列MQ
- [訊息佇列]RabbitMQ佇列MQ
- 佇列工廠之(MSMQ)佇列MQ
- RabbitMQ 訊息佇列之佇列模型MQ佇列模型
- 【Frank.Xu】WCF分散式開發必備知識(1):MSMQ訊息佇列分散式MQ佇列
- RMQ——支援合併和優先順序的訊息佇列MQ佇列
- RabbitMQ訊息佇列(六):使用主題進行訊息分發MQ佇列
- 訊息佇列中的Oracle佇列Oracle
- Laravel 的訊息佇列剖析Laravel佇列
- 全面理解Handler-1:理解訊息佇列,手寫訊息佇列佇列
- MQ訊息佇列_RabbitMQMQ佇列
- Java面試—訊息佇列Java面試佇列
- 訊息佇列雜談佇列
- 訊息佇列二三事佇列
- rabbitmq訊息佇列原理MQ佇列
- 訊息佇列設計佇列
- 訊息佇列簡史佇列
- 訊息佇列之RabbitMQ佇列MQ
- 訊息佇列之RocketMQ佇列MQ
- 理解訊息佇列(MQ)佇列MQ
- 訊息佇列之 RabbitMQ佇列MQ