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. Hadoop之mapreduce

    doc Hadoop初探之Stream Hadoop Stream 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试 用python + ...

  2. Python 编码规范 PEP8

    1 Introduction Guido 的核心思想是:对于代码而言,相比于写,它更多是被用来读的.这个指导旨在使Python代码更易读,且具有更强的协调性. 2 A Foolish Consiste ...

  3. PTA 根据后序和中序遍历输出先序遍历(25 分)

    7-1 根据后序和中序遍历输出先序遍历(25 分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果. 输入格式: 第一行给出正整数N(≤30),是树中结点的个数.随后两行 ...

  4. BZOJ 3673: 可持久化并查集(可持久化并查集+启发式合并)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3673 题意: 思路: 可持久化数组可以用可持久化线段树来实现,并查集的查询操作和原来的一般并查集操作 ...

  5. js实现可视化区域内拖拽

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  6. 【Mysql】key 、primary key 、unique key 与index区别

    参考:https://blog.csdn.net/nanamasuda/article/details/52543177 总的来说,primary key .unique key 这些key建立的同时 ...

  7. Educational Codeforces Round 23 E. Choosing The Commander trie数

    E. Choosing The Commander time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  8. 学习笔记57—归一化 (Normalization)、标准化 (Standardization)和中心化/零均值化 (Zero-centered)

    1 概念   归一化:1)把数据变成(0,1)或者(1,1)之间的小数.主要是为了数据处理方便提出来的,把数据映射到0-1范围之内处理,更加便捷快速.2)把有量纲表达式变成无量纲表达式,便于不同单位或 ...

  9. 关于JS历史

      js由来        95年那时,绝大多数因特网用户都使用速度仅为28.8kbit/s 的“猫”(调制解调器)上网,但网页的大小和复杂性却不断增加.为完成简单的表单验证而频繁地与服务器交换数据只 ...

  10. 2d游戏和 3d游戏的区别

    2D游戏和3D游戏的主要区别 一.总结 一句话总结:2D中的单位就是贴图,3D中的单位还有高 1. 3D 和 2D 游戏的区别主要体现在呈现画面和文件体积上: 2. 借助 3D 引擎可以提升 2D 游 ...