Поделиться через


Visual Basic Code Example: Acknowledgment Class Filter

 

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

This example provides a private Sub procedure that retrieves all the messages in a queue that have a given acknowledgment class. Filtering is done by peeking at the MSMQMessage.MsgClass property of each message in the queue and removing only those messages whose property value matches an acknowledgment class provided by a known acknowledgment class.

Note

Message Queuing requires a 20-byte correlation identifier.

To filter messages by acknowledgment class

  1. Declare the objects needed to read the messages in the queue.

Note

When declaring the MSMQMessage object when reading messages, you cannot use the New keyword.

  1. Obtain an MSMQQueueInfo object. The following example initializes the MSMQQueueInfo object by setting the MSMQQueueInfo.PathName property using the computer name and queue name provided by the caller.

    Because this procedure sets the MSMQQueueInfo.PathName property of the MSMQQueueInfo object, Message Queuing must obtain the format name of the queue before opening the queue. The format name of a public queue must be retrieved from the directory service, and the format name of a local private queue can be obtained from information stored on the local computer. However, a remote private queue cannot be opened unless the MSMQQueueInfo.FormatName property is set with a direct format name. This procedure can be modified to receive the format name from the caller or to generate a direct format name. The applicable format name can then be used to set the FormatName property. For more information, see Format Names.

  2. Call MSMQQueueInfo.Open to open the queue with receive access. When opening a queue with receive access the application can peek at or retrieve the messages in the queue.

  3. Call MSMQQueue.PeekCurrent to initialize the cursor. This call points the cursor to the first message in the queue.

  4. Using a loop, filter out the messages with the acknowledgment class provided by the caller.

    If the message has the correct acknowledgment class, the call to MSMQQueue.ReceiveCurrent removes the message and Message Queuing moves the cursor to the next message.

    If the message does not have the correct acknowledgment class, the call to MSMQQueue.PeekNext moves the cursor to the next message.

  5. When there are no messages left, call MSMQQueue.Close to release resources used to open queue and exit the Sub procedure.

Code Example

The following code example can be run on all versions of Message Queuing.

Private Sub FilterAckClass( _  
                           strAdminQueueName As String, _  
                           strComputerName As String, _  
                           lAckClass As Long _  
                           )  
  
  ' Declare Message Queuing objects.  
  Dim qinfo As New MSMQQueueInfo  
  Dim q As MSMQQueue  
  Dim msg As MSMQMessage  
  
  ' Set the path name in the  MSMQQueueInfo object.  
  qinfo.PathName = strComputerName & "\" & strAdminQueueName  
  
  ' On Error GoTo ErrorHandler  
  
  ' Open the queue with receive access.  
  Set q = qinfo.Open(Access:=MQ_RECEIVE_ACCESS, ShareMode:=MQ_DENY_NONE)  
  
  ' Peek at all messages in the queue using the cursor.  
  
  Set msg = q.PeekCurrent(ReceiveTimeout:=1000)  
  Do While Not msg Is Nothing  
  
    ' Check if the message has the acknowledgment class that we are seeking.  
    If msg.MsgClass = lAckClass Then  
      ' The acknowledge class matches. Display a MsgBox.  
      MsgBox "A message with the requested acknowledge class was found."  
  
      'Receive the message.  
      Set msg = q.ReceiveCurrent  
      Set msg = q.PeekCurrent(ReceiveTimeout:=1000)  
    Else  
      Set msg = q.PeekNext(ReceiveTimeout:=1000)  
    End If  
  Loop  
  
  ' Close the queue.  
  q.Close  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned. " + Chr(13) + _  
         "Description: " + Err.Description  
  If Not q Is Nothing And q.IsOpen2 Then  
    q.Close  
  EndIf  
End Sub