HOWTO: Send in a MSMQ Single-Message Transaction

ID: Q179016

The information in this article applies to:

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:

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...   ");
      }
     }

REFERENCES

For more information and sample code regarding MSMQ transactions, see the "MQSendMessage," "MSMQ Internal Transactions," and "Transaction Programming Considerations" topics in the SDK online Help.

Additional query words: programming interface

Keywords          : MQAPI MQControls MQProg MQQueue MQVB MQVC 
Version           : WINNT:1.0
Platform          : winnt
Issue type        : kbhowto

Last Reviewed: January 8, 1998