zoukankan      html  css  js  c++  java
  • 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; }
        }
     
    }
  • 相关阅读:
    Camera光学、成像和 3A 算法 (视觉),camera开发
    VS编程常见的编译和链接错误
    python文档下载
    如何使用微信小程序云函数发送短信验证码
    微信小程序发送短信验证码完整实例
    微信小程序如何发送短信验证码,无需搭建服务器
    微信小程序60秒倒计时
    发送短信验证码后60秒倒计时
    java实现注册的短信验证码
    java + maven 实现发送短信验证码功能
  • 原文地址:https://www.cnblogs.com/lori/p/2140388.html
Copyright © 2011-2022 走看看