MSMQ微軟訊息佇列的學習(先進先出)

weixin_33766168發表於2017-12-04

學習通過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微軟訊息佇列的學習(先進先出),如需轉載請自行聯絡原博主。

相關文章