微软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带来的好处是:由于是异步通信,无论是发送方还是接收方都不用等待对方返回成功消息,就可以执行余下的代 ...
随机推荐
- vs2012+qt5.2.0环境搭建
1.安装vs2012: 2.下载Qt 5.2.0 for Windows 32-bit(VS 2012, 579 MB) 和 Visual Studio Add-in 1.2.2for Qt5 注意: ...
- map的4种遍历方式
System.out.println("key= "+ key + " and value= " + map.get(key)); } ...
- silk与opencore-amr音频编码对比
silk与opencore-amr编码对比 在采样率8000 单声道 16位采样精度情况下 silk的压缩率为 1/15 opencore-amr 1/17 对比图 原始的音频编码 opencore- ...
- linux mysql查看安装信息
ps -ef|grep mysql root ? :: /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mys ...
- MySQL数据故障时备份与恢复
1.ib_logfile0和ib_logfile1是mysql用来存储操作执行的日志文件,用于事务暂存和回滚.当复制ibdata1数据文件到新的mysql中时,如果没有拷贝这两个日志文件,则会出现启动 ...
- MySQL Plugin 'InnoDB' init function returned error
. . 在MySQL的配置文件中,设定default-table-type=InnoDB,发现MySQL无法正常的启动,错误日志中给出了如下的信息: 150210 18:11:19 mysqld_sa ...
- php bom \ufeff
2015年5月29日 16:50:56 星期五 五月的最后一个周五............. 前两天遇到一个问题 PHP 返回json数据, 其他人死活解析不出来 json_last_error(); ...
- 17. javacript高级程序设计-错误处理与调试
1. 错误处理与调试 l 在可能发生错误的地方使用try-catch方法,可以对错误进行及时的相应 l 使用window.onerror事件处理程序,这种方式可以接受try-catch不能处理的所有错 ...
- apache官网怎样下载apache HTTP Server服务器
我相信有些朋友刚用apache服务器时,都希望从官网上下载,而面对着官网上众多的项目和镜像以及目录,也许有点茫然.下面是具体步骤 第一步:打开apache官网 第二步:点击右上角Download,出现 ...
- Tesseract-OCR 3.05 躲过语言文字识别(运行程序+中英日韩语言包)
最新版本 静态编译 tesseract 3.05.00dev leptonica-1.73 libgif 5.1.3 : libjpeg 8c : libpng 1.6.16 : libtiff 3. ...