MQ消息完整流程

public class QueueManger
    {
        private static string QueuePath = @".\private$\{0}";
            
        /// 
        /// 创建MSMQ队列
        /// 
        /// 队列路径
        /// 是否事务队列
        public static void Createqueue(string queueName, bool transactional = false)
        {
            try
            {
                QueuePath = string.Format(QueuePath, queueName);
                //判断队列是否存在
                if (!MessageQueue.Exists(QueuePath))
                {
                    MessageQueue.Create(QueuePath);
                    LoggerFile.Write(QueuePath + "已成功创建!"); 
                }
                else
                {
                    LoggerFile.Write(QueuePath + "已经存在!"); 
                }
            }
            catch (MessageQueueException e)
            {
                LoggerFile.Write(e.Message); 
            }
        }
        /// 
        /// 删除队列
        /// 
        /// 
        public static void Deletequeue(string queueName)
        {
            try
            {
                QueuePath = string.Format(QueuePath, queueName);
                //判断队列是否存在
                if (MessageQueue.Exists(QueuePath))
                {
                    MessageQueue.Delete(QueuePath);
                    LoggerFile.Write(QueuePath + "已删除!");
                }
                else
                {
                    LoggerFile.Write(QueuePath + "不存在!");
                }
            }
            catch (MessageQueueException e)
            {
                LoggerFile.Write(e.Message);
            }
        }
        /// 
        /// 发送消息
        /// 
        /// 用户数据类型
        /// 用户数据
        /// 队列名称
        /// 
        /// 
        public static bool SendMessage(T target, string queueName, MessageQueueTransaction tran = null)
        {
            try
            {
                QueuePath = string.Format(QueuePath, queueName);
                //连接到本地的队列
                MessageQueue myQueue = new MessageQueue(QueuePath);
                System.Messaging.Message myMessage = new System.Messaging.Message();
                myMessage.Body = target;
                myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
                //发送消息到队列中
                if (tran == null)
                {
                    myQueue.Send(myMessage);
                }
                else
                {
                    myQueue.Send(myMessage, tran);
                }
                LoggerFile.Write("消息已成功发送到" + queueName + "队列!");
                return true;
            }
            catch (ArgumentException e)
            {
                LoggerFile.Write(e.Message);
                return false;
            }
        }
        /// 
        /// 接收消息
        /// 
        /// 用户的数据类型
        /// 消息路径
        /// 用户填充在消息当中的数据
        public static T ReceiveMessage(string queueName, MessageQueueTransaction tran = null)
        {
            QueuePath = string.Format(QueuePath, queueName);
            //连接到本地队列
            MessageQueue myQueue = new MessageQueue(QueuePath);
            myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
            try
            {
                //从队列中接收消息
                System.Messaging.Message myMessage = tran == null ? myQueue.Receive() : myQueue.Receive(tran);
                return (T)myMessage.Body; //获取消息的内容
            }
            catch (MessageQueueException e)
            {
                LoggerFile.Write(e.Message);
            }
            catch (InvalidCastException e)
            {
                LoggerFile.Write(e.Message);
            }
            return default(T);
        }
        /// 
        /// 采用Peek方法接收消息
        /// 
        /// 用户数据类型
        /// 队列路径
        /// 用户数据
        public static T ReceiveMessageByPeek(string queueName)
        {
            QueuePath = string.Format(QueuePath, queueName);
            //连接到本地队列
            MessageQueue myQueue = new MessageQueue(QueuePath);
            myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
            try
            {
                //从队列中接收消息
                System.Messaging.Message myMessage = myQueue.Peek();
                return (T)myMessage.Body; //获取消息的内容
            }
            catch (MessageQueueException e)
            {
                LoggerFile.Write(e.Message);
            }
            catch (InvalidCastException e)
            {
                LoggerFile.Write(e.Message);
            }
            return default(T);
        }
        /// 
        /// 获取队列中的所有消息
        /// 
        /// 用户数据类型
        /// 队列路径
        /// 用户数据集合
        public static List GetAllMessage(string queueName)
        {
            QueuePath = string.Format(QueuePath, queueName);
            MessageQueue myQueue = new MessageQueue(QueuePath);
            myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
            try
            {
                Message[] msgArr = myQueue.GetAllMessages();
                List list = new List();
                msgArr.ToList().ForEach((o) =>
                {
                    list.Add((T)o.Body);
                });
                return list;
            }
            catch (Exception e)
            {
                LoggerFile.Write(e.Message);
            }
            return null;
        }
    }

你可能感兴趣的:(C#)