65.9K
CodeProject 正在变化。 阅读更多。
Home

Microsoft Message Queuing (MSMQ)

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (2投票s)

2007 年 11 月 28 日

viewsIcon

32846

downloadIcon

557

Microsoft 消息队列是 Microsoft 用于异步消息传递的技术。每当应用程序需要相互发送消息时,都可以使用 MSMQ。MSMQ 可以在远程机器之间进行通信,甚至可以通过 http/https 在互联网上进行通信。

引言

本文介绍如何设置和检索 Microsoft 消息队列中的对象。

背景

这是一个控制台应用程序,用于读取 XML 文件并将其反序列化为对象。该对象定义在项目中的 xmlMappingClass.cs 文件中。最后,该对象被发送到 MMQ,稍后可以从中获取。

使用代码

我在这里定义了三个类,足以发送和检索对象。

1.run.cs: 这个类是该应用程序的起点,它创建一个队列,然后插入 XML 文件中的对象(保存在 data 文件夹中)。在这里,我们还可以找到从队列中检索对象并将其打印到控制台的代码。

string queuePath = ".\\PRIVATE$\\Questions";
//Sending Xml object to MSQ 
MSQUtility.SendXmlObjectToQueue(Environment.CurrentDirectory+"\\Data\\Questions.xml", queuePath);
//Reading Xml Object from MSQ
string sXML = MSQUtility.ReadXmlObjectFromQueue(queuePath);
Console.Write(sXML);
//Delete all Queue
MSQUtility.ClearQueue(queuePath);

2.MSQUtility.cs: 在这个类中,已经编写了所有用于读取和发送对象到队列的函数。

public static void SendXmlObjectToQueue(string xmlFilePath, string queuePath)
{ 
EnsureQueueExists(queuePath);
MessageQueue queue = new MessageQueue(queuePath);
object GetallQuestionsFromXmlFile = DeserializeObject(xmlFilePath, (typeof(Questions)));

//Collection of all questions
Questions allQuestions = (Questions)GetallQuestionsFromXmlFile;
queue.Send(allQuestions);
}

// Creates the queue if it does not already exist.
public static void EnsureQueueExists(string path)
{
if (!MessageQueue.Exists(path))
{
MessageQueue.Create(path);
}
}

public static string ReadXmlObjectFromQueue(string sQueue)
{
MessageQueue oQueue;
Message oMessage;
try
{
if (!MessageQueue.Exists(sQueue)) { MessageQueue.Create(sQueue); }
oQueue = new MessageQueue(sQueue);
// Utilize our custom message formatter to deserialize the XmlDocument stored
// in the message queue.
oQueue.Formatter = new CustomMessageFormatter();
oMessage = oQueue.Receive(new TimeSpan(0, 0, 5));
string sXML = (string)oMessage.Body;
try { oQueue.Close(); }
catch (Exception) { }
oQueue.Close();
return sXML;
}
catch (Exception e) { Console.WriteLine(e.Message); }
return null;
}

public static void ClearQueue(string sQueue)
{
MessageQueue oQueue;
try
{
if (!MessageQueue.Exists(sQueue)) { MessageQueue.Create(sQueue); }
oQueue = new MessageQueue(sQueue);
oQueue.Purge();
oQueue.Close();
}
catch (Exception e) { Console.WriteLine(e.Message); }
}

public static Object DeserializeObject(string xmlFilePath, Type objectType)
{
XmlSerializer xs = new XmlSerializer(objectType);
FileStream fs = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read);
object obj = xs.Deserialize(fs);
fs.Close();
return obj;
}

3.XmlMappingClass.cs: 这个类是 XML 文件的映射类,它保存着序列化/反序列化的对象。

//This Classs map to this Xml Content.This is required to convert a Xml content into Object which is to be sent to MSQ
//<?xml version="1.0" encoding="utf-8" ?>
//<Questions Type='oops' Language="'c#'">
// <Question Id='1' SubType='Inheritance' ToughLevel='SE' Ques='What is the capital city of Australia?'/>
// <Question Id='1' SubType='Inheritance' ToughLevel='SE' Ques='What is the capital city of Australia?'/>
// <Question Id='1' SubType='Inheritance' ToughLevel='SE' Ques='What is the capital city of Australia?'/>
//</Questions> 

[Serializable()]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public partial class Questions{
public Questions(){
}

private Question[] m_question;
private string m_type;
private string m_language;
[XmlElement("Question", Form = XmlSchemaForm.Unqualified)]
public Question[] Question
{
get { return m_question; }
set { m_question = value; }
}

[XmlAttribute()]
public string Type
{
get { return m_type; }
set { m_type = value; }
}

[XmlAttribute()]
public string Language
{
get { return m_language; }
set { m_language = value; }
}
}

/// <summary>
/// Class for single question
/// </summary>

[Serializable()]
[XmlType(AnonymousType = true)]
public partial class Question
{
public Question()
{

}

private string m_id;
private string m_subType;
private string m_toughLevel;
private string m_ques;
[XmlAttribute()]
public string Id
{
get { return m_id; }
set { m_id = value; }
}

[XmlAttribute()]
public string SubType
{
get { return m_subType; }
set { m_subType = value; }
}

[XmlAttribute()]
public string ToughLevel
{
get { return m_toughLevel; }
set { m_toughLevel = value; }
}

[XmlAttribute()]
public string Ques
{
get { return m_ques; }
set { m_ques = value; }
}
}

也许你喜欢这篇文章...我尝试制作一个简单的控制台应用程序,即使是初学者也能理解。

参考文献

http://www.eggheadcafe.com/articles/20021221.asp
http://msdn2.microsoft.com/en-us/library/4dhdwx8w(VS.71).aspx
http://msdn2.microsoft.com/EN-US/library/wss66xs0(VS.71).aspx
http://www.developerfusion.co.uk/show/2131/2/
© . All rights reserved.