My technical journal.

Maybe useful, maybe not.
posts - 105 , comments - 45 , trackbacks - 0

Checking if MSMQ queues exist is hard work so should you bother?

Frank Boyne, 5 star MVP, posted a great response to a question on the MSMQ newsgroups which I felt was worth reiterating.

Basically someone wanted to check if a private queue on a remote machine existed before they sent a message to it. The problem was that they couldn't call the MessageQueue.Exists() method without getting InvalidOperationException.

As Frank explained, this method call isn't available for remote computers, as documented in MSDN.




The path syntax is not valid.


An error occurred when accessing a Message Queuing method.


The Exists(String) method is being called on a remote private queue


The application used format name syntax when verifying queue existence.

As you can see, an exception will be thrown for checking remote private queues.

The interesting part of Frank's response is what to do about it. Basically, checking that a remote queue exists before you send is no guarantee that the destination will still be there when the message arrives. Or, vice versa, someone could create the queue just after you've found it didn't exist.

Frank's alternative approach is to make use of other features that MSMQ provides, such as negative acknowledgements messages with administration queues.

What should happen is that either:

  • the message will be delivered successfully to the destination queue
  • a negative acknowledgement (NACK) will be returned to the administration queue with a class of  "The destination queue does not exist." (MQMSG_CLASS_NACK_BAD_DST_Q)

Alternatively you could use negative source journaling and, on failure to deliver, should see the same class of NACK in the corresponding "Dead-letter messages" system queue.

In summary, don't check if the queue exists but instead handle the non-delivery of the message should it turn out that the queue doesn't exist.

Print | posted on Monday, October 18, 2010 1:34 PM |



# re: Checking if MSMQ queues exist is hard work so should you bother?

It doesn't work on my pc.
I cann't receive Acknowledge message from administration queue when target queue doesn't exist on remote machine.
My pc is windowsXP.Remote machine is windows server2008.
Source is C#/.net2.0

Can you give me some advice?

wait for your mail.

MessageQueue mq = new MessageQueue(@".\private$\test");
mq.MessageReadPropertyFilter.Acknowledgment = true;
mq.MessageReadPropertyFilter.CorrelationId = true;

MessageQueue q = new MessageQueue(string.Format(@"formatname:direct=tcp:{0}\private$\test1", ""), QueueAccessMode.SendAndReceive);
Message m = new Message();
m.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
m.Body = "hello world";
m.AdministrationQueue = mq;
m.AcknowledgeType = AcknowledgeTypes.FullReachQueue| AcknowledgeTypes.FullReceive;
m.UseDeadLetterQueue = true;

Message ack=mq.Receive();
11/26/2010 11:59 AM | bianxuefeng
Post A Comment

Powered by: