消息队列MSMQ的使用
1.MSMQ安装
控制面板-程序和功能-打开或关闭Windows功能-Microsoft Message Queue(MSMQ)服务器,选中所有,点击确定。
2.消息队列的应用场景(转载自http://www.cnblogs.com/stopfalling/p/5375492.html)
①异步处理
②应用解耦
③流量削锋
④日志处理
⑤消息通讯
3.MSMQ消息分为事务性和非事务性
非事务性的消息保存在内存中,机器重启后丢失消息队列中的消息。
事务性的消息经过了持久化,保存在硬盘中,机器重启后队列中的消息不会丢失。
4.测试代码
消息发送方

namespace MessageQueueSender
{
class Program
{
static void Main(string[] args)
{
MessageQueue MSMQ = CreateMessageQueue(@".\private$\tpmsmq");
MSMQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); Console.WriteLine("是否继续发送消息:Y/N?");
string cmd = Console.ReadLine(); while (cmd.Equals("Y"))
{
Sender(MSMQ); Console.WriteLine("是否继续发送消息:Y/N?");
cmd = Console.ReadLine();
} Console.WriteLine("按任意键以停止...");
Console.ReadKey();
}
private static void Sender(MessageQueue MSMQ)
{
try
{
string random = GenerateRandom();
string obj = string.Format("{0} 发送方:{1}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), random); MSMQ.Send(obj, MessageQueueTransactionType.Single); Console.WriteLine(obj); }
catch (Exception ex)
{
Console.WriteLine(string.Format("{0} 发送方:{1}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message));
}
} public static MessageQueue CreateMessageQueue(string path)
{
MessageQueue mq = null; if (MessageQueue.Exists(path))
{
mq = new MessageQueue(path);
}
else
{
mq = MessageQueue.Create(path, true);
} return mq;
} public static string GenerateRandom()
{
int seed = GetRandomSeed();
return new Random(seed)
.Next(Int32.MaxValue).ToString();
} /// <summary>
/// 创建加密随机数生成器 生成强随机种子
/// </summary>
/// <returns></returns>
private static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng
= new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
} }

消息接收方

namespace MessageQueueReceiver
{
class Program
{
static void Main(string[] args)
{
MessageQueue MSMQ = CreateMessageQueue(@".\private$\tpmsmq");
MSMQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); Receiver(MSMQ);
} private static void Receiver(MessageQueue MSMQ)
{
while (true)
{
try
{
Message m = MSMQ.Receive(MessageQueueTransactionType.Single);
Console.WriteLine(string.Format("{0} 接收方:[{1}]",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), m.Body.ToString()));
}
catch (Exception ex)
{
Console.WriteLine(string.Format("{0} 接收方:{1}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message));
}
}
} public static MessageQueue CreateMessageQueue(string path)
{
MessageQueue mq = null; if (MessageQueue.Exists(path))
{
mq = new MessageQueue(path);
}
else
{
mq = MessageQueue.Create(path, true);
} return mq;
}
}
}

MessageQueue.Receive()方法是同步执行的,如果队列中没有消息,则会造成阻塞,程序会停止在这块,等待消息的产生。
消息队列MSMQ的使用的更多相关文章
- WCF分布式开发步步为赢(13):WCF服务离线操作与消息队列MSMQ
之前曾经写过一个关于MSMQ消息队列的文章:WCF分布式开发必备知识(1):MSMQ消息队列 ,当时的目的也是用它来作为学习WCF 消息队列MSMQ编程的基础文章.在那篇文章里,我们详细介绍了MSMQ ...
- C#使用消息队列(MSMQ)
最近项目用到消息队列,找资料学习了下.把学习的结果 分享出来 首先说一下,消息队列 (MSMQ Microsoft Message Queuing)是MS提供的服务,也就是Windows操作系统的功能 ...
- 详解.Net消息队列(MSMQ)应用
[IT168 技术文档]MSMQ是Windows 2000.Windows XP.Windows Server 2003的一个组件,并将继续包含在Windows Vista和以后的Windows服务器 ...
- PetShop 4.0学习笔记:消息队列MSMQ
直到今天才知道,在我们每天都在用的Window系统里还有这么好用的一个编程组件:消息队列.它能够解决在大数据量交换的情况下的性能问题,特别是BS系统的数据库性能.而且它的异步处理方式能给程序员最大的便 ...
- win7怎么安装消息队列 MSMQ
win7般都默认装了消息队列只需要进入 控制面板-程序-程序和功能-已安装更新-打开或关闭windows功能 勾选 Microsoft Message Queue (MSMQ)服务器 启动服务 行了: ...
- C# 消息队列-MSMQ
MQ是一种消息中间件技术,所以它能够支持多种类型的语言开发,同时也是跨平台的通信机制,也就是说MQ支持将信息转化为XML或者JSon等类型的数据存储到消息队列中,然后可以使用不同的语言来处理消息队列中 ...
- 消息队列msmq
http://q.cnblogs.com/q/26895/ 远程队列必须现在运程服务器上创建. 在 Windows Server 2008 上安装 IIS 服务和 MSMQ 功能后,系统会在 IIS ...
- Nginx集群之WCF分布式消息队列
目录 1 大概思路... 1 2 Nginx集群之WCF分布式消息队列... 1 3 MSMQ消息队列... 2 4 编写WCF服务.客户端程序... ...
- 消息队列_MSMQ(2)简单应用
上一篇讲了MSMQ的简单知识,那这次我们讲下简单代码的知识 附上源码: https://gitee.com/592576605/MSMQ_HANS 下面是简单的类库说明,具体咋用就看源码吧 类(Cla ...
随机推荐
- C# 修改GroupBox的边框颜色和字体颜色
改变GroupBox边框和的颜色 private void groupBox_BasicInformation_Paint(object sender, PaintEventArgs e) { e.G ...
- Python爬虫教程-20-xml 简介
本篇简单介绍 xml 在python爬虫方面的使用,想要具体学习 xml 可以到 w3school 查看 xml 文档 xml 文档链接:http://www.w3school.com.cn/xmld ...
- 【Udacity笔记】What is Machine Learning?
Teaching computers to learn to perform tasks from past experiences(recorded data) 一.Decision Tree(决策 ...
- 【Python】directory字典类型
它的基本格式是(key是键,value是值): d = {key1 : value1, key2 : value2 } Example dir = {'Mic':1,'Sun':2} for k in ...
- Oracle物化视图详解
现实工作中会有多个数据源同步到一个数据库完成数据分析的场景,这些数据可以不是实时同步的,我们一般通过定时任务抽取数据到统计分析库给应用使用. 一般的同步方式可以通过时间戳做全量和增量数据同步(存在原数 ...
- 在Server2012R2上导入Server2008R2的HyperV虚拟机
Importing Windows 2008 R2 Hyper-V VM Into Windows 8.1 For the purposes of this post, let’s try and i ...
- Python学习---Python下[字典]的学习
Python中唯一的映射类型(哈希表) -->Java中的HashMap<K,V> Python对key进行了哈希函数运算,根据计算的结果决定value的存储地址,所以字 ...
- mysql实现‘主从复制’
mysql主从复制(超简单) 怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 首先准备多台服务器,其中一台作为主服务器,从服务器数量自定. 1.主从服务器分别作以下操作: 主服务器 ...
- React v15.5.0更新说明 & v16.0.0更新预告
React今日发布了15.5.0版本,同时这也将是以15开头的最后一个版本,下一次发布,我们将迎来React 16.0.0 在15.5.0这一版本中,主要有以下两处改动: 独立React.PropTy ...
- (转)浅谈PostgreSQL的索引
1. 索引的特性 1.1 加快条件的检索的特性 当表数据量越来越大时查询速度会下降,在表的条件字段上使用索引,快速定位到可能满足条件的记录,不需要遍历所有记录. create table t(id i ...