Sending Messages that Request a Response

By sending the MSMQQueueInfo object of a second queue along with a message, the sending application indicates that it expects a response message from the receiving application. The MSMQQueueInfo object is passed in the ResponseQueueInfo property of the message.

When the receiving application reads the message and sees that a response is requested (ResponseQueueInfo is not NULL), it should then return a response message to the queue specified by ResponseQueueInfo. For more information on response queues, see Response Queues.

To request a response
  1. Determine what type of response message is needed. For example, the response message could be a message that only indicates a message arrived, or it could include a message body with complete instructions on what to do. Both the sending and receiving application must be able to understand the message.

  2. Locate the response queue, creating one if it doesn't exist.
    Set qinfos = query.LookupQueue(Label:="Response Queue")
    qinfos.Reset
    Set qinfoResp = qinfos.Next
    If qinfoResp Is Nothing Then
       Set qinfoResp = New MSMQQueueInfo
       qinfoResp.PathName = ".\RespQueue"
       qinfoResp.Label = "Response Queue"
       qinfoResp.Create
    End If
     
  3. Locate the destination queue, creating one if it doesn't exist.
    Set qinfos = query.LookupQueue(Label:="Destination Queue")
    qinfos.Reset
    Set qinfoDest = qinfos.Next
    If qinfoDest Is Nothing Then
       Set qinfoDest = New MSMQQueueInfo
       qinfoDest.PathName = ".\DestQueue"
       qinfoDest.Label = "Destination Queue"
       qinfoDest.Create
    End If
     
  4. Open the destination queue and send the message. The example below asks if a response message is wanted.
    Set q = qinfoDest.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
    
    msgSent.Label = "Test Message"
    msgSent.Body = "This message tests the response queue."
    
    response = MsgBox("Do you want a response message?", 4)
    If response = 6 Then
       Set msgSent.ResponseQueueInfo = qinfoResp
    End If
    
    msgSent.Send q
     
  5. Read the message in the destination queue, and return a response message if one is requested. In the example below, the original message's identifier is included in the response message's correlation identifier. This provides a way to match the response message with the original message read from the queue.
    Set q = qinfoDest.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
    Set msgRead = q.Receive
    
    If Not msgRead.ResponseQueueInfo Is Nothing Then
       Set qResp = msgRead.ResponseQueueInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
       msgResp.Label = "Response Message"
       msgResp.Body = "This is a response message"
       msgResp.CorrelationId = msgRead.id
       msgResp.Send qResp
       MsgBox "A response message was returned to :" + msgRead.ResponseQueueInfo.PathName
    Else
       MsgBox "No response was requested."
    End If
     

Example

This example locates the response queue and the destination queue (creating them if needed), displays a message box that allows you to request a response message, then sends the message depending on your response.

Next, the example reads the message from the queue, returning a response message if one is requested. If the response message is requested, its correlation identifier, CorrelationId, is set to the message identifier of the original message and the response message is sent.

To try this example using Microsoft® Visual Basic® (version 5.0), paste the code into the code window of a form, and then run the example and click the form.

Dim query As New MSMQQuery
Dim qinfos As MSMQQueueInfos
Dim qinfoResp, qinfoDest As MSMQQueueInfo
Dim qRead, qResp As New MSMQQueue
Dim msgSent As New MSMQMessage
Dim msgRead As New MSMQMessage
Dim msgResp As New MSMQMessage

Private Sub Form_Click()
   
   '**********************************
   ' Locate response queue (create one
   ' if one doesn't exist).
   '**********************************
   Set qinfos = query.LookupQueue(Label:="Response Queue")
   qinfos.Reset
   Set qinfoResp = qinfos.Next
   If qinfoResp Is Nothing Then
      Set qinfoResp = New MSMQQueueInfo
      qinfoResp.PathName = ".\RespQueue"
      qinfoResp.Label = "Response Queue"
      qinfoResp.Create
   End If
   '**********************************
   ' Locate destination queue
   '(create one if one doesn't exist).
   '**********************************
   Set qinfos = query.LookupQueue(Label:="Destination Queue")
   qinfos.Reset
   Set qinfoDest = qinfos.Next
   If qinfoDest Is Nothing Then
      Set qinfoDest = New MSMQQueueInfo
      qinfoDest.PathName = ".\DestQueue"
      qinfoDest.Label = "Destination Queue"
      qinfoDest.Create
   End If
   
   '************************
   ' Open destination queue.
   '************************
   Set q = qinfoDest.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
   
   '**************
   ' Send Message.
   '**************
   msgSent.Label = "Test Message"
   msgSent.Body = "This message tests the response queue."
   
   response = MsgBox("Do you want a response message?", 4)
   If response = 6 Then
      Set msgSent.ResponseQueueInfo = qinfoResp
   End If
   
   msgSent.Send q
  
   MsgBox "The message was sent. Check the MSMQ Explorer to see the messages in the queue."
   q.Close
  
   '************************************
   ' Read the message in the destination
   ' queue and send response message if
   ' one is requested.
   '************************************
   Set q = qinfoDest.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
   Set msgRead = q.Receive
   
   If Not msgRead.ResponseQueueInfo Is Nothing Then
      Set qResp = msgRead.ResponseQueueInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
      msgResp.Label = "Response Message"
      msgResp.Body = "This is a response message"
      msgResp.CorrelationId = msgRead.id
      msgResp.Send qResp
      MsgBox "A response message was returned to :" + msgRead.ResponseQueueInfo.PathName
   Else
      MsgBox "No response was requested."
   End If
    
End Sub
 

© 1997 by Microsoft Corporation. All rights reserved.