MSMQ 概述

1) MSMQ概述

MSMQ 表示微软消息队列服务。MSMQ 可以工作在在线或者离线场景,并提供异步编程功能。如果客户端离线,MSMQ将会是最合适的方法,这是因为服务端不需要等待客户端读取数据并向服务端返回确认。

(2) 确定MSMQ 是否已经安装

通过在运行窗口执行"Services",然后找到Message Queuing. 如果没有就说明MSMQ 没有安装。

(3) MSMQ 安装

控制面板 -> 添加/删除Windows 组件 -- 选择消息队列 - 下一步

这将在你的系统中安装MSMQ,然后你可以通计算机管理来进行确认。

控制面板 -> 管理工具 -> 计算机管理 -> 服务和应用 -> 消息队列,

你将看到出栈队列,私有队列,系统队列,触发器。

(4) 消息类型

MSMQ 支持两种类型的消息: XML 和二进制, 下面的例子分别使用XML的二进制消息。

(5) MSMQ 架构(命名空间集成关系)

System
Messaging
Message
MessageQueue
MessageEnumerator
MessageType
MessagePriority
...

MSMQ 示例程序

示例 1 (使用 XmlMessageFormatter)

static void Main(string[] args)
{
MessageQueue messageQueue = null;
if (MessageQueue.Exists(@".\Private$\MyQueues"))
{
messageQueue = new MessageQueue(@".\Private$\MyQueues");
messageQueue.Label = "Testing Queue";
}
else
{
messageQueue = MessageQueue.Create(@".\Private$\MyQueues");
messageQueue.Label = "Newly Created Queue";
}
messageQueue.Send("First ever Message is sent to MSMQ", "Title");

messageQueue.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
//iterating the queue contents
foreach (Message msg in messageQueue)
{
string readMessage = msg.Body.ToString();
Console.WriteLine(readMessage);
//process message
}
//after all processing delete the messages
messageQueue.Purge();
Console.ReadKey();
}

示例 2 (使用 BinaryMessageFormatter)

class Program
{
static void Main(string[] args)
{
CreateQueue(@".\Private$\ImageQueue");
SendMessage();
ReceiveMessage();
Console.ReadKey();
}

public static void CreateQueue(string queuePath)
{
try
{
if (!MessageQueue.Exists(queuePath))
{
MessageQueue.Create(queuePath);
}
else
{
Console.WriteLine(queuePath + " already exists.");
}
}
catch(MessageQueueException e)
{
Console.WriteLine(e.Message);
}
}

//Send an image to a queue, using the BinaryMessageFormatter.
public static void SendMessage()
{
try
{
//Create new bitmap.
//File must be in \bin\debug or \bin\release folder
//Or a full path to its location should be given

MessageQueue myQueue = new MessageQueue(@".\Private$\ImageQueue");
Image myImage = Bitmap.FromFile("MyImage.jpg");
Message msg = new Message(myImage, new BinaryMessageFormatter());
myQueue.Send(msg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

//Receive a message that contains an image.
public static void ReceiveMessage()
{
try
{
MessageQueue myQueue = new MessageQueue(@".\Private$\ImageQueue");
myQueue.Formatter = new BinaryMessageFormatter();
Message myMessage = myQueue.Receive();
Bitmap myImage = (Bitmap)myMessage.Body;
myImage.Save("NewImage.jpg", ImageFormat.Jpeg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}

在这个例子中我们将一个JPG图片文件存储到MSMQ队列,它可以在接下来的步骤中被接收然后使用。

运行这个程序并确认是否"NewImage.Jpg" 文件在Debug或者Release 文件夹中被创建。

消息由一个主体(body)和若干属性构成。消息的主体可以由文本、二进制构成,根据需要还可以被加密。在MSMQ 中消息的大小不能够超过4MB

消息类型是一个对象
myMessage.Body = book;
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.App.Book) });

接收消息为对象的数据
Book book = (Book)myMessage.Body; //获取消息的内容
优先级
message.Priority = MessagePriority.Highest; //最高消息优先级
------------------------
message.Label = tbName.Text;
message.Body = tbContext.Text;

if (cbPriority.Checked)
{
message.Priority = MessagePriority.Highest;
}
else
{
message.Priority = MessagePriority.Normal;
}
myQueue.Send(message);
MessageBox.Show("成功发送消息到队列");
消息3个维度 Label Body Priority

MSMQ 概述的更多相关文章

  1. MSMQ菜鸟教程

    一  .MSMQ概述 MSMQ全称MicroSoft Message Queue,微软消息队列,是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于 ...

  2. MSMQ学习笔记一——概述

    一.MSMQ是什么 Message Queuing(MSMQ) 是微软开发的消息中间件,可应用于程序内部或程序之间的异步通信.主要的机制是:消息的发送者把自己想要发送的信息放入一个容器中(我们称之为M ...

  3. WCF学习之旅—WCF概述(四)

    一.WCF概述 1) 什么是WCF? Windows Communication Foundation (WCF) 是用于构建面向服务的应用程序的框架.借助 WCF,可以将数据作为异步消息从一个服务终 ...

  4. 【WCF--初入江湖】01 WCF编程概述

    01 WCF编程概述 SOA的优点 1.服务独立于平台和工作环境.服务并不关心自己所处的环境,也不关心与之进行通信的服务所处的    环境. 2.服务相互隔离. 3.服务对协议.格式和传输中立. 4. ...

  5. WCF从零学习之WCF概述(一)

    WCF从零学习之WCF概述(一) 一.WCF概述 我先了解了分布式应用程序开发,所谓分布式应用程序是指应用程序分布在不同计算机上,通过网络来共同完成一项任务.通常为服务器/客户端模式. 在WCF发布之 ...

  6. 【AR实验室】ARToolKit之概述篇

    0x00 - 前言 我从去年就开始对AR(Augmented Reality)技术比较关注,但是去年AR行业一直处于偶尔发声的状态,丝毫没有其"异姓同名"的兄弟VR(Virtual ...

  7. Recurrent Neural Network系列1--RNN(循环神经网络)概述

    作者:zhbzz2007 出处:http://www.cnblogs.com/zhbzz2007 欢迎转载,也请保留这段声明.谢谢! 本文翻译自 RECURRENT NEURAL NETWORKS T ...

  8. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  9. .Net 大型分布式基础服务架构横向演变概述

    一. 业务背景 构建具备高可用,高扩展性,高性能,能承载高并发,大流量的分布式电子商务平台,支持用户,订单,采购,物流,配送,财务等多个项目的协作,便于后续运营报表,分析,便于运维及监控. 二. 基础 ...

随机推荐

  1. [CodeForce 801A] Vicious Keyboard

    题目链接:http://codeforces.com/problemset/problem/801/A 思路:题目中字符串的长度最长100个字符,所以,可以考虑用暴力,先遍历一遍匹配"VK& ...

  2. (转载)MySQl数据库-批量添加数据的两种方法

    方法一:使用excel表格 方法二:使用insert语句(FileWriter批量写入) 使用excel表格 1.打开数据表,按照表的字段在excel中添加数据.注意:表中字段名必须和excel中的名 ...

  3. 剥开比原看代码09:通过dashboard创建密钥时,前端的数据是如何传到后端的?

    作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...

  4. 【C#】非常重要的泛型

    泛型 为什么要有泛型, 在没有泛型之前, 什么东西充当了泛型的作用? 在泛型出现之前, 代码中会有很多需要强制转换的地方. 比如 int a = (int) object, 对于这样类似的代码, 编译 ...

  5. HDU 5988 Coding Contest(浮点数费用流)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5988 题意:在acm比赛的时候有多个桌子,桌子与桌子之间都有线路相连,每个桌子上会有一些人和一些食物 ...

  6. 【Mac】Finder显示或隐藏文件

    第一步:打开「终端」应用程序. 第二步:输入如下命令: defaults write com.apple.finder AppleShowAllFiles -boolean true ; killal ...

  7. 一行css解决图片统一大小后的拉伸问题(被冷漠的object-fit)

    一.先来个实战 1. 测试案例 需求: 要求表情库里所有表情包大小都固定 实际效果: 由于图片原始大小都不一样,强行设定大小值会导致拉伸,如果不设定大小则参差不齐.例如: //html <bod ...

  8. SNMOJ 31

    考虑将给入的$A$数组差分,得到差分数组$C$ 每一次操作相当于把差分数组的每一位${+1}$,其中一个位置上${-n+1}$. 我们可以直接算出要进行多少次操作:${T=\frac{\sum A[i ...

  9. Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树+树链剖分+线段树

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  10. 实现一个键对应多个值的字典(multidict)

    一个字典就是一个键对应一个单值的映射.如果你想要一个键映射多个值,那么你就需要将这多个值放到另外的容器中, 比如列表或者集合里面.比如,你可以像下面这样构造这样的字典: d = { , , ], , ...