You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/MvcShipping/Comm/MSMQTool.cs

122 lines
4.3 KiB
C#

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 DSWeb.Areas.MvcShipping.Comm
{
/// <summary>
/// 消息队列工具类
/// </summary>
public class MSMQTool
{
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);
}
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) });
}
}
}
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);
}
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;
}
}
}
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;
}
}
}
[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);
}
}
}