Sdílet prostřednictvím


Visual Basic Code Example: Reading Messages in the Transactional Dead-Letter Queue

 

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 Sub procedure that synchronously reads all the messages in the transactional dead-letter queue (DEADXACT) of a known computer.

This example receives the computer name and generates the machine format name needed to open the transactional dead-letter queue for reading messages from the computer GUID obtained by calling the MSMQApplication.MachineIdOfMachineName method. This method cannot be used if there is no connection to the directory service. This restriction applies to dependent clients, independent clients that are working offline, and Message Queuing routing servers.

For information on when Message Queuing sends messages to the transactional dead-letter queue, see Source Journaling.

In MSMQ 2.0 and later, this procedure can be modified to generate the direct format name of the transactional dead-letter queue instead of retrieving its machine format name.

To read message in a transactional dead-letter queue

  1. Declare the objects needed to retrieve the message.

Note

When declaring the MSMQMessage object when reading messages, the New keyword cannot be used.

  1. Obtain the identifier of the computer provided by the caller. This identifier is needed to create a format name for the dead-letter queue.

  2. Set the MSMQQueueInfo.FormatName property. The syntax for referencing transactional dead-letter queues is as follows:

    MACHINE=MachineGUID;DEADXACT  
    

    In MSMQ 2.0 and later, the direct format name of the transactional dead-letter queue can be generated instead of using the machine format name. This step is then replaced by the following code:

    MSMQQueueInfo.FormatName = "DIRECT=OS:" & wszComputerName & "\SYSTEM$;DEADXACT"  
    
  3. 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.

  4. Using a loop, call MSMQQueue.Receive to read each messages in the dead-letter queue.

  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.

Sub ReadingComputerXactDeadLetter( _  
                                  strComputerName As String _  
                                  )  
  Dim qinfo As New MSMQQueueInfo  
  Dim q As MSMQQueue  
  Dim msg As MSMQMessage  
  Dim strMachineId As String  
  
  ' Obtain the computer GUID for the format name.  
  On Error GoTo ErrorHandler  
  strMachineId = MachineIdOfMachineName(strComputerName)  
  
  ' Set the format name of transactional dead-letter queue.  
  qinfo.FormatName = "MACHINE=" & strMachineId & ";DEADXACT"  
  
  ' Open the transactional dead-letter queue.  
  Set q = qinfo.Open(Access:=MQ_RECEIVE_ACCESS, _  
                         ShareMode:=MQ_DENY_NONE)  
  
  ' Read the first message in the transactional dead-letter queue.  
  Set msg = q.Receive(ReceiveTimeout:=1000)  
  
  ' Read the remaining messages in the queue.  
  Do While Not msg Is Nothing  
    MsgBox "A message was removed from the queue."  
    Set msg = q.Receive(ReceiveTimeout:=1000)  
  Loop  
  
  MsgBox "There are no more messages in the transactional dead-letter queue. The queue will be closed."  
  q.Close  
  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