美文网首页
消息队列MSMQ

消息队列MSMQ

作者: 高调的小丑 | 来源:发表于2018-07-24 15:05 被阅读34次

    首先是将windows的MQ功能开启。


    新建两个windows应用程序,用来测试消息传输


    发送消息的代码

            MessageQueue mq;
            private void Form1_Load(object sender, EventArgs e)
            {
                //新建消息循环队列或连接到已有的消息队列
                string path = ".\\private$\\killf";
                if (MessageQueue.Exists(path))
                {
                    mq = new MessageQueue(path);
                }
                else
                {
                    mq = MessageQueue.Create(path);
                }
    
                mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                mq.Send(textBox1.Text);
            }
    

    用于接收的代码

            MessageQueue mq;
            private void Form1_Load(object sender, EventArgs e)
            {
                //新建消息循环队列或连接到已有的消息队列
                string path = ".\\private$\\killf";
                if (MessageQueue.Exists(path))
                {
                    mq = new MessageQueue(path);
                }
                else
                {
                    mq = MessageQueue.Create(path);
                }
    
                mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                mq.ReceiveCompleted += mq_ReceiveCompleted;
                mq.BeginReceive();
            }
    
            void mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
            {
                //throw new NotImplementedException();
                MessageQueue mq = (MessageQueue)sender;
                System.Messaging.Message m = mq.EndReceive(e.AsyncResult);
    
                //处理消息
                string str = m.Body.ToString();
                this.textBox1.Invoke(new Action<string>(ShowMsg), str);
    
                //继续下一条消息
                mq.BeginReceive();
            }
            private void ShowMsg(string msg)
            {
                this.textBox1.Text = this.textBox1.Text + msg + Environment.NewLine;
                return;
            }
    

    测试完成!


    OK.png

    1、命名空间 using System.Messaging;
    2、默认存储路径 C:\WINDOWS\system32\msmq\storage
    3、创建消息队列:

    MessageQueue mq = MessageQueue.Create(@".\Private$\LeeMSMQ");
    

    4、删除队列:

    MessageQueue.Delete(@".\Private$\LeeMSMQ");
    

    5、发送消息:

    MessageQueue mq = new MessageQueue(@".\Private$\LeeMSMQ");
                mq.Send("sayhello1,hello msmq!", "sayhello1");
                mq.Send("sayhello2,hello msmq!", "sayhello2");
    

    6、接受并删除消息:

    MessageQueue mq = new MessageQueue(@".\Private$\LeeMSMQ")
    Message msg = mq.Receive();//引用的队列中可用的第一条消息
    

    7、接受但不删除消息:

    Message msg = mq.Peek();
    

    8、删除所有消息:

    Message msg = mq.Purge();
    

    9、返回本机所有私有队列的消息

          //返回本机所有私有队列的消息
                 foreach (MessageQueue mq in MessageQueue.GetPrivateQueuesByMachine("liyanping"))
                {
                    mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
                    Message[] msg = mq.GetAllMessages();
                    foreach (Message m in msg)
                    {
                        Console.WriteLine("label:{0},body:{1}", m.Label, m.Body);
                    }
                }
    

    10、返回指定队列的消息

                if (MessageQueue.Exists(@".\Private$\LeeMSMQ"))//判断私有消息是否存在
                {
                    using (MessageQueue mq = new MessageQueue(@".\Private$\LeeMSMQ"))
                    {
                        mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });//设置消息队列格式化器
                        Message msg = mq.Receive();//接收消息
                        Console.WriteLine("label:{0},body: {1}", msg.Label, msg.Body);//输出消息
                        MessageQueue.Delete(@".\Private$\LeeMSMQ");
                    }
                } 
    

    相关文章

      网友评论

          本文标题:消息队列MSMQ

          本文链接:https://www.haomeiwen.com/subject/iydeixtx.html