1.首先在需要进行消息队列的服务器上安装MSMQ,我的系统是win2003+iis6,所以这个安装选项在添加删除程序->windows组件->应用程序服务器内.默认是不安装的,需要手动选择.
2.建立消息队列的存放路径.这可以在windows的计算机管理内添加,也可以在程序中添加.我是在程序中添加.
3.在.net中要引用System.Messaging;
using System;
using System.Collections.Generic;
using System.Messaging;
namespace MSMQ
{
class Program
{
private const string path = @".\private$\ljz";
private static MessageQueue queue = MessageQueue.Exists(path) ? new MessageQueue(path) : MessageQueue.Create(path, true);
//private static List<string> list = new List<string>();
public enum Level
{
Low,
Normal,
High
}
static void Main(string[] args)
{
Send("第一次!", Level.Low);
Send("第二次!", Level.Normal);
Send("第三次!", Level.High);
Console.WriteLine(Receive());
Console.WriteLine(Receive());
Console.WriteLine(Receive());
Send("异步第一次!", Level.Normal);
Send("异步第二次!", Level.High);
Send("异步第三次!", Level.Low);
ReceiveByAsyn();
//foreach (string str in list)
//{
// Console.WriteLine(str);
//}
Console.ReadKey();
}
public static void Send(string content, Level level)
{
System.Messaging.Message message = new System.Messaging.Message();
message.Formatter = new System.Messaging.BinaryMessageFormatter();
message.Body = content;
switch (level)
{
case Level.Low:
message.Priority = MessagePriority.Low;
break;
case Level.High:
message.Priority = MessagePriority.High;
break;
default:
message.Priority = MessagePriority.Normal;
break;
}
//MessageQueueTransaction tran = new MessageQueueTransaction();
//try
//{
// tran.Begin();
// queue.Send(message);
// tran.Commit();
//}
//catch
//{
// tran.Abort();
//}
queue.Send(message, MessageQueueTransactionType.Automatic);
}
public static string Receive()
{
System.Messaging.Message message = queue.Receive();
message.Formatter = new System.Messaging.BinaryMessageFormatter();
return message.Body.ToString();
}
public static void ReceiveByAsyn()
{
queue.ReceiveCompleted += new ReceiveCompletedEventHandler(queue_ReceiveCompleted);
queue.BeginReceive();
}
private static void queue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
MessageQueue queueTmp = sender as MessageQueue;
System.Messaging.Message mess = queueTmp.EndReceive(e.AsyncResult);
mess.Formatter = new System.Messaging.BinaryMessageFormatter();
Console.WriteLine(mess.Body.ToString());
//list.Add(mess.Body.ToString());
queueTmp.BeginReceive();
}
}
}
using System.Collections.Generic;
using System.Messaging;
namespace MSMQ
{
class Program
{
private const string path = @".\private$\ljz";
private static MessageQueue queue = MessageQueue.Exists(path) ? new MessageQueue(path) : MessageQueue.Create(path, true);
//private static List<string> list = new List<string>();
public enum Level
{
Low,
Normal,
High
}
static void Main(string[] args)
{
Send("第一次!", Level.Low);
Send("第二次!", Level.Normal);
Send("第三次!", Level.High);
Console.WriteLine(Receive());
Console.WriteLine(Receive());
Console.WriteLine(Receive());
Send("异步第一次!", Level.Normal);
Send("异步第二次!", Level.High);
Send("异步第三次!", Level.Low);
ReceiveByAsyn();
//foreach (string str in list)
//{
// Console.WriteLine(str);
//}
Console.ReadKey();
}
public static void Send(string content, Level level)
{
System.Messaging.Message message = new System.Messaging.Message();
message.Formatter = new System.Messaging.BinaryMessageFormatter();
message.Body = content;
switch (level)
{
case Level.Low:
message.Priority = MessagePriority.Low;
break;
case Level.High:
message.Priority = MessagePriority.High;
break;
default:
message.Priority = MessagePriority.Normal;
break;
}
//MessageQueueTransaction tran = new MessageQueueTransaction();
//try
//{
// tran.Begin();
// queue.Send(message);
// tran.Commit();
//}
//catch
//{
// tran.Abort();
//}
queue.Send(message, MessageQueueTransactionType.Automatic);
}
public static string Receive()
{
System.Messaging.Message message = queue.Receive();
message.Formatter = new System.Messaging.BinaryMessageFormatter();
return message.Body.ToString();
}
public static void ReceiveByAsyn()
{
queue.ReceiveCompleted += new ReceiveCompletedEventHandler(queue_ReceiveCompleted);
queue.BeginReceive();
}
private static void queue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
MessageQueue queueTmp = sender as MessageQueue;
System.Messaging.Message mess = queueTmp.EndReceive(e.AsyncResult);
mess.Formatter = new System.Messaging.BinaryMessageFormatter();
Console.WriteLine(mess.Body.ToString());
//list.Add(mess.Body.ToString());
queueTmp.BeginReceive();
}
}
}