
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
|
1093
20.4 Using Message Queues on a Local Workstation
Problem
You need a way to disconnect two components of your application (like a web ser-
vice endpoint and processing logic) so that the first component has to worry about
only formatting the instructions and the bulk of the processing occurs in the second
component.
Solution
Use the MQWorker class shown here in both the first and second components to write
and read messages to and from a message queue.
MQWorker uses the local message-
queuing services to do this. The queue pathname is supplied in the constructor, and
the existence of the queue is checked in the
SetUpQueue method.
class MQWorker
{
private string _mqPathName;
MessageQueue _queue = null;
public MQWorker(string queuePathName)
{
if (string.IsNullOrEmpty(queuePathName)
throw new ArgumentNullException("queuePathName");
_mqPathName = queuePathName;
SetUpQueue( );
}
SetUpQueue creates a message queue of the supplied name using the MessageQueue
class if none exists. It accounts for the scenario in which the message-queuing ser-
vices are running on a workstation computer. In that situation, it makes the queue
private, as that is the only type of queue allowed on a workstation.
private void SetUpQueue( )
{
// See if the queue exists, ...