Click here to Skip to main content
15,886,110 members
Articles / Mobile Apps
Article

Microsoft Message Queuing (MSMQ)

Rate me:
Please Sign up or sign in to vote.
2.00/5 (2 votes)
28 Nov 2007 32.1K   535   19   1
Microsoft Message Queuing is Microsoft technology for asynchronous messaging. Whenever there is need for applications to send messages to each other, MSMQ can be used. MSMQ can communicate between remote machines, even over internet using http/https.

Introduction

This article is for setting and retrieving object from Microsoft Messaging Queue.

Background

This is console application which is used to read a xml file and deserialized it into an object .This is defined in xmlMappingClass.cs file in this project.Finally ,this object is then sent to MMQ which can be fetched later.

Using the code

I have defined three classes here which are sufficient to send and retrieve an object.

1.run.cs: This class is starting point of this application which create a queue and then insert object of xml file(kept in data Folder).Here,we can also find the code to retrive an object from queue and is then printed on the console.

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: In this class,all the functions for reading and sending an object into queue have been written.

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: This class is mapping class for the Xml file which holds serialized/Deserialized object.

//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; }
}
}

Probably,you may have liked this article...i have tried to make such a simple console application which can be understood even by a beginnner.

References:

http://www.eggheadcafe.com/articles/20021221.asp
<a href="http://msdn2.microsoft.com/en-us/library/4dhdwx8w(VS.71).aspx">http://msdn2.microsoft.com/en-us/library/4dhdwx8w(VS.71).aspx</a>
<a href="http://msdn2.microsoft.com/EN-US/library/wss66xs0(VS.71).aspx">http://msdn2.microsoft.com/EN-US/library/wss66xs0(VS.71).aspx</a>
<a href="http://www.developerfusion.co.uk/show/2131/2/">http://www.developerfusion.co.uk/show/2131/2/</a>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader Vertex Software
India India
5+years,ASP.NET(1.1 and 2.0,3.5),c#.net,SharePoint, Webservices,XML, SQL Server 2000, VS 2008/2005/2003, Remoting, AD, Nunit, NHibernate,Design Patterns,Microsoft Ajax,Agile Technology,LINQ,WCF,WF,REST Service,Microsoft MVC framework

Comments and Discussions

 
GeneralGood one Pin
Natarajanus12-Dec-13 9:22
Natarajanus12-Dec-13 9:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.