.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;
- namespace testweb
- {
- public partial class MSMQtest : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
-
-
-
-
-
- MsgModel m = receiveComplexMsg<MsgModel>();
- Response.Write(m.ToString());
-
- }
- private void sendSimpleMsg()
- {
-
- MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
-
- 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");
-
- 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(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 MQ = new MessageQueue(@".\private$\MsgQueue");
-
- 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");
-
- 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");
-
- 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);
- }
-
-
-
-
-
-
- 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
- {
-
- 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);
- }
- }
- }