消息队列”是 Microsoft 的消息处理技术,它在任何安装了 Microsoft Windows 的计算机组合中,为任何应用程序提供消息处理和消息队列功能,无论这些计算机是否在同一个网络上或者是否同时联机。
“消息队列网络”是能够相互间来回发送消息的任何一组计算机。网络中的不同计算机在确保消息顺利处理的过程中扮演不同的角色。它们中有些提供路由信息以确定如何发送消息,有些保存整个网络的重要信息,而有些只是发送和接收消息。
“消息队列”安装期间,管理员确定哪些服务器可以互相通信,并设置特定服务器的特殊角色。构成此“消息队列”网络的计算机称为“站点”,它们之间通过“站点链接”相互连接。每个站点链接都有一个关联的“开销”,它由管理员确定,指示了经过此站点链接传递消息的频率。
利用消息队列可以很好的异步来出来数据传送和存储,当你频繁的向数据库中插入时,就可采取消息队列来异步向数据库中插入数据。向将要插入的数据发送到指定的消息队列中,在有后台将消息读取插入到数据库中,在Pet Shop 4.0中接订单的操作就是利用消息队列,先将接下的订单插入到消息队列中就立即返回,由后台接收消息插入订单。
下面是我写的两个小程序,分别用来向消息队列中写入消息和向消息队列中读取消息。界面如下:
![](https://images.cnblogs.com/cnblogs_com/zhang3533/MSMQClient.JPG)
![](https://images.cnblogs.com/cnblogs_com/zhang3533/MSMQServer.JPG)
我是在VS2005中实现的,首先要添加System.Messaging.dll的引用:
主要代码:
//1 创建消息队列
private void btnCreate_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
String Path = txtPath.Text;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Cursor = Cursors.WaitCursor;
//判断队列是否存在,不存在就创建
EnsureQueueExists(Path);
MyQueue = new System.Messaging.MessageQueue(Path,true);
this.listView1.Items.Clear();
tssl.Text = "创建成功!";
}
catch (MessageQueueException ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(ex.Message);
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Cursor = Cursors.Default;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
EnsureQueueExists方法
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// 检查队列,如果队列不存在,则建立
///
/// 队列名称
private static void EnsureQueueExists(string path)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (!System.Messaging.MessageQueue.Exists(path))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!System.Messaging.MessageQueue.Exists(path))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Messaging .MessageQueue.Create(path,false);
System.Messaging.MessageQueue mqTemp = new System.Messaging.MessageQueue(path);
mqTemp.SetPermissions("Everyone", System.Messaging.MessageQueueAccessRights.FullControl);
//mqTemp.Label = "MMM";
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
发送消息
//发送消息
private void btnSend_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
MessageQueueTransaction myTransaction= new MessageQueueTransaction();;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (MyQueue == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
//Order为一个类对象,有两个对象:消息和发送的时间
Model.Order order = new Model.Order();
order.Message = txtMessage.Text;
order.SendTime = DateTime.Now;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//加入事务,前提条件是该消息队列在创建时支持事务
myTransaction.Begin();
//发送
MyQueue.Send(order);
myTransaction.Commit();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
InsertListView(order);
}
catch (MessageQueueException ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//myTransaction.Abort();
MessageBox.Show(ex.Message);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
接受消息端:
private void btnListen_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string Path = txtPath.Text.Trim ();
if (!System.Messaging.MessageQueue.Exists(Path))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("该消息队列不存在");
txtPath.SelectAll();
return;
}
this.listView1.Items.Clear();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Messaging.MessageQueue MyQueue = new System.Messaging.MessageQueue(Path);
//这句很重要,序列化接受到的消息数据,否则不知道接收到的数据,我发送的数据类型为Order,所以此也为
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
MyQueue .Formatter =new XmlMessageFormatter (new Type []
{typeof (Model .Order)});
MyQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted);
MyQueue.BeginReceive();
}
catch (MessageQueueException ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(ex.Message);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
异步接收消息
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageQueue mq = (MessageQueue)source;
System.Messaging .Message ms = mq.EndReceive(asyncResult.AsyncResult);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
listView1.Invoke(new EventHandler(Event), new object[]
{ ms.Body, EventArgs.Empty });
mq.BeginReceive();
}
catch(MessageQueueException ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(ex.Message);
}
return;
}
private void Event(object o, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
InsertListView((Model .Order )o);
}
private void InsertListView(Model.Order Obj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
ListViewItem Item = new ListViewItem();
Item.Text = Obj.Message;
Item.SubItems.Add(Obj.SendTime.ToString ());
this.listView1.Items.Insert(0,Item);
}
}
最后提供完整的代码,大家可以改进代码利用在自己的项目中(补充要能正常运行安装Windows的可选组件消息队列)
/Files/zhang3533/MessageQueue.zip