.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)
{
message.formatter = new system.messaging.xmlmessageformatter(new type[] { typeof(t) });//消息类型转换
t msg = (t)message.body;
return msg;
}
}
return default(t);
}
/// <summary>
/// 创建消息队列
/// </summary>
/// <param name="name">消息队列名称</param>
/// <returns></returns>
public void createnewqueue(string name)
{
if (!system.messaging.messagequeue.exists(".\\private$\\" + name))//检查是否已经存在同名的消息队列
{
system.messaging.messagequeue mq = system.messaging.messagequeue.create(".\\private$\\" + name);
mq.label = "private$\\"+name;
response.write("创建成功!<br/>");
}
else
{
//system.messaging.messagequeue.delete(".\\private$\\" + name);//删除一个消息队列
response.write("已经存在<br/>");
}
}
}
[serializable]
public class msgmodel
{
public string id { get; set; }
public string name { get; set; }
public msgmodel() { }
public msgmodel(string _id, string _name)
{
id = _id;
name = _name;
}
public override string tostring()
{
if (string.isnullorempty(id) || string.isnullorempty(name)) return "";
return string.format("id--{0},name--{1}",id,name);
}
}
}