C#MSMQ 消息队列工具
这里写自定义目录标题
MSMQ消息队列工具
//定义全局变量
MessageQueue send_queue;
MessageQueue rece_queue;
public bool gb_auto_send = false; //自动发送
public bool gb_auto_rece = false; //自动接收
public bool gb_auto_bak = false; //自动备份
public int gi_cycle = 1;//线程周期 秒数
System.Threading.Timer threadTimer; //定义Timer类
public delegate void SetMQService(object value);//定义委托
public UnMsMqForm()
{
InitializeComponent();
}
///
/// 1.通过Create方法创建使用指定路径的新消息队列
///
///
public Tuple<bool, String> Createqueue_send( string queuePath)
{
try
{
if (!MessageQueue.Exists(queuePath))
{
send_queue = MessageQueue.Create(queuePath);
}
else
{
send_queue = new MessageQueue(queuePath);
return new Tuple<bool, string>(true, queuePath + " 已经存在!成功");
//Console.WriteLine(queuePath + "已经存在!");
//MessageQueue.Delete(queuePath);
//MessageQueue.Create(queuePath);
//Console.WriteLine(queuePath + "删除重建");
}
return new Tuple<bool, string>(true, "连接发送消息队列成功");
}
catch (MessageQueueException e)
{
//Console.WriteLine(e.Message);
return new Tuple<bool, string>(false, e.Message);
}
catch (ArgumentException e)
{
//Console.WriteLine(e.Message);
return new Tuple<bool, string>(false, e.Message);
}
}
/// <summary>
/// 1.通过Create方法创建使用指定路径的新消息队列
/// </summary>
/// <param name="queuePath"></param>
public Tuple<bool, String> Createqueue_rece(string queuePath)
{
try
{
if (!MessageQueue.Exists(queuePath))
{
rece_queue = MessageQueue.Create(queuePath);
}
else
{
rece_queue = new MessageQueue(queuePath);
return new Tuple<bool, string>(true, queuePath + " 已经存在!成功");
//Console.WriteLine(queuePath + "已经存在!");
//MessageQueue.Delete(queuePath);
//MessageQueue.Create(queuePath);
//Console.WriteLine(queuePath + "删除重建");
}
return new Tuple<bool, string>(true, "连接接收消息队列成功");
}
catch (MessageQueueException e)
{
//Console.WriteLine(e.Message);
return new Tuple<bool, string>(false, e.Message);
}
catch (ArgumentException e)
{
//Console.WriteLine(e.Message);
return new Tuple<bool, string>(false, e.Message);
}
}
/// <summary>
/// 2.连接消息队列并发送消息到队列
/// 远程模式:MessageQueue rmQ = new MessageQueue("FormatName:Direct=OS:machinename//private$//queue");
/// rmQ.Send("sent to regular queue - Atul");对于外网的MSMQ只能发不能收
/// </summary>
/// <param name="XMLStr">发送字符串</param>
/// <param name="EncodingType"></param>
/// <param name="LableStr"></param>
/// <param name="MsgTracType"></param>
/// <returns></returns>
public Tuple<bool, string> SendMessage(string XMLStr, string LableStr)
{
try
{
System.Text.Encoding EncodingType = null;
MessageQueueTransactionType MsgTracType = MessageQueueTransactionType.Single;
if (EncodingType == null)
{
EncodingType = System.Text.Encoding.Unicode;
}
//lock (MsgQueueHelperLocker.MQSendLocker)
//{
//连接到本地队列
//MessageQueue myQueue = new MessageQueue(string.IsNullOrEmpty(StrPath) ? QPath : StrPath);//"FormatName:Direct=TCP:172.20.20.46\\private$\\in"
System.Messaging.Message myMessage = new System.Messaging.Message();
Byte[] StrBytes = EncodingType.GetBytes(XMLStr);
myMessage.BodyStream = new MemoryStream(StrBytes);
//MSMQ消息标签设置,格式:YJCHDECDSB2B_S_ECDS_企业十位海关编码_0000000000_业务编号__年月日时分秒毫秒_GUID。
//企业十位海关编码:报文B2B_BE_CHECK节点中的TRADE_CODE。
//业务编号:报文B2B_BE_CHECK节点中的ERP_NO。
if (!string.IsNullOrEmpty(LableStr))
myMessage.Label = LableStr;// "YJCHDECDSB2B_S_ECDS_" + objVal.B2B_BE_CHECK.TRADE_CODE + "_0000000000_" + objVal.B2B_BE_CHECK.ERP_NO + "__" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Guid.NewGuid().ToString();
//if (myQueue.Transactional)
// MsgTracType = MessageQueueTransactionType.Single;
//发生消息到队列中
send_queue.Send(myMessage, MsgTracType);
//}
return new Tuple<bool, string>(true, "send ok");
}
catch (MessageQueueException e)
{
return new Tuple<bool, string>(false, e.Message);
//Console.WriteLine(e.Message);
}
catch (NullReferenceException e)
{
return new Tuple<bool, string>(false, e.Message);
//Console.WriteLine(e.Message);
}
}
程序代码下载 https://download.****.net/download/u014734340/10946221