zoukankan      html  css  js  c++  java
  • .Net 操作MSMQ

    下面这个类可以用来直接操作MSMQ,但有一个需要注意的是,如果你是用APS.NET或WINDOWS SERVICE 操作MSMQ
    一定要记的把MSMQ的队列权限设成everyone完全控制,不然会访问不了.我的程序中也增加了对这个权限的控制
    mq.SetPermissions("Everyone", MessageQueueAccessRights.FullControl); //这一句就够了
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Messaging;
    namespace SNET.Common
    {
        /// <summary>
        ///
        /// </summary>
        public class MSMQHelper
        {

            /// <summary>
            /// 通过Create方法创建使用指定路径的新消息队列
            /// </summary>
            /// <param name="queuePath"></param>
            public static void Createqueue(string queuePath)
            {
                try
                {
                    if (!MessageQueue.Exists(queuePath))
                    {
                        MessageQueue mq = MessageQueue.Create(queuePath,true);
                        if (mq != null)
                        {
                            mq.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);
                        }
                    }              
                }
                catch (MessageQueueException e)
                {
                    throw new Exception(e.ToString());
                }
            }
            /// <summary>
            /// Sends the message.
            /// </summary>
            public static void SendMessage(string queuePath,string strBody)
            {
                MessageQueue myQueue = null;
                try
                {
                    //连接到本地的队列
                    myQueue = new MessageQueue(queuePath);
                    Message myMessage = new Message();
                    myMessage.Body = strBody;
                    myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                    //发送消息到队列中
                    myQueue.Send(myMessage);
                    myQueue.Dispose();
                }
                catch (ArgumentException e)
                {
                    throw new Exception(e.ToString());
                }
                finally
                {
                    if (myQueue != null)
                        myQueue.Dispose();
                }
            }
            /// <summary>
            /// Sends the message.
            /// </summary>
            public static void SendMessage(string queuePath, string queueLable,string strBody)
            {
                MessageQueue myQueue = null;
                try
                {
                    //连接到本地的队列
                    myQueue = new MessageQueue(queuePath);
                    Message myMessage = new Message();
                    myMessage.Body = strBody;
                    if (queueLable != null)
                    {
                        myMessage.Label = queueLable;
                    }
                    myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                    //发送消息到队列中
                    myQueue.Send(myMessage);
                    myQueue.Dispose();
                }
                catch (ArgumentException e)
                {
                    throw new Exception(e.ToString());
                }
                finally
                {
                    if (myQueue != null)
                        myQueue.Dispose();
                }
            }
            /// <summary>
            /// Receives the message.
            /// </summary>
            /// <param name="QueuePath">The queue path.</param>
            /// <returns></returns>
            public static string ReceiveMessage(string QueuePath)
            {            
                //连接到本地队列
                MessageQueue myQueue = new MessageQueue(QueuePath);
                myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                try
                {
                    //从队列中接收消息
                    Message myMessage = myQueue.Receive(new TimeSpan(0,0,6));                
                    string context = (string)myMessage.Body; //获取消息的内容
                    return context;

                }
                catch (MessageQueueException e)
                {
                    throw new Exception(e.ToString());
                }
                catch (InvalidCastException e)
                {
                    throw new Exception(e.ToString());
                }
                finally
                {
                    if (myQueue != null)
                    myQueue.Dispose();
                }
                return "";
            }
            /// <summary>
            /// Clears the message.
            /// </summary>
            /// <param name="QueuePath">The queue path.</param>
            public static void ClearAllMessage(string QueuePath)
            {
                MessageQueue myQueue = null;
                try
                {
                    myQueue = new MessageQueue(QueuePath);
                    myQueue.Purge();
                }
                catch (System.Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
                finally
                {
                    if(myQueue != null)
                    myQueue.Dispose();
                }
            }
            /// <summary>
            /// Clears the message.
            /// </summary>
            /// <param name="QueuePath">The queue path.</param>
            public static void DeleteMessage(string QueuePath)
            {          
                try
                {               
                    MessageQueue.Delete(QueuePath);
                }
                catch (System.Exception ex)
                {
                    throw new Exception(ex.ToString());
                }     
            }
            /// <summary>
            /// Gets all message.
            /// </summary>
            /// <param name="QueuePath">The queue path.</param>
            /// <returns></returns>
            public static List<string> GetAllMessage(string QueuePath)
            {
                MessageQueue myQueue = null;
                try
                {
                    //连接到本地队列
                    myQueue = new MessageQueue(QueuePath);
                    Message[] message = myQueue.GetAllMessages();
                    XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                    List<string> msg = new List<string>(message.Length);
                    for (int i = 0; i < message.Length; i++)
                    {
                        message[i].Formatter = formatter;
                        msg.Add(message[i].Body.ToString());
                    }
                    return msg;
                }
                catch (System.Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
                finally
                {
                    if (myQueue != null)
                    {
                        myQueue.Dispose();
                    }
                }
            }
            /// <summary>
            /// Gets all message by enumerator.
            /// </summary>
            /// <param name="QueuePath">The queue path.</param>
            /// <returns></returns>
            public static List<string> GetAllMessageByEnumerator(string QueuePath)
            {
                List<string> msgs = null;
                MessageQueue myQueue = null;
                try
                {
                    //连接到本地队列
                    myQueue = new MessageQueue(QueuePath);              
                    XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                    MessageEnumerator enumerator = myQueue.GetMessageEnumerator();
                    msgs = new List<string>();
                    while (enumerator.MoveNext())
                    {
                        Message content = (Message)enumerator.Current;
                        content.Formatter = formatter;
                        msgs.Add(content.Body.ToString());
                        enumerator.RemoveCurrent();
                    }
                    
                }
                catch (System.Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
                finally
                {
                    if (myQueue != null)
                    {
                        myQueue.Dispose();
                    }
                }
                return msgs;
            }
        }
    }
  • 相关阅读:
    更博不能忘——webpack学习笔记
    面试中遇到过的闭包~
    github踩坑之git命令收集与整理(windows)
    应该写个博客
    重学C语言(一):熟悉汇编语言
    关于Swift的闭包(closure)以及其在可选(Optional)类型中的应用
    解决Xcode 7出现cdtool cannot compile的问题(2015年8月12日更新)
    更改LaTeX的数学模式中使用的字体
    在Mac下安装使用支持中文的LaTeX(二)
    在Mac下安装使用支持中文的LaTeX(一)
  • 原文地址:https://www.cnblogs.com/Leung/p/1534250.html
Copyright © 2011-2022 走看看