MSMQ 处理机制

1.基本处理方式

1.创建消息队列

2.发送消息

3.接收消息

public class MSMQ
    {
        /// <summary>
        /// 通过Create方法创建使用指定路径的新消息队列
        /// </summary>
        /// <param name="queuePath"></param>
        public static void Createqueue(string queuePath)
        {
            try
            {
                if (!MessageQueue.Exists(string.Format(".\\{0}", queuePath)))
                {
                    MessageQueue.Create(string.Format(".\\private$\\{0}", queuePath));

                }
                else
                {

                }
            }
            catch (MessageQueueException e)
            {

            }
        }

        /// <summary>
        /// 连接消息队列并发送消息到队列
        /// </summary>
        public static bool SendMessage<T>(string queuePath, T be)
        {
            bool flag = false;
            try
            {
                //连接到本地的队列
                MessageQueue myQueue = new MessageQueue(string.Format(".\\private$\\{0}", queuePath));

                Message myMessage = new Message();
                myMessage.Body = be;
                myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
                //发送消息到队列中

                myQueue.Send(myMessage);
                flag = true;
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
            return flag;
        }

        /// <summary>
        /// 连接消息队列并发送消息到队列
        /// </summary>
        public static bool SendMessagelist<T>(string queuePath, List<T> be)
        {
            bool flag = false;
            try
            {
                //连接到本地的队列
                MessageQueue myQueue = new MessageQueue(string.Format(".\\private$\\{0}", queuePath));

                Message myMessage = new Message();
                myMessage.Body = be;
                myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
                //发送消息到队列中

                myQueue.Send(myMessage);
                flag = true;
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
            return flag;
        }

        /// <summary>
        /// 连接消息队列并从队列中接收消息

        /// </summary>
        public static T ReceiveMessage<T>(string queuePath)
        {
            //连接到本地队列

            MessageQueue myQueue = new MessageQueue(string.Format(".\\private$\\{0}", queuePath));
            myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });

            //从队列中接收消息
            System.Messaging.Message myMessage = myQueue.Receive();
            T be = (T)myMessage.Body; //获取消息的内容

            return be;

        }

        /// 连接消息队列并从队列中接收消息

        /// </summary>
        public static List<T> ReceiveMessageList<T>(string queuePath)
        {
            //连接到本地队列

            MessageQueue myQueue = new MessageQueue(string.Format(".\\private$\\{0}", queuePath));
            myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(List<T>) });

            //从队列中接收消息
            System.Messaging.Message myMessage = myQueue.Receive();
            List<T> be = (List<T>)myMessage.Body; //获取消息的内容

            return be;

        }

        /// <summary>
        /// 清空指定队列的消息

        /// </summary>
        public static void ClearMessage(string queuePath)
        {
            MessageQueue myQueue = new MessageQueue(string.Format(".\\private$\\{0}", queuePath));
            myQueue.Purge();
        }

        /// <summary>
        /// 删除指定队列的消息

        /// </summary>
        /// <param name="queuePath"></param>
        public static void DeleteMessage(string queuePath)
        {
            MessageQueue myQueue = new MessageQueue(string.Format(".\\private$\\{0}", queuePath));
            myQueue.Purge();
        }
        /// <summary>
        /// 连接队列并获取队列的全部消息
        /// </summary>
        public static void GetAllMessage(string queuePath)
        {
            //连接到本地队列

            MessageQueue myQueue = new MessageQueue(string.Format(".\\private$\\{0}", queuePath));
            Message[] message = myQueue.GetAllMessages();
            XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            for (int i = 0; i < message.Length; i++)
            {
                message[i].Formatter = formatter;
                Console.WriteLine(message[i].Body.ToString());
            }
        }

        /// <summary>
        /// 获取指定消息队列数量
        /// </summary>
        /// <param name="queuePath"></param>
        /// <returns>消息队列数量</returns>
        public static int GetMessageCount(string queuePath)
        {
            //连接到本地队列

            MessageQueue myQueue = new MessageQueue(string.Format(".\\private$\\{0}", queuePath));

            Message[] message = myQueue.GetAllMessages();
            return message.Length;
        }
    }

4.使用消息队列必然要用到序列化

public class WmsSerializable
   {

       /// <summary>
       /// 序列化字符

       /// </summary>
       /// <param name="o">类的对象</param>
       /// <returns></returns>
       public static string Serialize<T>(T o)
       {
           Type type =  typeof(T) ;
           // 创建一个xml序列化对象

           System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(type);

           // 创建一个内存流对象
           MemoryStream memoryStream = new MemoryStream();
           // 将传入的systemMeg转换成一个内存流,保存在xsl序列化对象中
           xml.Serialize(memoryStream, o);
           return Convert.ToBase64String(memoryStream.ToArray());
       }

       /// <summary>
       /// 反序列化类

       /// </summary>
       /// <param name="messageString">要序列化的字符</param>
       /// <param name="objectType">类的对象</param>
       /// <returns></returns>
       public static T DeSerialize<T>(string messageString)
       {
           Type type = typeof(T);
           // 创建一个xml序列化对象

           System.Xml.Serialization.XmlSerializer xsl = new System.Xml.Serialization.XmlSerializer(type);
           // 序列化一个内存流
           MemoryStream ms = new MemoryStream(Convert.FromBase64String(messageString));
           return (T)xsl.Deserialize(ms);
       }

       /// <summary>
       /// 序列化字符

       /// </summary>
       /// <param name="o">类的对象</param>
       /// <returns></returns>
       public static string SerializeList<T>(T o)
       {
           Type type = typeof(T);
           // 创建一个xml序列化对象

           System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(type);

           // 创建一个内存流对象
           MemoryStream memoryStream = new MemoryStream();
           // 将传入的systemMeg转换成一个内存流,保存在xsl序列化对象中
           xml.Serialize(memoryStream, o);
           return Convert.ToBase64String(memoryStream.ToArray());
       }

       /// <summary>
       /// 反序列化类

       /// </summary>
       /// <param name="messageString">要序列化的字符</param>
       /// <param name="objectType">类的对象</param>
       /// <returns></returns>
       public static List<T> DeSerializeList<T>(string messageString)
       {
           Type type = typeof(T);
           // 创建一个xml序列化对象

           System.Xml.Serialization.XmlSerializer xsl = new System.Xml.Serialization.XmlSerializer(type);
           // 序列化一个内存流
           MemoryStream ms = new MemoryStream(Convert.FromBase64String(messageString));
           return (List<T>)xsl.Deserialize(ms);
       }
   }

 

5.这样创建的消息队列并不能保存到硬盘中,如何才能进一步处理消息队列呢?

关于消息的加密、路由等等特性,需要有配置Active Directory的消息队列服务器。
为了避免存放消息队列的计算机重新启动而丢失消息,可以通过设置消息对象的Recoverable属性为true,在消息传递过程中将消息保存到磁盘上来保证消息的传递,默认为false

你可能感兴趣的:(MQ)