Delen via


Visual Basic Code Example: Requesting Source Journaling

 

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 requests source journaling based on the journaling level provided by the caller. This function sets the MSMQMessage.Journal property of the message using the provided journaling level, and then sends the message to a known destination queue. For positive source journaling, the value of the journaling level passed to the function must be MQMSG_JOURNAL, and for negative source journaling, the value must be MQMSG_DEADLETTER. Both positive and negative source journaling can be requested by passing MQMSG_JOURNAL | MQMSG_DEADLETTER.

For information on source journaling, see Source Journaling.

To request source journaling

  1. Declare the objects needed to send a message that requests positive source journaling. This procedure declares the following objects.

    MSMQDestination

    MSMQMessage

  2. Generate a direct format name using the computer name and queue name provided by the caller.

  3. Set the MSMQDestination.FormatName property.

  4. Set the MSMQMessage.Journal property using the journaling level provided by the caller.

  5. Optional. Set additional message properties. This procedure sets the MSMQMessage.Label property of the message to "Test Message: Positive Journaling".

  6. Call MSMQMessage.Send to send a copy of the message to the destination queue referenced by the format name.

Note

When using an MSMQDestination object, Message Queuing implicitly opens the destination queue with send access and then closes the destination queue when the object is released.

Code Example

The following code example requires MSMQ 3.0.

Sub RequestJournaling( _  
                      strComputerName As String, _  
                      strQueueName As String, _  
                      lJournalLevel As Long _  
                      )  
  Dim strFormatName As String  
  Dim dest As New MSMQDestination  
  Dim msg As New MSMQMessage  
  
  ' Create a direct format name.  
  strFormatName = "DIRECT=OS:" & strComputerName & "\" & strQueueName  
  
  ' Set the format name of the MSMQDestination object.  
  On Error GoTo ErrorHandler  
  dest.FormatName = strFormatName  
  
  ' Set the Journal property to request positive journaling.  
  msg.Journal = lJournalLevel  
  
  ' Set other message properties.  
  msg.Label = "Test Message: Positive Journaling"  
  
  ' Send the message.  
  msg.Send DestinationQueue:=dest  
  
  Exit Sub  
  
  ErrorHandler:  
    MsgBox "Error " + Hex(Err.Number) + " was returned." _  
           + Chr(13) + Err.Description  
End Sub