惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
V
V2EX
雷峰网
雷峰网
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
F
Full Disclosure
Y
Y Combinator Blog
V
V2EX - 技术
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
SecWiki News
SecWiki News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
量子位
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
K
Kaspersky official blog
B
Blog
A
Arctic Wolf
Hacker News: Ask HN
Hacker News: Ask HN
L
LangChain Blog
T
Tor Project blog
P
Privacy & Cybersecurity Law Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
Lohrmann on Cybersecurity
D
Docker
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy International News Feed
Simon Willison's Weblog
Simon Willison's Weblog

博客园 - 东哥技术专栏 - Coldwine's Blog

SQL Server Transactional Replication 中的 CommitBatchSize 和 CommitBatchThreshold 属性 转: SQL Server Analysis Service中Cube的结构 Cannot connect to an Analysis Services named instance after installing SQL Server Express 今天遇到一个问题才知道如果安装了命名实例 SSIS Service 需要手动更改配置文件 Data Mining Tutorial数据挖掘教程 (PART-1) 一些关于“数据挖掘介”技术的有用文档 The Fundamentals of the SQL Server 2005 XML Datatype SSL协议与数字证书原理 (ZT) PKI 技术白皮书 (ZT) Windows 2000 公钥基础结构详解 (ZT) SQL Server 2000 之前的版本号对照表 关于 c# 的 Partial Class 关于ASP.NET CS1595 问题 开发人员的十种必备工具 ASP.NET中的状态管理 ASP.NET中编程杀死进程 关于SqlDataReader一些用法 使用附属程序集的一些经验 使用C#建立WINDOWS服务
. Net环境下消息队列(MSMQ)对象的应用
东哥技术专栏 - Coldwine's Blog · 2005-07-31 · via 博客园 - 东哥技术专栏 - Coldwine's Blog

关于消息对象(MSMQ)的一些基本概念可以从《消息队列(Message Queue)简介及其使用》查阅,这里归纳在.Net 环境下应用消息队列(MSMQ)开发的一些基本对象和方法。

队列类型及其相应的路径格式:

Public:  [MachineName]\[QueueName]

Private:  [MachineName]\Private$\[QueueName]

Journal:  [MachineName]\[QueueName]\Journal$

Machine journal:  [MachineName]\Journal$

Machine dead-letter:  [MachineName]\DeadLetter$

Machine transactional dead-letter:  [MachineName]\XactDeadLetter$

The first portion of the path indicates a computer or domain name or uses a period (.) to indicate the current computer.

1. 创建消息队列

可以手动的方式通过Windows提供的工具创建,或者通过程序的方式创建:

if(MessageQueue.Exists(".\\Private$\\MSMQDemo"))

            queue = new MessageQueue(".\\Private$\\MSMQDemo");

else

            queue = MessageQueue.Create(".\\Private$\\MSMQDemo");

2. 发送消息

缺省情况下,消息序列化XML格式,也可设置为MessageQueue对象的Formatter属性为BinaryMessageFormatter,以二进制格式序列化。

设置消息序列化格式:

if(rdoXMLFormatter.Checked)

            queue.Formatter = new XmlMessageFormatter();

else

            queue.Formatter = new BinaryMessageFormatter();

发送简单的文本消息:

string strMessage = "Hello, I am Rickie.";

queue.Send(strMessage, "Simple text message");

消息队列可以传送简单的文本消息,也可以传送对象消息,但需要满足如下条件:

1class必须有一个无参数的公共构造函数,.Net使用这个构造函数在接收端重建对象。

2class必须标示为serializable(序列化)。

3)所有的class属性必须可读写,因为.Net在重建对象时不能够恢复只读属性的属性值,因此只读属性不能够序列化。

发送对象消息(CustomerInfo class需要满足上述条件):

CustomerInfo theCustomer = new CustomerInfo("0001", "Rickie Lee", "Rickieleemail@yahoo.com");

queue.Send(theCustomer, "Object message");

3. /显示消息

当消息接受后,消息将从队列中删除。可以通过使用MessageQueue.Peek方法来检索消息队列中的第一个消息的复制,保留消息在队列中。不过,这样只能获取的相同的消息。更好的办法是通过foreach来读消息队列中的消息,但不删除队列中的消息。

foreach(System.Messaging.Message message in queue)

{

          txtResults.Text += message.Label + Environment.NewLine;

}

4. 接收消息

一般而言,可以通过Receive方法来读取队列中的消息,对于非事务性的队列,优先读取高优先级的消息。如果队列中有多个相同优先级的消息,则以先进先去的方式进行读取消息。对于事务性的队列,则完全以先进先去的方式进行读取消息,忽略消息的优先级。

System.Messaging.Message receivedMessage;

receivedMessage = queue.Receive(TimeSpan.FromSeconds(5));

上面采用同步调用,并且一直等到队列中有可用消息或超时过期。

Demo界面(不在提供DEMO程序)

其他相关事项:

  • 关于消息的加密、路由等等特性,需要有配置Active Directory的消息队列服务器。
  • 为了避免存放消息队列的计算机重新启动而丢失消息,可以通过设置消息对象的Recoverable属性为true,在消息传递过程中将消息保存到磁盘上来保证消息的传递,默认为false
  • 消息发送方和消息接收方需采用相同的序列化格式,如XMLBinary
  • 建议每一个消息队列存放相同类型的消息对象,这样可以省掉获取消息对象后,进行类型判别的麻烦。 

5.消息队列在分布式系统中的应用

消息队列MSMQ和数据库不一样,消息队列缺乏足够的错误检查能力,并且MSMQ由于需要束缚在windows平台,这些是MSMQ的不足之处。另外Production环境中,需要编写大量的代码来进行错误检测和响应。还有大量的死信队列、响应队列和日记队列可能部分在企业不同的计算机上,使得跟踪这些问题或进行诊断变得比较困难。

但是,MSMQ作为组件内部连接比较有用。例如,你可以创建一个XML Web Services使用MSMQ来转发对另一个Server端组件的请求,这种设计巧妙回避了其他异步调用的方法,并且确保可扩展性和性能。

References:

1, Matthew MacDonald, Microsoft® .NET Distributed Applications: Integrating XML Web Services and .NET Remoting

2, Rickie, 消息队列(Message Queue)简介及其使用