Visual Basic Code Example: Returning Response Messages

 

Updated: July 19, 2016

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

The following example provides a private Sub procedure that peeks at the MSMQMessage.ResponseDestination property of a message and returns a response message if a response queue was specified.

For information on responding to sent messages, see Response Messages.

This example uses the MSMQMessage.ResponseDestination property (introduced in MSMQ 3.0) to determine if the sending application requested response messages. This property returns an MSMQDestination object that can reference any of the following format name types.

  • A single public, private, or direct format name. These format names are used to specify a single response queue.

    For information about these format names, see Public Format Names, Private Format Names, and Direct Format Names.

  • A multiple-element format name. This format name is used to specify multiple response queues.

    For information about multiple-element format names, see Multiple-Element Format Names.

  • A distribution list format name. This format name is used to specify multiple response queues.

    For information about distribution list format names, see Distribution List Format Names.

  • A multicast address format name. This format name is used to specify multiple response queues.

    For information about multicast address format names, see Multicast Address Format Names.

To return response messages

  1. Declare Message Queuing objects.

  2. Call MSMQQueueInfo.Open to open destination queue to read a message.

  3. Call MSMQQueue.Peek to read the first message in the queue.

  4. Process message to see if response message is requested.

  5. If a response was requested, set the MSMQDestination object to the response queue specified in the original message.

  6. Set the properties of the response queue.

System_CAPS_ICON_note.jpg Note

This example returns the message identifier and destination of the original message in the response message.

  1. Send the response message.
System_CAPS_ICON_note.jpg Note

There is no need to close the response queue when using an MSMQDestination object.

The following code example requires MSMQ 3.0.

Sub ReturnResponse( _  
                   strFormatName As String _  
                   )  
  ' Objects for reading a message.  
  Dim qinfo As New MSMQQueueInfo  
  Dim q As MSMQQueue  
  Dim msgIn As MSMQMessage        ' Do not use "New" when reading messages.  
  ' Objects for sending the message.  
  Dim dest As New MSMQDestination  
  Dim msgResp As New MSMQMessage  
  
  On Error GoTo ErrorHandler  
  
  ' Open destination queue to read a message.  
  qinfo.FormatName = strFormatName  
  Set q = qinfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)  
  
  ' Peek at the first message in the queue then close the queue  
  Set msgIn = q.Peek  
  q.Close  
  
  ' Determine whether a response message is requested.  
  If msgIn.ResponseDestination Is Nothing Then  
    Exit Sub  
  End If  
  
  ' Set the destination.  
  Set dest = msgIn.ResponseDestination  
  
  ' Set properties of the response message.  
  msgResp.Label = "Test Message: This is a response."  
  Set msgResp.ResponseDestination = msgIn.Destination  
  msgResp.CorrelationId = msgIn.Id  
  
  ' Send the response message.  
  msgResp.Send DestinationQueue:=dest  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
  If Not q Is Nothing And q.IsOpen2 Then  
    q.Close  
  EndIf  
End Sub  
  

Community Additions

ADD
Show: