1.MSMQ安装
控制面板-程序和功能-打开或关闭Windows功能-Microsoft Message Queue(MSMQ)服务器,选中所有,点击确定。
2.消息队列的应用场景(转载自http://www.cnblogs.com/stopfalling/p/5375492.html)
①异步处理
②应用解耦
③流量削锋
④日志处理
⑤消息通讯
3.MSMQ消息分为事务性和非事务性
非事务性的消息保存在内存中,机器重启后丢失消息队列中的消息。
事务性的消息经过了持久化,保存在硬盘中,机器重启后队列中的消息不会丢失。
4.测试代码
消息发送方
namespace MessageQueueSender
{
class Program
{
static void Main(string[] args)
{
MessageQueue MSMQ = CreateMessageQueue(@".\private$\tpmsmq");
MSMQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
Console.WriteLine("是否继续发送消息:Y/N?");
string cmd = Console.ReadLine();
while (cmd.Equals("Y"))
{
Sender(MSMQ);
Console.WriteLine("是否继续发送消息:Y/N?");
cmd = Console.ReadLine();
}
Console.WriteLine("按任意键以停止...");
Console.ReadKey();
}
private static void Sender(MessageQueue MSMQ)
{
try
{
string random = GenerateRandom();
string obj = string.Format("{0} 发送方:{1}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), random);
MSMQ.Send(obj, MessageQueueTransactionType.Single);
Console.WriteLine(obj);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("{0} 发送方:{1}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message));
}
}
public static MessageQueue CreateMessageQueue(string path)
{
MessageQueue mq = null;
if (MessageQueue.Exists(path))
{
mq = new MessageQueue(path);
}
else
{
mq = MessageQueue.Create(path, true);
}
return mq;
}
public static string GenerateRandom()
{
int seed = GetRandomSeed();
return new Random(seed)
.Next(Int32.MaxValue).ToString();
}
/// <summary>
/// 创建加密随机数生成器 生成强随机种子
/// </summary>
/// <returns></returns>
private static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng
= new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
}
}
消息接收方
namespace MessageQueueReceiver
{
class Program
{
static void Main(string[] args)
{
MessageQueue MSMQ = CreateMessageQueue(@".\private$\tpmsmq");
MSMQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
Receiver(MSMQ);
}
private static void Receiver(MessageQueue MSMQ)
{
while (true)
{
try
{
Message m = MSMQ.Receive(MessageQueueTransactionType.Single);
Console.WriteLine(string.Format("{0} 接收方:[{1}]",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), m.Body.ToString()));
}
catch (Exception ex)
{
Console.WriteLine(string.Format("{0} 接收方:{1}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message));
}
}
}
public static MessageQueue CreateMessageQueue(string path)
{
MessageQueue mq = null;
if (MessageQueue.Exists(path))
{
mq = new MessageQueue(path);
}
else
{
mq = MessageQueue.Create(path, true);
}
return mq;
}
}
}
MessageQueue.Receive()方法是同步执行的,如果队列中没有消息,则会造成阻塞,程序会停止在这块,等待消息的产生。