Monitoring a Messaging Queue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to use the Microsoft Messaging Queue for an application I am
writing. I am able to create, push and pull messages with this app. The
problem right now though is: my "pull / pop" of the messages is based upon a
click of a button by the user. What I would like to do is automate the
process in which when an item is entered in the queue: a process is called /
spawned to will handle the item. Or perhaps every couple seconds: check the
queue for items (get the count) and perform a loop until that count is 0.

I am unsure how to go about "automating" the pop / pull of hte messages.

Would anyone be able to give me some pointers or advice?

Thanks
Andy
 
You never really automate the posting of a message, an action such as clicking the button or uploading a file nearly always fires the event / calls the code. Reading the queue, we've set up a windows service that creates an object containing the following code (note: I've removed / change certain bits have been removed to make it generic - but it should give you a good starting point). As the windows service starts ProcessMessageQueue on a separate thread, it carries on processing the queue until the flag is set to false (normally on shutting down the service).

public override void ProcessMessageQueue()
{
MyObject emptyObj = new MyObject()
while (flag) // Property of class, set to false by calling object to terminate processing queue
{
try
{
MyObject myObject;
myObject = (MyObject)GetMessage(emptyObj.GetType());
if (myObject != null)
ProcessMessage(myObject);
}
catch(Exception e)
{
LogError(e);
}
}
}

private object GetMessage(Type objectType)
{
MessageQueue msmqQueue;
if( MessageQueue.Exists( msgQueue))
msmqQueue = new MessageQueue( msgQueue);
else
msmqQueue = MessageQueue.Create( msgQueue);

msmqQueue.UseJournalQueue = true;

Type[] targetTypes = new Type[1];
targetTypes[0] = objectType;
msmqQueue.Formatter = new XmlMessageFormatter( targetTypes);

Message message;
try
{
message = msmqQueue.Receive(new TimeSpan(0,0,3));
return (object)message.Body;
}
catch (MessageQueueException e)
{
if (e.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
{
return null;
}
else
throw e;
}
}
 
Back
Top