• RV
    • Missy – My Home
    • JBAM – Newmar Dutch Star
    • Shaneeda
    • Buying a Used RV
    • Gas vs Diesel RVs
    • Ideal Motorhome
    • Choosing the Ideal RV
  • Cycling
  • Jeep
  • About
  • Flying
    • Vans RV-4
    • Daisy – RV-3B
  • Resources
  • Subscribe

JdFinley.com

Fulltime RV Living Adventures

  • Is Solar For You?
  • Gas vs Diesel RVs
  • Buying a Used RV
  • Choosing the Ideal RV
  • Ideal Motorhome

MSMQ – Receive Message

July 28, 2011

The introduction to this serious of Microsoft Message Queuing (MSMQ) posts is here. This time around, I’d like to show you how to read (or receive, dequeue, or pick) a message from a local queue.

By default, the first message read from the queue is the message with the highest priority followed by the time received (first in, first out – FIFO).  You have the ability to override this behavior when required.

The following is a simple form of reading a message from a queue.

Dim _message As System.Messaging.Message
Dim _queue As MessageQueue
Dim _messageBodyContents As String

Try

_queue = New MessageQueue("QueueName")

Dim _messageFormatter As XmlMessageFormatter = CType(mQueue.Formatter, XmlMessageFormatter)

_messageFormatter.TargetTypeNames = New String() {"System.String,mscorlib"}

_message = _queue.Peek(New TimeSpan(10000))
_messageBodyContents = CType(_message.Body, String)

' Perform business logic
_queue.Receive()
_message = _queue.ReceiveById(_message.Id)

Catch ex As MessageQueueException
If ex.MessageQueueErrorCode = MessageQueueErrorCode.IOTimeout Then
' Queue is empty
Else
' Handle the exception
End If

End Try

MSMQ Queue

You must create an instance of an MSMQ MessageQueue to begin. In the above example, note that I first Peek the message.  This allows me to get a copy of the message, perform the required business logic, and only then receive the message (ReceiveById).  This allows be to be sure the necessary business logic has completed successfully before I delete the message from the queue (which is what receiving the message does).  If the message or associated business logic is not critical, you can skip the Peek/ReceiveById and simply use the MessageQueue.Receive method.

The Peek and Receive methods are blocking calls. This may be acceptable for your needs.  I prefer to use the optional parameter and set a timeout on this block.  This thread is then free to perform other actions (and shut down if told to do so).

Queue Names

The name of the message queues can be a difficult concept to pick up.  Things are pretty simple when reading local queues. It get’s a bit more complicated with remote queues (that is, queues that physically exist on a different machine).  A local private queue name follows this format:
“.\private$\TestQueue”

A more verbose option is also fine:
“FormatName:DIRECT=OS:[ComputerName]\private$\TestQueue”

As is a TCP option which is really unnecessary when working locally but it will work just the same:
“FormatName:DIRECT=TCP:10.200.1.1\private$\TestQueue”

Queue Security

An item that frequently trips people up is forgetting to set security on an MSMQ message queue. By default, anyone can use MSMQ to send a message to a queue but NOT read from it.  You can tailor the settings to meet your security requirements using the same Computer Management plug-in as is used to manage queues (or it can be done programatically).  For a development sandbox, giving the Everyone group Full Control will get you going.

For more message queuing implementation details try these additional posts:

  • Message Queuing
  • Receiving Remote MSMQ Messages
(Visited 838 times, 1 visits today)

Related posts:

Default ThumbnailMessage Queuing Default ThumbnailXML Serialization Default ThumbnailIsolated Storage Default ThumbnailObject Serialization (binary)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Twenty years from now you will be more disappointed by the things you didn’t do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover.

— Mark Twain

Popular Posts

  • Powermatic Model 90 Wood Lathe & VFD
  • Wood Turning on a Lathe
  • Chainsaw Review: Sportsman 20 in. 52cc
  • Cielo Grande Barbed Wire Fence Repair

Email Notification


 

Posts by Subject

3DPrinting Aircraft Airstrip Bible Bus bus boys Camping Cat Computers Cycling Development Electrical electronics Entertainment Environment Factory Five family FAQ Finances flying Food God Health Holiday Home Humor kayak Life Maintenance Nature Photography Review RV-3 RV-4 sailboat sailing self improvement Sewing solar Travel Video weather wood woodworking X-Country

Posts by Month

© Copyright © 2025 JDFinley.com · All Rights Reserved · Privacy Policy

Unauthorized use and/or duplication of this material without express and written permission from this site’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to JD Finley and JdFinley.com with appropriate and specific direction to the original content.