Visual Basic Code Example: Reading Messages in a Queue Journal
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
Declare the objects needed to retrieve the message.
|When declaring the MSMQMessage object for reading messages, the New keyword cannot be used.|
Obtain an MSMQQueueInfo object.
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.
Initialize the MSMQQueueInfo object by setting its MSMQQueueInfo.PathName property.
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
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.
Using a loop, call MSMQQueue.Receive to read each messages in the journal.
When there are no messages left, call MSMQQueue.Close to release the resources used to open the journal and exit the Sub procedure.
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