Get the Message - MSMQ

Response Queues

Frequently, you would like the sending application to receive a message back from the processing application. This is accomplished by setting up a response queue. This causes the roles of the sending and processing application to be reversed for the return message. In other words, the processing application will post a message, typically containing the result or status of the original request into a message queue. The original sending application will then receive the message and act accordingly.

The creation of a response queue is accomplished through the use of the ResponseQueueInto property of the MSMQMessage object. The code below illustrates how to set the queue up to be used.

Private Sub PostMessage()

Dim QI as New MSMQQueueInfo
Dim RequestQ As MSMQQueue
Dim ResponseQ As New MSMQQueue
Dim msg As New MSMQMessage

QI.PathName = "MyServer\QueueName"

msg.Label = "Sales Order 1"
msg.Body = "Customer=Bruce;Product=Pokemon;" & _
ResponseQ.PathName = ".\ResponseQueue"

Set msg.ResponseQueueInfo = ResponseQ

msg.Send RequestQ


End Sub

On the back end, the receiving application must act appropriately and return a message to the sending process. As you can see, the ResponseQueueInfo property contains a reference to a valid queue. It is this queue that gets opened and posted in the same manner as any other queue. An example appears below.

Private Sub ReceiveMessage()

Dim QI as New MSMQQueueInfo
Dim ReceiveQ As MSMQQueue
Dim ResponseQ As MSMQQueue
Dim msg As MSMQMessage
Dim RespMsg As New MSMQMessage

QI.PathName = "MyServer\QueueName"

Set msg = ReceiveQ.Receive(ReceiveTimeout:=1000)
If msg is Nothing Then
MsgBox "No message was received"
End If

If Not msg.ResponseQueueInfo is Nothing Then
Set ResponseQ = msg.ResponseQueueInfo.Open( _
RespMsg.Label = "Response"
RespMsg.Body = "Response Body"
RespMsg.CorrelationId = msg.Id

RespMsg.Send ResponseQ
End If


End Sub

As always, an article of this length cannot cover much more that the concepts and basic functionality behind a topic as complicated as message queuing. If you're interested in learning more about this topic, might we suggest a book that is as close to a bible in this area as you could find. It is "Professional Windows DNA: Building Distributed Web Applications with VB, COM+, MSMQ, SOAP, and ASP".

You might also like...


About the author

Bruce Johnson Canada

I am the owner of a small application development consulting company that specialized in the design and implementation of Internet-based applications. While there are others who can make a web ...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“XML is like violence - if it's not working for you, you're not using enough of it.”