刚学习微软的消息队列,通过查找资料做了一个简单的实例,大概步骤和代码如下:
1、首页在操作系统上安装消息队列
控制面板—>添加安装程序:在IIS(window2003应用程序服务)下面,钩选"消息队列"
图一:
/Files/lmjob/clip_image002_thumb.jpg
2、新建ASP.NET Web应用程序,并添加引用"System.Messaging"类
3、创建消息、发送消息、接收消息代码:
///
<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
/// 创建消息队列
/// </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
经测试发现:1、如果接收到消息后,消息管理器在消息队列中立即删除该消息。2、消息队列可以传输多种类型数据(例:字符串、整形、对象等)
附:项目代码