這篇文章主要介紹了.Net消息隊列的使用方法,需要的朋友可以參考下
.Net使用消息隊列,借助windows組件來存儲要完成的一系列任務,不用程序使用同一個隊列,方便不同程序之間的數據共享和協作……
以本人經驗,這個在某個方面類似於session(當然還有很多方面不同),相同之處:session可以把信息存儲在aspnet_state服務中,網站重新編譯或者重新啟動網站,session不會丟失(session超時是正常情況,這種情況除外)。
win7中安裝消息隊列組件,其他操作系統請百度搜索相關資料。

如果服務沒有自動啟動,需要啟動服務:

先創建隊列,再使用隊列,隊列中的消息,發送一個多一個,接收一個少一個,先進先出。
碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Messaging;
//添加物理文件 System.Messaging 的引用
namespace testweb
{
public partial class MSMQtest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//CreateNewQueue("MsgQueue");//創建一個消息隊列
//sendSimpleMsg();//每一個隊列最好只發送和接收同一種格式的信息,不然不好轉換格式。
//receiveSimpleMsg();//
//receiveSimpleMsg();
//sendComplexMsg();
//receiveComplexMsg();
MsgModel m = receiveComplexMsg<MsgModel>();
Response.Write(m.ToString());
}
private void sendSimpleMsg()
{
//實例化MessageQueue,並指向現有的一個名稱為VideoQueue隊列
MessageQueue MQ = new MessageQueue(@".private$MsgQueue");
//MQ.Send("消息測試", "測試消息");
System.Messaging.Message message = new System.Messaging.Message();
message.Label = "消息lable";
message.Body = "消息body";
MQ.Send(message);
Response.Write("成功發送消息," + DateTime.Now + "<br/>");
}
private void receiveSimpleMsg()
{
MessageQueue MQ = new MessageQueue(@".private$MsgQueue");
//調用MessageQueue的Receive方法接收消息
if (MQ.GetAllMessages().Length > 0)
{
System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));
if (message != null)
{
//message.Formatter = new System.Messaging.XmlMessageFormatter(new string[] { "Message.Bussiness.VideoPath,Message" });//消息類型轉換
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
Response.Write(string.Format("接收消息成功,lable:{0},body:{1},{2}<br/>", message.Label, message.Body.ToString(), DateTime.Now));
}
}
else
{
Response.Write("沒有消息了!<br/>");
}
}
private void sendComplexMsg()
{
//實例化MessageQueue,並指向現有的一個名稱為VideoQueue隊列
MessageQueue MQ = new MessageQueue(@".private$MsgQueue");
//MQ.Send("消息測試", "測試消息");
System.Messaging.Message message = new System.Messaging.Message();
message.Label = "復雜消息lable";
message.Body = new MsgModel("1", "消息1");
MQ.Send(message);
Response.Write("成功發送消息,"+DateTime.Now+"<br/>");
}
private void receiveComplexMsg()
{
MessageQueue MQ = new MessageQueue(@".private$MsgQueue");
//調用MessageQueue的Receive方法接收消息
if (MQ.GetAllMessages().Length > 0)
{
System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));
if (message != null)
{
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(MsgModel) });//消息類型轉換
MsgModel msg = (MsgModel)message.Body;
Response.Write(string.Format("接收消息成功,lable:{0},body:{1},{2}<br/>", message.Label, msg, DateTime.Now));
}
}
else
{
Response.Write("沒有消息了!<br/>");
}
}
private T receiveComplexMsg<T>()
{
MessageQueue MQ = new MessageQueue(@".private$MsgQueue");
//調用MessageQueue的Receive方法接收消息
if (MQ.GetAllMessages().Length > 0)
{
System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));
if (message != null)
{
&nb