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. 【Machine Learning】决策树之简介(1)

    Content 1.decision tree representation 2.ID3:a top down learning algorithm 3.expressiveness of data ...

  2. bootstrap colorscheme以及theme自动生成

    http://paintstrap.com/ 是一个根据adobe kuler color scheme自动生成theme 的工具,比较直观好用,对于调整前端theme有一定参考意义

  3. NgModelController: $setViewValue,$render,Formatter, Parser

    NgModelController为ngModel directive提供了API.这个controller包含了关于data-binding,validation,css update, value ...

  4. MySQL半同步复制的搭建和配置原理

    半同步复制: 什么是半同步复制?我们知道在默认情况下,MySQL的复制是异步的,这意味着主服务器及其从服务器是独立的.异步复制可以提供最佳的性能,因为主服务器在将更新的数据写入它的二进制日志(Binl ...

  5. Oracle案例02——ORA-12034: "SCOTT"."USER_TABLE" 上的实体化视图日志比上次刷新后的内容新

    最近同事在交接工作时,发现有几个schedule job没有执行成功,我这边给看了下,其中一个是由于数据库迁移,调用dblink的host主机IP在tnsnames中没有变更导致,还有一个是无法视图的 ...

  6. UIButton的titleLabel

    UIButton的titleLabel @property(nonatomic, readonly, retain) UILabel *titleLabel Description - 描述A vie ...

  7. Sandworm Attack小结

    这个漏洞刚出来时就分析过,当时大致弄明白了原理,但对很多细节和原理还是一知半解.后来开始找工作……今天终于有时间来把欠的这部分功课补上. 这个漏洞网上的各种中英文分析已经很多了,因此这里我只根据自己的 ...

  8. python 利用栈实现复杂计算器

    #第五周的作业--多功能计算器#1.实现加减乘除及括号的优先级的解析,不能使用eval功能,print(eval(equation))#2.解析复杂的计算,与真实的计算器结果一致#用户输入 1 - 2 ...

  9. vscode:解决操作git总让输入用户名及密码问题

    只要是使用git操作,不管是同步,拉去,克隆,vscode总让我们输入用户名及密码,是一件很繁琐的事情 我们打开终端,会看到cmd定位在我们仓库位置,我们只要添加:git config --globa ...

  10. C# Windows服务的安装和卸载批处理

    @ECHO "请按任意键开始安装后台服务. . ."@ECHO "清理原有服务项. . ."%SystemRoot%\Microsoft.NET\Framewo ...