微软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带来的好处是:由于是异步通信,无论是发送方还是接收方都不用等待对方返回成功消息,就可以执行余下的代 ...
随机推荐
- Linux之Shell的算术运算
在Bash的算术运算中有以下几种方法:名称 语法 范例算术扩展 $((算术式)) r ...
- Codeforces 519 E. A and B and Lecture Rooms
Description 询问一个树上与两点距离相等的点的个数. Sol 倍增求LCA. 一棵树上距离两点相等,要么就只有两点的中点,要么就是与中点相连的所有点. 有些结论很容易证明,如果距离是偶数,那 ...
- 29 GroupSock(NetAddressList)——live555源码阅读(四)网络
29 GroupSock(NetAddressList)——live555源码阅读(四)网络 29 GroupSock(NetAddressList)——live555源码阅读(四)网络 简介 Net ...
- django的cookie 和session
Cookie 1.获取cookie: request.COOKIES['key'] request.get_signed_cookie(key, default=RAISE_ERROR, salt=' ...
- Additive Number
Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...
- Semantic-UI-React (称 stardust) 对比 Antd
Semantic-UI-React: http://react.semantic-ui.com/ ANTD :http://ant.design/ Amaze UI React: http://ama ...
- Mybatis中的in查询和foreach标签
Mybatis中的foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separato ...
- ACM/ICPC 之 SPFA范例两道(POJ3268-POJ3259)
两道以SPFA算法求解的最短路问题,比较水,第二题需要掌握如何判断负权值回路. POJ3268-Silver Cow Party //计算正逆最短路径之和的最大值 //Time:32Ms Memory ...
- 13.SpringMVC和Spring集成(一) && 14.SpringMVC和Spring集成(二)
1.概念 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,Spring致力于J2EE应用的各层的解决方案,Spring是企业应用开发的“一站式”选择,并贯 ...
- (Win7 x64)NetBeans 8.0.2 使用Tomcat 8作为服务器
1.下载Apache Tomcat,解压至本地硬盘的根目录. 2.运行CMD,输入: 解压盘符:\apache-tomcat-8.0.xx\bin\service.bat install 3.安装完成 ...