Microsoft KB Archive/179016

= How To Send in a MSMQ Single-Message Transaction =

Article ID: 179016

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Message Queue Server 1.0

-



This article was previously published under Q179016



SUMMARY
As part of its internal transaction support, Microsoft Message Queue Server (MSMQ) provides specific functionality to handle cases where an MSMQ application wants to send only a single message in a single transaction. One reason to do this is to ensure exactly-once delivery, without needing to coordinate with other activities into a single transaction.

The benefits of using this functionality are:


 * Speed, compared to using an external transaction coordinator.
 * Simplified application code.
 * Delivery that is assured to occur exactly once.

The software development kit (SDK) provides examples of doing this as an explicit internal transaction. This article provides examples of how to perform the send as an implicit internal transaction, because there are no examples of this in the SDK online Help.



MORE INFORMATION
The following code sample illustrates sending a single-message transaction using the ActiveX methods: Dim qinfo As New MSMQQueueInfo Dim q As MSMQQueue Dim m As New MSMQMessage

qinfo.PathName = "MachineName\TransactionalQueueName" Set q = qinfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE) m.Send q, MQ_SINGLE_MESSAGE q.Close Using C API, set the pTransaction parameter to MQ_SINGLE_MESSAGE. The following code uses the "Sending Messages Using an Internal Transaction" example from the SDK; it demonstrates an explicit internal transactional send and modifies it for the implicit semantics: void TransactSend(QUEUEHANDLE h, MQMSGPROPS * pMsgProps) {     HRESULT hr; printf ("\nStarting transaction...\n\n"); //////////////////////////////////////      // Call MQSendMessage to send a transactional message to      // the destination queue. //////////////////////////////////////      hr = MQSendMessage(h,                  // Handle to dest queue                        &msgprops,           // Pointer to MQMSGPROPS                        MQ_SINGLE_MESSAGE);  // Implicit internal // transactional message. if (FAILED(hr)) {     printf("\nFailed in MQSendMessage. hresult- %lxh\n", (DWORD) hr) ; }     else {         printf ("Committing the transaction...   "); }    }

