Compartir a través de


Visual Basic Code Example: Reading Messages in a Queue Journal

 

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 journal of a known destination queue.

For information on when Message Queuing sends messages to the journal of a queue, see Target Journaling.

To read a message in a queue journal

  1. Declare the objects needed to retrieve the message.

Note

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

  1. Obtain an MSMQQueueInfo object.

  2. Construct the path name of the corresponding queue from the computer name and queue name supplied by the caller. In the case of a private queue, the caller must prefix the queue name with the PRIVATE$ keyword.

  3. Initialize the MSMQQueueInfo object by setting its MSMQQueueInfo.PathName property.

  4. Reset the MSMQQueueInfo.FormatName property with the format name of the queue journal. The syntax for referencing a queue journal is:

    PUBLIC=QueueGUID;JOURNAL  
    PRIVATE=ComputerGUID\QueueNumber;JOURNAL  
    DIRECT=os:ComputerName\QueueName;JOURNAL  
    DIRECT=os:ComputerName\PRIVATE$\QueueName;JOURNAL  
    

    In this example, the format name of the queue journal is constructed from the public or private format name of the corresponding queue that was created.

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

  6. Using a loop, call MSMQQueue.Receive to read each messages in the journal.

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

Code Example

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

Sub ReadingQueueJournal( _  
                        strComputerName As String, _  
                        strQueueName As String _  
                        )  
  Dim qinfo As New MSMQQueueInfo  
  Dim q As MSMQQueue  
  Dim msg As MSMQMessage  
  
  ' Set the path name in the MSMQQueueInfo object.  
  On Error GoTo ErrorHandler  
  qinfo.PathName = strComputerName & "\" & strQueueName  
  qinfo.Refresh  
  
  ' Construct the format name of the queue journal.  
  qinfo.FormatName = qinfo.FormatName & ";JOURNAL"  
  
  ' Open the queue journal.  
  Set q = qinfo.Open(Access:=MQ_RECEIVE_ACCESS, _  
                         ShareMode:=MQ_DENY_NONE)  
  
  ' Read the first message in the queue journal.  
  Set msg = q.Receive(ReceiveTimeout:=1000)  
  
  ' Read the remaining messages in the queue journal.  
  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. The queue journal 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