MSMQ在做分布式处理时可以提高应用程序的稳定和并发性能,以下是做项目上用到的一个简单的MSMQ CLASS
通常用户在进行数据同步时可以利用msmq来做一个同步中间件来解决远程数据同步的问题,在internet上数据不应该放置在外网这样不安全,所以对于某些用户数据的同步完全可以用MSMQ3。0的Http访问远程主机的MSMQ,然后再通过后台进程或用 MSMQ3。0的触发来解决。
/********************************************************************
created: 2008/04/24
created: 24:4:2008 14:35
filename: D:\Projects\MSMQService\MSMQHelper\MSMQAdmin.cs
file path: D:\Projects\MSMQService\MSMQHelper
file base: MSMQAdmin
file ext: cs
author: (Leung)
purpose:
*********************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
namespace TechSailor.MSMQHelper
{
public class MSMQAdmin
{
/// <summary>
///
/// </summary>
public enum MQType
{
/// <summary>
///
/// </summary>
publicMQ = 1,
/// <summary>
///
/// </summary>
privateMQ = 2
}
/// <summary>
/// Creates the MQ.
/// </summary>
/// <param name="mqType">Type of the mq.</param>
/// <param name="MQName">Name of the MQ.</param>
/// <returns></returns>
public bool CreateMQ(MQType mqType, string MQName)
{
if (MQName.Length > 0)
{
if (mqType == MQType.privateMQ)
{
if (!MessageQueue.Exists(@".\Private$\" + MQName))
{
System.Messaging.MessageQueue.Create(@".\Private$\" + MQName);
MessageQueue mqTemp = new MessageQueue(@".\Private$\" + MQName);
mqTemp.SetPermissions("Everyone", System.Messaging.MessageQueueAccessRights.FullControl);
}
return true;
}
else if (mqType == MQType.publicMQ)
{
if (!MessageQueue.Exists(@"192.168.1.10\" + MQName))
{
System.Messaging.MessageQueue.Create(@"192.168.1.10\" + MQName);
}
return true;
}
}
return false;
}
/// <summary>
/// Sends the message.
/// </summary>
/// <returns></returns>
public bool SendMessage(MQType mqType, string MQName, string MQLabel, string MSMQServerBody)
{
if (MQName.Length > 0 && MSMQServerBody.Length > 0)
{
MessageQueue mq = null;
try
{
if (mqType == MQType.privateMQ)
{
if (!MessageQueue.Exists(@".\Private$\" + MQName))
{
CreateMQ(MQType.privateMQ, "MQName");
}
mq = new MessageQueue(@".\Private$\" + MQName);
}
if (mq != null)
{
System.Messaging.Message msg = new System.Messaging.Message();
msg.Body = MSMQServerBody;
msg.Label = MQLabel;
msg.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
mq.Send(msg);
return true;
}
}
catch (System.Exception e)
{
{
throw new Exception(e.ToString());
}
}
}
return false;
}
/// <summary>
/// Removes the MQ.
/// </summary>
/// <param name="mqType">Type of the mq.</param>
/// <param name="MQName">Name of the MQ.</param>
/// <returns></returns>
public bool RemoveMQ(MQType mqType, string MQName)
{
if(mqType== MQType.privateMQ)
{
//System.Messaging.MessageQueue.de
}
return false;
}
/// <summary>
/// Gets the message.
/// </summary>
/// <param name="mqType">Type of the mq.</param>
/// <param name="QueuePath">The queue path.</param>
/// <returns></returns>
public System.Collections.ArrayList ReceiveMQMessage(MQType mqType, string MQName)
{
System.Messaging.MessageQueue mq = null;
if (mqType == MQType.privateMQ)
{
mq = new MessageQueue(@".\Private$\" + MQName,false);
}
//mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(MyBase.SmsQueue) });收取对象方式
if(mq!= null)
{
mq.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
System.Messaging.Message[] arrM = mq.GetAllMessages();
mq.Close();
System.Collections.ArrayList al = new System.Collections.ArrayList();
foreach (System.Messaging.Message m in arrM)
{
al.Add(m.Body.ToString());
}
return al;
}
return null;
}
}
}
通常用户在进行数据同步时可以利用msmq来做一个同步中间件来解决远程数据同步的问题,在internet上数据不应该放置在外网这样不安全,所以对于某些用户数据的同步完全可以用MSMQ3。0的Http访问远程主机的MSMQ,然后再通过后台进程或用 MSMQ3。0的触发来解决。
/********************************************************************
created: 2008/04/24
created: 24:4:2008 14:35
filename: D:\Projects\MSMQService\MSMQHelper\MSMQAdmin.cs
file path: D:\Projects\MSMQService\MSMQHelper
file base: MSMQAdmin
file ext: cs
author: (Leung)
purpose:
*********************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
namespace TechSailor.MSMQHelper
{
public class MSMQAdmin
{
/// <summary>
///
/// </summary>
public enum MQType
{
/// <summary>
///
/// </summary>
publicMQ = 1,
/// <summary>
///
/// </summary>
privateMQ = 2
}
/// <summary>
/// Creates the MQ.
/// </summary>
/// <param name="mqType">Type of the mq.</param>
/// <param name="MQName">Name of the MQ.</param>
/// <returns></returns>
public bool CreateMQ(MQType mqType, string MQName)
{
if (MQName.Length > 0)
{
if (mqType == MQType.privateMQ)
{
if (!MessageQueue.Exists(@".\Private$\" + MQName))
{
System.Messaging.MessageQueue.Create(@".\Private$\" + MQName);
MessageQueue mqTemp = new MessageQueue(@".\Private$\" + MQName);
mqTemp.SetPermissions("Everyone", System.Messaging.MessageQueueAccessRights.FullControl);
}
return true;
}
else if (mqType == MQType.publicMQ)
{
if (!MessageQueue.Exists(@"192.168.1.10\" + MQName))
{
System.Messaging.MessageQueue.Create(@"192.168.1.10\" + MQName);
}
return true;
}
}
return false;
}
/// <summary>
/// Sends the message.
/// </summary>
/// <returns></returns>
public bool SendMessage(MQType mqType, string MQName, string MQLabel, string MSMQServerBody)
{
if (MQName.Length > 0 && MSMQServerBody.Length > 0)
{
MessageQueue mq = null;
try
{
if (mqType == MQType.privateMQ)
{
if (!MessageQueue.Exists(@".\Private$\" + MQName))
{
CreateMQ(MQType.privateMQ, "MQName");
}
mq = new MessageQueue(@".\Private$\" + MQName);
}
if (mq != null)
{
System.Messaging.Message msg = new System.Messaging.Message();
msg.Body = MSMQServerBody;
msg.Label = MQLabel;
msg.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
mq.Send(msg);
return true;
}
}
catch (System.Exception e)
{
{
throw new Exception(e.ToString());
}
}
}
return false;
}
/// <summary>
/// Removes the MQ.
/// </summary>
/// <param name="mqType">Type of the mq.</param>
/// <param name="MQName">Name of the MQ.</param>
/// <returns></returns>
public bool RemoveMQ(MQType mqType, string MQName)
{
if(mqType== MQType.privateMQ)
{
//System.Messaging.MessageQueue.de
}
return false;
}
/// <summary>
/// Gets the message.
/// </summary>
/// <param name="mqType">Type of the mq.</param>
/// <param name="QueuePath">The queue path.</param>
/// <returns></returns>
public System.Collections.ArrayList ReceiveMQMessage(MQType mqType, string MQName)
{
System.Messaging.MessageQueue mq = null;
if (mqType == MQType.privateMQ)
{
mq = new MessageQueue(@".\Private$\" + MQName,false);
}
//mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(MyBase.SmsQueue) });收取对象方式
if(mq!= null)
{
mq.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
System.Messaging.Message[] arrM = mq.GetAllMessages();
mq.Close();
System.Collections.ArrayList al = new System.Collections.ArrayList();
foreach (System.Messaging.Message m in arrM)
{
al.Add(m.Body.ToString());
}
return al;
}
return null;
}
}
}