消息队列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 ...
随机推荐
- Android中如何在Eclipse中关联源代码?(图文)
关联源代码 1.删除工程里面的Android Depandencies,删除后会报错,不要理会.看下面 2.添加libs目录下的Android-support-v4.jar包 选中-->右键-- ...
- golang rest api example
package main import ( "net/http" "github.com/gin-gonic/gin" "github.com/jin ...
- 如何判断单链表是否存在环 & 判断两链表是否相交
给定一个单链表,只给出头指针h: 1.如何判断是否存在环? 2.如何知道环的长度? 3.如何找出环的连接点在哪里? 4.带环链表的长度是多少? 解法: 1.对于问题1,使用追赶的方法,设定两个指针sl ...
- win+ R下的常见命令
-------------------------电脑运行常见命令----------------------------- Windows+R输入cmd 运行net start mssqlserve ...
- 【Leetcode】【Medium】Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- ElasticSearch之常用插件安装命令
#head监控安装,推荐 bin/plugin -install mobz/elasticsearch-head #bigdesk集群状态,推荐 bin/plugin -install lukas-v ...
- 沉淀,再出发:Maven的使用和规范
沉淀,再出发:Maven的使用和规范 一.前言 Maven作为项目管理工具,在一个大型项目开发的每个阶段都有着很大的用处,为什么需要这个东西呢,还是为了消除不确定性,统一化管理,正如我们做的每一件事其 ...
- 合并两个数组 以KEY 作为键
<?php $a= array( array( 'ID'=> 2 ) ); $b= array( arr ...
- centos7下搭建sphinx全文检索引擎
Sphinx是一个基于SQL的全文检索引擎,可以结合MySQL,PostgreSQL做全文搜索,它可以提供比数据库本身更专业的搜索功能,使得应用 程序更容易实现专业化的全文检索.Sphinx特别为一些 ...
- HttpServletRequest的随手记
request对象的三个主要的方法: getParameter(String name):获取请求参数名字对应的参数值.只获取一个.如果名字对应有多个参数值的话,那么只会获取参数数组中的第一个.比如一 ...