代码
//顾名思义,微软消息队列(MSMQ)是一种给队列发送消息以便稍后进行处理的方法。
//消息由一个“Producer”(生产者)应用程序发送出去,再由一个“Consumer”(消费者)应用程序返回。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
using System.Threading;
namespace Model
{
public class MSM
{
private MessageQueue _messageQueue = null;
private string queuePath = @".\Private$\MSMQ";
private string formatType = "xml";
public MessageQueue MessQueue
{
get
{
if (_messageQueue == null)
{
if (MessageQueue.Exists(queuePath))
{
_messageQueue = new MessageQueue(queuePath);
}
else
{
_messageQueue = MessageQueue.Create(queuePath);
}
}
return _messageQueue;
}
}
//发送消息
public void SendMessage(User u)
{
if (formatType == "xml")
{
MessQueue.Formatter = new XmlMessageFormatter();
}
else
{
MessQueue.Formatter = new BinaryMessageFormatter();
}
Message m = new Message(u);
MessQueue.Send(m);
}
//MessageQueue.Receive得到队列中的第一条消息,并从队列中删除这条消息
public User Recieve(int timeout)
{
Message m = MessQueue.Receive(TimeSpan.FromSeconds(timeout));
m.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(User) });
return (User)m.Body;
}
//MessageQueue.Receive得到队列中的第一条消息,但不从队列中删除这条消息
public User Peek(int timeout)
{
Message m = MessQueue.Peek(TimeSpan.FromSeconds(timeout));
m.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(User) });
return (User)m.Body;
}
//最常见的情况是从队列中取出所有消息,然后一次性处理它们。这时要调用MessageQueue.GetAllMessages方法。
public List<User> RecieveMessage()
{
List<User> users = new List<User>();
MessQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(User) });
foreach (Message m in MessQueue.GetAllMessages())
{
users.Add((User)m.Body);
}
return users;
}
//GetMessageEnumerator2方法只领取当前消息在本地保存,在调用MoveNext时才领取下一条消息
public List<User> RecieveMessage2()
{
MessageEnumerator enumerator = MessQueue.GetMessageEnumerator2();
MessQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(User) });
List<User> users = new List<User>();
while (enumerator.MoveNext())
{
users.Add ((User)enumerator.Current.Body);
enumerator.RemoveCurrent();//删除当前消息
}
return users;
}
}
}
下面简单的测试一下:
发送消息:
class Send
{
static void Main(string[] args)
{
MessageSendTest();
Console.ReadLine();
}
public static void MessageSendTest()
{
MSM msm = new MSM();
for (int i = 0; i < 10; i++)
{
User user = new User()
{
UserId = i.ToString(),
UserName = "userName:" + i.ToString(),
UserPwd = "UserPWd:" + i.ToString(),
Version = "Version:" + i.ToString()
};
Thread.Sleep(5000);
msm.SendMessage(user);
Console.WriteLine("已发送:"+user.UserId + "-----" + user.UserName + "-----" + user.UserPwd + "-----" + user.Version);
}
//List<User> users=msm.RecieveMessage();
//foreach (User user in users)
//{
// Console.WriteLine("已发送:" + user.UserId + "-----" + user.UserName + "-----" + user.UserPwd + "-----" + user.Version);
//}
}
}
接收消息:
class Recieve
{
static void Main(string[] args)
{
while (true)
{
GetMessage();
}
}
public static void GetMessage()
{
MSM msm = new MSM();
User user = msm.Recieve(5);
Console.WriteLine("已接收:"+user.UserId + "-----" + user.UserName + "-----" + user.UserPwd + "-----" + user.Version);
}
}
//消息由一个“Producer”(生产者)应用程序发送出去,再由一个“Consumer”(消费者)应用程序返回。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
using System.Threading;
namespace Model
{
public class MSM
{
private MessageQueue _messageQueue = null;
private string queuePath = @".\Private$\MSMQ";
private string formatType = "xml";
public MessageQueue MessQueue
{
get
{
if (_messageQueue == null)
{
if (MessageQueue.Exists(queuePath))
{
_messageQueue = new MessageQueue(queuePath);
}
else
{
_messageQueue = MessageQueue.Create(queuePath);
}
}
return _messageQueue;
}
}
//发送消息
public void SendMessage(User u)
{
if (formatType == "xml")
{
MessQueue.Formatter = new XmlMessageFormatter();
}
else
{
MessQueue.Formatter = new BinaryMessageFormatter();
}
Message m = new Message(u);
MessQueue.Send(m);
}
//MessageQueue.Receive得到队列中的第一条消息,并从队列中删除这条消息
public User Recieve(int timeout)
{
Message m = MessQueue.Receive(TimeSpan.FromSeconds(timeout));
m.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(User) });
return (User)m.Body;
}
//MessageQueue.Receive得到队列中的第一条消息,但不从队列中删除这条消息
public User Peek(int timeout)
{
Message m = MessQueue.Peek(TimeSpan.FromSeconds(timeout));
m.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(User) });
return (User)m.Body;
}
//最常见的情况是从队列中取出所有消息,然后一次性处理它们。这时要调用MessageQueue.GetAllMessages方法。
public List<User> RecieveMessage()
{
List<User> users = new List<User>();
MessQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(User) });
foreach (Message m in MessQueue.GetAllMessages())
{
users.Add((User)m.Body);
}
return users;
}
//GetMessageEnumerator2方法只领取当前消息在本地保存,在调用MoveNext时才领取下一条消息
public List<User> RecieveMessage2()
{
MessageEnumerator enumerator = MessQueue.GetMessageEnumerator2();
MessQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(User) });
List<User> users = new List<User>();
while (enumerator.MoveNext())
{
users.Add ((User)enumerator.Current.Body);
enumerator.RemoveCurrent();//删除当前消息
}
return users;
}
}
}
下面简单的测试一下:
发送消息:
class Send
{
static void Main(string[] args)
{
MessageSendTest();
Console.ReadLine();
}
public static void MessageSendTest()
{
MSM msm = new MSM();
for (int i = 0; i < 10; i++)
{
User user = new User()
{
UserId = i.ToString(),
UserName = "userName:" + i.ToString(),
UserPwd = "UserPWd:" + i.ToString(),
Version = "Version:" + i.ToString()
};
Thread.Sleep(5000);
msm.SendMessage(user);
Console.WriteLine("已发送:"+user.UserId + "-----" + user.UserName + "-----" + user.UserPwd + "-----" + user.Version);
}
//List<User> users=msm.RecieveMessage();
//foreach (User user in users)
//{
// Console.WriteLine("已发送:" + user.UserId + "-----" + user.UserName + "-----" + user.UserPwd + "-----" + user.Version);
//}
}
}
接收消息:
class Recieve
{
static void Main(string[] args)
{
while (true)
{
GetMessage();
}
}
public static void GetMessage()
{
MSM msm = new MSM();
User user = msm.Recieve(5);
Console.WriteLine("已接收:"+user.UserId + "-----" + user.UserName + "-----" + user.UserPwd + "-----" + user.Version);
}
}
结果是每个5秒发送一个队列,Reciew就马上得到该队列。