zoukankan      html  css  js  c++  java
  • MessageQueue学习总结

    具体的理论这里就不说了。主要说一下做的例子。本例子主要是发送不同的消息。然后根所ID得到指定的消息内容。

    方法:

      1、在专用队列增加一个专用队列"Demo"。所有选项为默认即可。

      2、发送消息。输入消息的ID,输入消息的内容发送消息。

      3、输入被接收消息的ID,根据此ID找到消息后查看其内容。

    消息ID设置方法:

      消息中没有ID这个选项。所以不知如何把消息发送给指定的人。后来找到了Label属性, 我将要发送的给某某人的ID设在此属性中来解决问题。  如果谁有更好的方法也请回贴告知我。

    代码:

       第二步中发送按钮代码为:

             /// <summary>
            
    /// 定义消息路径
            
    /// </summary>
            public const string sMessageConnectionString = @".\Private$\Demo";
           

            
    /// <summary>
            
    /// 发送
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            private void btnSend_Click(object sender, EventArgs e)
            {
                
    if (txbSend.Text.Trim() == "")
                {
                    MessageBox.Show(
    "请输入要发送的message!");
                    txbSend.Focus();
                    
    return;
                }

                
    //完整队列格式为:  计算机名\private$\队列名称 (专用队列)
                MessageQueue mqQue = new MessageQueue(txtQueue.Text.Trim());
                mqQue.MessageReadPropertyFilter.SetAll();

                System.Messaging.Message msg 
    = new System.Messaging.Message();
                
    //消息主体
                msg.Body = txbSend.Text.Trim() + " " + DateTime.Now.ToString();
                
    //用描述设置ID
                msg.Label = txbMessageID.Text;
                
    //将消息加入到发送队列
                msg.ResponseQueue = mqQue;
                msg.AttachSenderId 
    = true;

                msg.Formatter 
    = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });

                
    try
                {   
                    
    //发送
                    mqQue.Send(msg);
                }
                
    catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }

            }

      第三步中接收消息的代码:

            /// <summary>
            
    /// 接收
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            private void btnReceive_Click(object sender, EventArgs e)
            {
               
                MessageQueue mqQue 
    = null;
                
    if (MessageQueue.Exists(sMessageConnectionString))
                {
                    mqQue 
    = new System.Messaging.MessageQueue(sMessageConnectionString);
                }
                
    //得到所有消息
                System.Messaging.Message[] messages = mqQue.GetAllMessages();
                
    if (messages.Length > 0)
                {
                    
    foreach (System.Messaging.Message msgVal in messages)
                    {
                        
    //根据ID得到消息(这个ID放在消息的label中)
                        if (msgVal.Label==txbReceiveMsgID.Text)
                        {
                            
    //同步接收,直到得到一条消息为止,如果消息队列为空,会一直阻塞
                            System.Messaging.Message msg = mqQue.Receive();
                            msg.Formatter 
    = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
                            
    //消息的内容
                            txbReceive.Text = "[" + DateTime.Now.ToString() + "]: " + msg.Body.ToString();
                            
    break;
                        }
                        
    else
                        {
                            txbReceiveMsgID.Text 
    = "没有找到指定ID的Message";
                        }
                    }
                }
            }

      清除所有消息代码:

       /// <summary>
            
    /// 清除消息队列
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            private void btnClear_Click(object sender, EventArgs e)
            {
                MessageQueue mqQue 
    = null;
                
    if (MessageQueue.Exists(sMessageConnectionString))
                {
                    
    //清除所有消息
                    mqQue = new System.Messaging.MessageQueue(sMessageConnectionString);
                    mqQue.Purge();

                }
            }

    代码下载:

    https://files.cnblogs.com/scottckt/MessageStudy.rar


  • 相关阅读:
    Unity3D在各平台上的路径
    Unity简单的单例模式
    C#遍历枚举(Enum)
    C#常用的流类型(FileStream,SteamWriter/StreamReader,MemoryStream等)
    编写一个C程序,运行时输入a,b,c三个值,输出其中最大者
    精确一维搜索算法(直接法)
    Java一维数组求和
    java 导出EXCEL
    Java判断字符串的数字类型(小数、整数)
    网址存储
  • 原文地址:https://www.cnblogs.com/scottckt/p/1434584.html
Copyright © 2011-2022 走看看