다음을 통해 공유


Visual Basic Code Example: Sending a Message Using a Single-Message Transaction

 

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

Message Queuing provides a single-message transaction that implicitly uses the internal transaction support provided by Message Queuing. The benefits of this type of transaction include:

  • Greater speed, compared to using an external transaction coordinator.

  • Simplified application code.

  • Exactly-once delivery without the need to coordinate other activities within a single transaction.

The only difference between sending a nontransactional message and using a single-message transaction is that the destination queue must be a transactional queue, and the Transaction parameter of MSMQMessage.Send must be set to MQ_SINGLE_MESSAGE. For more information on single-message transactions, see Single-Message Transactions.

Note

Each Message Queuing message can have no more than 4 MB of data.

To send a message in a single-message transaction

  1. Declare the objects needed to send a message. This example uses MSMQQueueInfo, MSMQQueue, and MSMQMessage objects.

  2. Call MSMQQueueInfo.Open to open the transactional queue with send access. This example also calls MSMQQueueInfo.Create to create a transactional queue on the local computer if the specified queue does not exist.

  3. Call MSMQMessage.Send to send the message. In this example, the Transaction parameter is set to MQ_SINGLE_MESSAGE.

  4. Call MSMQQueue.Close to close the queue.

Code Example

This example sends a single message to a known transactional queue. It uses the path name of the queue to specify the queue, opens the queue with send access, sends the message to the queue in a single-message transaction, and then closes the queue.

To try this example in Microsoft® Visual Basic® (version 5.0 or 6.0), paste the code into the Declaration section of a form, press F5, and click the form.

Option Explicit  
  
' Declare the required objects.  
Dim qinfo As New MSMQQueueInfo  
Dim qSend As MSMQQueue  
Dim msg As New MSMQMessage  
  
Private Sub SendSingleMessageXact()  
  
  ' Open the transactional queue with SEND access. The queue is  
  ' created if no such queue exists.  
  qinfo.PathName = ".\TransactionTestQueue"  
  qinfo.Label = "Send Message Test"  
  On Error Resume Next          'Ignore if queue already exists.  
  qinfo.Create IsTransactional:=True  
  On Error GoTo ErrorHandler  
  Set qSend = qinfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)  
  
  ' Send the message to queue as a single-message transaction.  
  msg.Label = "Transactional test message"  
  msg.Body = "Message sent as single-message transaction."  
  msg.Send qSend, MQ_SINGLE_MESSAGE  
  
  ' Close the queue.  
  qSend.Close  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Unexpected error!" + Chr(13) + _  
         "Reason: " + Err.Description _  
         + Chr(13) + "Error: " + Hex(Err.Number)  
  If Not qSend Is Nothing And qSend.IsOpen2 Then   
    qSend.Close  
  End If  
End Sub