微软MSMQ消息队列的使用
首先在windows系统中安装MSMQ
一、MSMQ交互
开发基于消息的应用程序从队列开始。MSMQ包含四种队列类型:
- 外发队列:消息发送到目的地之前,用它来临时存储消息。
- 公共队列:在主动目录中公布。整个网络各种服务器上的应用程序能够通过主动目录找到并应用公共队列。
- 私有队列:这些是本地服务器上的队列,对其它服务器无效(因此这些队列不在主动目录中公布。)
- 系统队列:包含日记队列(由系统生成)、死队列和事务型死信队列。死消息无法传送。
System.Messaging命名空间执行MSMQ的编程操作。这个命名空间有两个主要的对象:
- Message:队列发送或读取的实际消息或数据。
- MessageQueue:接收/发送消息的MSMQ消息队列。
二、消息队列类型
消息对列分为3类:
公共队列
MachineName\QueueName
能被别的机器所访问,如果你的多个项目中用到消息队列,那么你可以把队列定义为公共队列
专用队列
MachineName\Private$\QueueName
只针对于本机的程序才可以调用的队列,有些情况下为了安全起见定义为私有队列。
日志队列
MachineName\QueueName\Journal$
然后编写以下代码:
消息的发送:
class Program
{
static void Main(string[] args)
{
Message msg = null;
MessageQueue mq = null;
int count = ;
if (MessageQueue.Exists(@".\Private$\TechRepublic"))
{
MessageQueue.Delete(@".\Private$\TechRepublic");
}
while (true)
{
count++; try
{
msg = new Message();
msg.Priority = MessagePriority.Normal;
if (!MessageQueue.Exists(@".\Private$\TechRepublic"))
{
mq = MessageQueue.Create(@".\Private$\TechRepublic");
}
else
{
mq = new MessageQueue(@".\Private$\TechRepublic");
}
msg.Label = "Test Message" + count;
Product p = new Product
{
Id = count,
Name = "name"+count,
CreateTime = DateTime.Now
};
msg.Body = p;
mq.Send(msg);
Console.WriteLine(p.Id); }
catch (System.Messaging.MessageQueueException ex)
{
Console.WriteLine("MSMQ Error: " + ex.ToString());
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.ToString());
} } #region 发送简单的类型
//try
//{
// mq = new MessageQueue(@".\Private$\TechRepublic");
// msg = mq.Receive(new TimeSpan(0, 0, 60));
// msg.Formatter = new XmlMessageFormatter(new String[] { "System.String,mscorlib" });
// //mq.Receive();
// Console.WriteLine(msg.Label.ToString() + " -- " + msg.Body.ToString());
//}
//catch (System.Messaging.MessageQueueException ex)
//{
// Console.WriteLine("MSMQ Error: " + ex.ToString());
//}
//catch (Exception ex)
//{
// Console.WriteLine("Error: " + ex.ToString());
//}
//finally
//{
// mq.Close();
//}
#endregion
Console.ReadKey();
}
}
消息的接收:
class Program
{ static void Main(string[] args)
{
Message msg = null;
MessageQueue mq = null; while (true)
{
Thread.Sleep();
try
{
if (MessageQueue.Exists(@".\Private$\TechRepublic"))
{
mq = new MessageQueue(@".\Private$\TechRepublic");
//#region 同步接收消息
//if (mq.GetAllMessages().Any())
//{
//msg = mq.Receive(new TimeSpan(0, 0, 60));
////通过Receive方法接收消息同时永久性地从队列中删除消息;
////通过Peek方法从队列中取出消息而不从队列中移除该消息。
//msg.Formatter = new XmlMessageFormatter(new String[] { "MyLib.Product,MyLib" });
//Product p = ((Product)msg.Body);
//Console.WriteLine(msg.Label.ToString() + " -- " + p.Id + " ---" + p.CreateTime);
//}
//#endregion #region 异步接收消息 //利用委托机制:
if (mq.GetAllMessages().Any())
{
mq.BeginReceive();
mq.ReceiveCompleted += mq_ReceiveCompleted;
}
#endregion } }
catch (System.Messaging.MessageQueueException ex)
{
Console.WriteLine("MSMQ Error: " + ex.ToString());
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.ToString());
}
} Console.ReadKey();
} static void mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
MessageQueue mq = (MessageQueue)sender;
Message msg = mq.EndReceive(e.AsyncResult);
msg.Formatter = new XmlMessageFormatter(new String[] { "MyLib.Product,MyLib" });
Product p = ((Product)msg.Body);
Console.WriteLine(msg.Label.ToString() + " -- " + p.Id + " ---" + p.CreateTime);
mq.BeginReceive();//如果希望继续异步接收消息,请在 ReceiveCompleted 事件处理程序中再次调用 BeginReceive 方法
}
}
namespace MyLib
{
[Serializable]
public class Product
{
public long Id { get; set; }
public string Name { get; set; }
public DateTime CreateTime { get; set; }
public DateTime? UpdateTime { get; set; }
}
}
微软MSMQ消息队列的使用的更多相关文章
- 【转】MSMQ消息队列安装
一.Windows 7安装.管理消息队列1.安装消息队列 执行用户必须要有本地 Administrators 组中的成员身份,或等效身份. 具体步骤: 开始—>控制面板—>程 ...
- MSMQ消息队列安装
一.Windows 7安装.管理消息队列1.安装消息队列 执行用户必须要有本地 Administrators 组中的成员身份,或等效身份. 具体步骤: 开始—>控制面板—>程 ...
- 【6】.net msmq消息队列实例
1.msmq消息队列windows环境安装 控制面板---->程序和功能---->启用或关闭Windows程序---->Microsoft Message Queue(MSMQ)服务 ...
- MSMQ消息队列 用法
引言 接下来的三篇文章是讨论有关企业分布式开发的文章,这三篇文章筹划了很长时间,文章的技术并不算新,但是文章中使用到的技术都是经过笔者研究实践后总结的,正所谓站在巨人的肩膀上,笔者并不是巨人,但也希望 ...
- WCF分布式开发必备知识(1):MSMQ消息队列
本章我们来了解下MSMQ的基本概念和开发过程.MSMQ全称MicroSoft Message Queue,微软消息队列,是在多个不同应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一 ...
- 跟我一起学WCF(1)——MSMQ消息队列
一.引言 Windows Communication Foundation(WCF)是Microsoft为构建面向服务的应用程序而提供的统一编程模型,该服务模型提供了支持松散耦合和版本管理的序列化功能 ...
- C#实战Microsoft Messaging Queue(MSMQ)消息队列(干货)
前言 在使用MSMQ之前,我们需要自行安装消息队列组件!(具体安装方法大家自己搜一下吧) 采用MSMQ带来的好处是:由于是异步通信,无论是发送方还是接收方都不用等待对方返回成功消息,就可以执行余下的代 ...
- MSMQ消息队列
MSMQ全称MicroSoft Message Queue,微软消息队列,是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任一 ...
- C#实战Microsoft Messaging Queue(MSMQ)消息队列
前言 在使用MSMQ之前,我们需要自行安装消息队列组件!(具体安装方法大家自己搜一下吧) 采用MSMQ带来的好处是:由于是异步通信,无论是发送方还是接收方都不用等待对方返回成功消息,就可以执行余下的代 ...
随机推荐
- OOP复习笔记
/*OOP相关的代名词不做讲解*/ OOP的三大特征: 封装 - 继承 - 多态 -----------------------------------目录---------------------- ...
- PHP中九大缓存技术总结
PHP缓存包括PHP编译缓存和PHP数据缓存两种.PHP是一种解释型语言,属于边编译边运行的那种.这种运行模式的优点是程序修改很方便,但是运行效率却很低下.PHP编译缓存针对这种情况做改进处理,使得P ...
- __getattr__ 与动态属性
直接上代码 >>> class Test(object): ... def __getattr__(self,attr_name): ... setattr(self, attr_n ...
- linux awk, xargs
awk , 很赞的教程:http://coolshell.cn/articles/9070.html xargs, http://blog.csdn.net/andy572633/article/de ...
- phpcms采集地址中为相对路径解决方法
1.修改数据库v9_collection_node,增加两个字段replace_from,replace_to(varchar(200)) 2./phpcms/modules/collection/t ...
- Android SDK在线更新镜像服务器
http://www.cnblogs.com/tc310/p/4696731.html#_label0
- Discovering versions from the identity service failed when creating the password plugin.
If you are behind the proxy, then you have to set no_proxy environmental variable for your localhost ...
- Codeanywhere
停用了一个,试一试这个怎么样. 网速太慢了,还在摸索中,没有放弃这个. 备选为Cloud9
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- C#之系统自带保存属性
源代码下载链接 程序开发很多时候需要根据运行环境做不通的参数配置,通过写ini之类的文本文件是一种方法,但这种方法也同时会把数据暴露 Winform开发中可以将需要配置的字段属性保存到程序中(其实也是 ...