1、首页在操作系统上安装消息队列
控制面板—>添加安装程序:在IIS(window2003应用程序服务)下面,钩选"消息队列"
图一:
2、新建ASP.NET Web应用程序,并添加引用"System.Messaging"类
3、创建消息、发送消息、接收消息代码:
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MSMQTest._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> 发送端:<asp:TextBox ID="txt_Message" runat="server" TextMode="MultiLine"></asp:TextBox> <asp:Button ID="btn_Send" runat="server" Text="发送" onclick="btn_Send_Click" /> <br /> 接收端:<asp:TextBox ID="txt_Accept" runat="server" TextMode="MultiLine"></asp:TextBox> <asp:Button ID="btn_Accept" runat="server" Text="接收" onclick="btn_Accept_Click" /> <br /> 接收端1:<asp:TextBox ID="txt_Accept0" runat="server" TextMode="MultiLine"></asp:TextBox> <asp:Button ID="btn_Accept0" runat="server" Text="再次接收" onclick="btn_Accept0_Click" /> </div> </form> </body> </html>
Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Messaging; namespace MSMQTest { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 创建消息队列 /// </summary> /// <param name="name">消息队列名称</param> /// <returns></returns> public static string CreateNewQueue(string name) { if (!System.Messaging.MessageQueue.Exists(".\\private$\\" + name))//检查是否已经存在同名的消息队列 { System.Messaging.MessageQueue mq = System.Messaging.MessageQueue.Create(".\\private$\\" + name); mq.Label = "test"; return "ok"; } else { return "已经存在"; } } /// <summary> /// 同步发送消息 /// </summary> /// <param name="input">发送的内容</param> /// <param name="queueName">发送哪个消息队列中</param> public static void SendMsg(string input, string queueName) { // System.Messaging.MessageQueue.Create(".\\private$\\test2"); System.Messaging.MessageQueue mq = new MessageQueue(".\\private$\\" + queueName); System.Messaging.Message message = new System.Messaging.Message(); message.Body = input; message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) }); mq.Send(message); } /// <summary> /// 同步发送消息 /// </summary> /// <param name="input">发送的内容</param> /// <param name="queueName">发送哪个消息队列中</param> public static void SendMsg(string[] strList, string queueName) { System.Messaging.MessageQueue mq = new MessageQueue(".\\private$\\" + queueName); System.Messaging.Message message = new System.Messaging.Message(); message.Body = strList; message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) }); mq.Send(message); } /// <summary> /// 同步接收消息(消息为字符串) /// </summary> /// <param name="queueName">从哪个消息队列接受消息</param> /// <returns></returns> public static string ReceiveMsg(string queueName) { System.Messaging.MessageQueue mq = new MessageQueue(".\\private$\\" + queueName); if (mq.GetAllMessages().Length == 0)//判断是否还有消息,如果没有消息的话Receive方法会一直独占进程直到有消息为止,所以必须判断 { return "没有消息了"; } else { Message m = mq.Receive(); m.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) }); return m.Body.ToString(); } } /// <summary> /// 同步接收消息(消息为数组对象) /// </summary> /// <param name="queueName">从哪个消息队列接受消息</param> /// <returns></returns> public static string[] ReceiveMsgList(string queueName) { System.Messaging.MessageQueue mq = new MessageQueue(".\\private$\\" + queueName); if (mq.GetAllMessages().Length == 0)//判断是否还有消息,如果没有消息的话Receive方法会一直独占进程直到有消息为止,所以必须判断 { return null; } else { Message m = mq.Receive(); m.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string[]) }); return (string[])m.Body; } } #region 发送消息事件 protected void btn_Send_Click(object sender, EventArgs e) { SendMsg(this.txt_Message.Text, "LmjobMsg"); //消息为数组对象 //string[] list = new string[] { "王一", "王二", "王三", "王四" }; //SendMsg(list, "LmjobMsg"); } #endregion #region 接收消息事件 protected void btn_Accept_Click(object sender, EventArgs e) { string messageBody = ReceiveMsg("LmjobMsg"); this.txt_Accept.Text = messageBody; ////如果消息为数组对象 //string[] messageBody = ReceiveMsgList("LmjobMsg"); //string messageValue = ""; //if (null != messageBody) //{ // foreach (string a in messageBody) // { // messageValue += a+","; // } //} //this.txt_Accept.Text = messageValue; } #endregion protected void btn_Accept0_Click(object sender, EventArgs e) { string messageBody = ReceiveMsg("LmjobMsg"); this.txt_Accept0.Text = messageBody; } } }
url:http://blog.163.com/dreamman_yx/blog/static/26526894201091110378519/