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的使用的更多相关文章

  1. WCF分布式开发步步为赢(13):WCF服务离线操作与消息队列MSMQ

    之前曾经写过一个关于MSMQ消息队列的文章:WCF分布式开发必备知识(1):MSMQ消息队列 ,当时的目的也是用它来作为学习WCF 消息队列MSMQ编程的基础文章.在那篇文章里,我们详细介绍了MSMQ ...

  2. C#使用消息队列(MSMQ)

    最近项目用到消息队列,找资料学习了下.把学习的结果 分享出来 首先说一下,消息队列 (MSMQ Microsoft Message Queuing)是MS提供的服务,也就是Windows操作系统的功能 ...

  3. 详解.Net消息队列(MSMQ)应用

    [IT168 技术文档]MSMQ是Windows 2000.Windows XP.Windows Server 2003的一个组件,并将继续包含在Windows Vista和以后的Windows服务器 ...

  4. PetShop 4.0学习笔记:消息队列MSMQ

    直到今天才知道,在我们每天都在用的Window系统里还有这么好用的一个编程组件:消息队列.它能够解决在大数据量交换的情况下的性能问题,特别是BS系统的数据库性能.而且它的异步处理方式能给程序员最大的便 ...

  5. win7怎么安装消息队列 MSMQ

    win7般都默认装了消息队列只需要进入 控制面板-程序-程序和功能-已安装更新-打开或关闭windows功能 勾选 Microsoft Message Queue (MSMQ)服务器 启动服务 行了: ...

  6. C# 消息队列-MSMQ

    MQ是一种消息中间件技术,所以它能够支持多种类型的语言开发,同时也是跨平台的通信机制,也就是说MQ支持将信息转化为XML或者JSon等类型的数据存储到消息队列中,然后可以使用不同的语言来处理消息队列中 ...

  7. 消息队列msmq

    http://q.cnblogs.com/q/26895/ 远程队列必须现在运程服务器上创建. 在 Windows Server 2008 上安装 IIS 服务和 MSMQ 功能后,系统会在 IIS  ...

  8. Nginx集群之WCF分布式消息队列

    目录 1       大概思路... 1 2       Nginx集群之WCF分布式消息队列... 1 3       MSMQ消息队列... 2 4       编写WCF服务.客户端程序... ...

  9. 消息队列_MSMQ(2)简单应用

    上一篇讲了MSMQ的简单知识,那这次我们讲下简单代码的知识 附上源码: https://gitee.com/592576605/MSMQ_HANS 下面是简单的类库说明,具体咋用就看源码吧 类(Cla ...

随机推荐

  1. 多线程 读写锁SRWLock

    在<秒杀多线程第十一篇读者写者问题>文章中我们使用事件和一个记录读者个数的变量来解决读者写者问题.问题虽然得到了解决,但代码有点复杂.本篇将介绍一种新方法——读写锁SRWLock来解决这一 ...

  2. 【windows c】 遍历目录

    方式一: DWORD z_dRed = 0; char z_FilePath[MAX_PATH] = {0}; char z_newPath[MAX_PATH] = {0}; char z_tmpPa ...

  3. windows 打印刻录 代码规范及问题

    1.获取CString字节数 int z_Len = WideCharToMultiByte(CP_ACP, 0, z_FileXml.GetBuffer(), z_FileXml.GetLength ...

  4. ST Link 调试问题总结

    用过ST Link调试工具的同事都应该知道,ST Link是一个很不错的调试工具,它具有小并且功能齐全,价格便宜等特点,现在市场上普遍是下面这两种ST Link, 但如果用的比较多,会发现有时候会存在 ...

  5. Atlas+Keepalived实现MySQL读写分离、读负载均衡

    一.基础介绍 ========================================================================================== 1. ...

  6. java img图片转pdf 工具类

    package com.elitel.hljhr.comm.web.main.controller; import java.io.File; import java.io.FileNotFoundE ...

  7. docker images存放路径指定

    steps: 1. mkdir /home/docker(你想要docker存放image的目录) 2. systemctl stop docker 3. vi /usr/lib/systemd/sy ...

  8. Python学习---socketServer编程

    学会去看源码 服务器端: import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): ...

  9. Realtek无线网卡对于Ubuntu的WiFi不支持的处理办法

    1.应急办法:查询rfkill list all,可以看到ideapad_laptop的无线被物理关闭,可是博主的笔记本根本没这个键位,所以执行sudo modprobe -r ideapad_lap ...

  10. RequireJS进阶-模块的优化及配置的详解

    概述 关于RequireJS已经有很多文章介绍过了.这个工具可以将你的JavaScript代码轻易的分割成苦干个模块(module)并且保持你的代码模块化与易维护性.这样,你将获得一些具有互相依赖关系 ...