C# 消息队列-MSMQ
MQ是一种消息中间件技术,所以它能够支持多种类型的语言开发,同时也是跨平台的通信机制,也就是说MQ支持将信息转化为XML或者JSon等类型的数据存储到消息队列中,然后可以使用不同的语言来处理消息队列中的消息,这样就很容易的做到了信息的通信,同时也为信息的通信起到了缓冲的作用,经常会在金融项目中使用这种通信机制。
1 MQ安装
打开控制面板-“添加/删除程序” – “添加/删除 windows组件”步骤安装MSMQ。
MSMQ可以安装为工作组模式或域模式。如果安装程序没有找到一台运行提供目录服务的消息队列的服务器,则只可以安装为工作组模式,此计算机上的“消息队列”只支持创建专用队列和创建与其他运行“消息队列”的计算机的直接连接。
2配置MSMQ
打开计算机管理 – 服务和应用程序-消息队列,在专用队列下创建MSMQDemo队列

3 MQ Demo
在.NET中微软对MQ做了封装,把MQ有关的信息封装到了MessageQueue类中,在开发的时候可以直接引用该类,对队列中的消息做操作。
在操作消息前首先要为消息指定存储的队列,所以在创建消息时首先要在服务器上创建一个队列,然后为MessageQueue指定消息队列的路径。
接下来演示发送数据和接收数据代码的编写方法,下面的示例中使用的是私有的队列类型来演示的操作。首先从发送数据开始,在发送数据时首先要创建我们的MQ,然后根据MQ的地址创建相应的队列,调用队列的send方法将数据信息发送到队列中,如下代码:
private void SendMailMQ(List<string> addressStr, string account, string password, string host, string port, string title, string message, long TenantId)
{
try
{
//声明MQ路径
var ekQ = ".\\Private$\\mailqueue";
//不存在路径时创建
if (!MessageQueue.Exists(ekQ))
MessageQueue.Create(ekQ); //创建实例化
var queue = new MessageQueue(ekQ); foreach (var item in addressStr)
{
var encryptStr = JsonConvert.SerializeObject(
new
{
MailMsg = new
{
account = account,
password = password,
host = host,
port = port,
title = title,
message = message,
address = item
},
TenantId = TenantId
});
queue.Send(encryptStr);
SaveLog(encryptStr);
}
}
catch (Exception ex)
{
SaveLog("异常:" + ex.Message + ex.StackTrace);
}
}
运行上面的代码后MQ将会把消息发送到相应的队列中,这里采用的是专有队列所以会将消息发送到本地的队列中,这样在消息的发送方和调用方之间就构建了一个相互松耦合的桥梁,它就是消息队列,
接下来演示如何接收消息队列。
public class MailThread
{
MessageQueue queue;
public void StartMailMQ()
{
string ekQ = ".\\Private$\\mailqueue";
//不存在路径时创建
if (!MessageQueue.Exists(ekQ))
MessageQueue.Create(ekQ); queue = new MessageQueue(ekQ); queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });
while (!MessageQueue.Exists(ekQ))
{
Console.WriteLine("未找到mailqueue队列");
}
queue.ReceiveCompleted += Queue_ReceiveCompleted;
queue.BeginReceive(MessageQueue.InfiniteTimeout);
} private void Queue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
var m = e.Message;
var deceivedMsg = m.Body.ToString();
Console.WriteLine("接收邮件消息内容:{0} \n--------------", (string)m.Body);
if (!string.IsNullOrEmpty(deceivedMsg))
new DispatchMessager().SendMail(deceivedMsg); queue.BeginReceive(MessageQueue.InfiniteTimeout);
}
}
尾声
MQ是一种企业服务的消息中间节技术,这种技术常常伴随着企业服务总线相互使用,构成了企业分布式开发的一部分,如果考虑到消息的发送和传送之间是可以相互不联系的并且需要分布式架构,则可以考虑使用MQ做消息的中间价技术,MQ的功能已经足够开发使用。
C# 消息队列-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)服务器 启动服务 行了: ...
- 消息队列MSMQ的使用
1.MSMQ安装 控制面板-程序和功能-打开或关闭Windows功能-Microsoft Message Queue(MSMQ)服务器,选中所有,点击确定. 2.消息队列的应用场景(转载自http:/ ...
- 消息队列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 ...
随机推荐
- SDL 开发实战(一):SDL介绍及开发环境配置
一.什么是SDL? SDL是 “Simple DirectMedia Layer”的缩写,SDL是一个开源的跨平台的多媒体库,封装了复杂的音视频底层操作,简化了音视频处理的难度. SDL使用C语言写成 ...
- [Swift]LeetCode1 .两数之和 | Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [Swift]LeetCode340.最多有K个不同字符的最长子串 $ Longest Substring with At Most K Distinct Characters
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [Swift]LeetCode348. 设计井字棋游戏 $ Design Tic-Tac-Toe
Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...
- [Swift]LeetCode730. 统计不同回文子字符串 | Count Different Palindromic Subsequences
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [Swift]LeetCode735. 行星碰撞 | Asteroid Collision
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- [Swift]LeetCode799. 香槟塔 | Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
- [Swift]LeetCode812. 最大三角形面积 | Largest Triangle Area
You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...
- [Swift]LeetCode970.强整数 | Powerful Integers
Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some ...
- linux终端文本编辑神器vi的使用
vi —— 终端中的编辑器 目标 vi 简介 打开和新建文件 三种工作模式 常用命令 分屏命令 常用命令速查图 01. vi 简介 1.1 学习 vi 的目的 在工作中,要对 服务器 上的文件进行 简 ...