
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Using Message Queues on a Local Workstation
|
1095
to the BinaryMessageFormatter for the Message, since that is how we write to the
queue. Finally, the body of the message is returned from the method.
public string ReadMessage( )
{
Message msg = null;
msg = _queue.Receive( );
msg.Formatter = new BinaryMessageFormatter( );
return (string)msg.Body;
}
}
To show how the MQWorker class is used, the following example creates an MQWorker.
It then sends a message (a small blob of XML) using
SendMessage, then retrieves it
using
ReadMessage:
// NOTE: Message Queue services must be set up for this to work.
// This can be added in Add/Remove Windows Components.
// This is the right syntax for workstation queues.
//MQWorker mqw = new MQWorker(@".\private$\MQWorkerQ");
MQWorker mqw = new MQWorker(@".\MQWorkerQ");
string xml = "<MyXml><InnerXml location=\"inside\"/></MyXml>";
Console.WriteLine("Sending message to message queue: " + xml);
mqw.SendMessage("Message Label",xml);
// This could also be in a separate component.
string retXml = mqw.ReadMessage( );
Console.WriteLine("Read message from message queue: " + retXml);
Discussion
Message queues are very useful when you are attempting to distribute the processing
load for scalability purposes. Without question, using a message queue adds over-
head to the