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微软消息队列的学习(先进先出),如需转载请自行联系原博主。

目录
相关文章
|
12天前
|
消息中间件 存储 负载均衡
消息队列学习之RabbitMQ
【4月更文挑战第3天】消息队列学习之RabbitMQ,一种基于erlang语言开发的流行的开源消息中间件。
15 0
|
16天前
|
消息中间件 存储 负载均衡
消息队列学习之kafka
【4月更文挑战第2天】消息队列学习之kafka,一个分布式的,支持多分区、多副本,基于 Zookeeper 的分布式消息流平台。
13 2
|
3月前
|
消息中间件 存储 Kafka
MQ消息队列学习入门
MQ消息队列学习入门
76 0
|
9月前
|
消息中间件 Kafka
MQ 学习日志(八) 消息队列的延时以及过期失效问题处理
消息队列的延时以及过期失效问题处理
196 0
|
9月前
|
消息中间件 存储 算法
|
9月前
|
消息中间件 大数据 Kafka
MQ 学习日志(二) 为什么使用消息队列,mq有什么优点和缺点
为什么使用消息队列,mq有什么优点和缺点
87 0
MQ 学习日志(二) 为什么使用消息队列,mq有什么优点和缺点
|
10月前
|
消息中间件 存储 NoSQL
深入学习Redis 消息队列 Stream
Stream 是 Redis 5.0 版本中新增的一种数据结构,它是一个高性能、持久化的消息队列,可以用于实现消息的发布和订阅。Stream 可以看作是一个有序的消息队列,每个消息都有一个唯一的 ID,可以根据 ID 进行消息的查找、删除和确认。在 Stream 中,消息以键值对的形式存储,可以存储任意类型的数据。Stream 还支持多个消费者组,每个消费者组可以独立消费消息,避免消息重复消费。Stream 的引入使得 Redis 在消息队列领域更具竞争力,同时也为开发者提供了一种高效、可靠的消息处理方式
315 0
深入学习Redis 消息队列 Stream
|
11月前
|
消息中间件
学习系统编程No.22【消息队列和信号量】
学习系统编程No.22【消息队列和信号量】
|
消息中间件 JavaScript
js基础笔记学习263消息队列1
js基础笔记学习263消息队列1
54 0
js基础笔记学习263消息队列1
|
消息中间件 JavaScript
js基础笔记学习264消息队列2
js基础笔记学习264消息队列2
61 0
js基础笔记学习264消息队列2