| Message Queue functionality has been around for quite awhile and is a highly scalable way to managing queued data requests between disconnected applications. There are a variety of uses for Message Queuing. Today, I'll provide you with the nuts and bolds of managing messages to and from the queue and leave the complexities of a custom app to you. With Microsoft .NET, we can serialize a struct and store it's values inside each message. When the message is sent via the .Send() method, the struct is automatically serialized to XML. When we retrieve the message, we use the XmlMessageFormatter as part of the process of deserializing the message back to the original struct with it's current values in tact. |
| |
|
|
| |
| All in all, it is a fairly simple concept that delivers powerful capabilities to our applications. That being said, if your application requirements need to utilize a queuing mechanism, Microsoft .NET and Message Queuing is an ideal solution. It can work exceptionally well if you need to read/write data to files from many users at the same time with file types that are set up for such an endeavor. |
| To give you just one example of how Message Queuing might be used is a common situation that comes up in small call centers. A client provides you with an Excel spreadsheet of customer data. By the end of the day, they expect to have their data accessible in read/write mode by over 200 sales agents via a web browser script. Of course, they'll want the updated customer and call result data in the same spreadsheet and sent back to them on an hourly basis for distribution to their local sales force. Those of you in the call center business can relate to the technical limitations/desires of most clients. |
| Typically, you'd want to extract the data from Excel and move it back and forth from your SQL Server database. However, you just don't have time to put all the necessary applications together much less a queuing mechanism to make sure each agent gets a unique record. With message queuing, you can create a quick an dirty application to reload the queue at regular intervals. That same application can process finalized records in a second queue back to the Excel spreadsheet prior to reloading the queue with records that still need to be called. This ensures that you've only got one application attempting to access the spreadsheet at any one given time. Now, Excel offers options for multi-user support but your app may have requirements that might not work well in that environment (I recently had just such a case involving MathCad and Excel). Of course, you can also read/write messages to the queue from ASP.NET allowing you to have browser based interfaces as well. |
| If you'd like to see this message queue scenario in action, take a moment to read this article: Integrate Message Queue With Excel, XML and ADO.NET In The Enterprise. |
| In order to use a Message Queue, you'll need to make sure you've installed Message Queuing. Here are the instructions: |
| |
| 1. | Open Control Panel. |
| 2. | Select Add/Remove Programs. |
| 3. | Select Add/Remove Windows Components. |
| 4. | Check Message Queuing and proceed with the installation. Of course, if you've already installed other components like IIS, you'll want to leave those checked as well. |
| |
| Here's a screen shot of the Microsoft Management Console if the Message Queue Services are running properly. |
 |
| |
| Simply copy/paste the code below into a C# Console Application and run it. You can also watch the message queue in action by opening up the Microsoft Management Console under Services & Applications. And of course, remember to include the System.Messaging class to your project references. |
| Please take a moment to rate this article. Rate Article (opens in separate window). |
| |
| C# Console Application Source Code |
using System;
using System.Messaging;
using System.Runtime.Serialization;
namespace EggHeadCafeQueue
{
public struct Customer
{
public string RecordID;
public string FirstName;
public string LastName;
public string Address1;
public string City;
public string State;
public string Zip;
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
EggHeadCafeQueue.Class1 oSampleApp = new EggHeadCafeQueue.Class1();
oSampleApp.RunTest();
}
public void RunTest()
{
// Create a name for the queue
string sQueue = ".\\PRIVATE$\\EggHeadCafe";
int i=0;
EggHeadCafeQueue.Customer oCustomer;
try
{
for(i=0;i<5;i++)
{
oCustomer = new EggHeadCafeQueue.Customer();
oCustomer.RecordID = i.ToString();
oCustomer.FirstName = "RobDog";
oCustomer.LastName = "SnoopCat";
oCustomer.Address1 = "1234 Sesame Street";
oCustomer.City = "Orlando";
oCustomer.State = "FL";
oCustomer.Zip = "90210";
this.SendRecordToQueue(sQueue,oCustomer);
Console.Write(oCustomer.RecordID + " Sent To Message Queue\n");
}
Console.Write("\n\n");
Message oMessage;
oCustomer = new EggHeadCafeQueue.Customer();
for(i=0;i<5;i++)
{
oMessage = this.GetRecordFromQueue(sQueue);
if (oMessage!=null)
{
oCustomer = (EggHeadCafeQueue.Customer) oMessage.Body;
Console.Write(oCustomer.RecordID + " " + oCustomer.FirstName + " " + oCustomer.LastName+ "\n");
}
}
}
catch (Exception e) { Console.Write(e.Message); }
Console.Write("\n\nHit Enter To Quit: ");
Console.ReadLine();
}
public Message GetRecordFromQueue(string sQueue)
{
Message oMessage;
try
{
if (!MessageQueue.Exists(sQueue)) { MessageQueue.Create(sQueue); }
MessageQueue oQueue = new MessageQueue(sQueue);
oQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(EggHeadCafeQueue.Customer) });
oMessage = oQueue.Receive(new TimeSpan(0,0,5));
return oMessage;
}
catch (Exception e) { Console.Write(e.Message); }
return null;
}
public bool SendRecordToQueue(string sQueue,object oRecord)
{
bool fRet=false;
try
{
if (!MessageQueue.Exists(sQueue)) { MessageQueue.Create(sQueue); }
MessageQueue oQueue = new MessageQueue(sQueue);
oQueue.Send(oRecord);
fRet = true;
}
catch (Exception e) { Console.Write(e.Message); }
return fRet;
}
}
}
|