Transactional Queues


With recoverable messages, it is not guaranteed that the messages will arrive in order and just once. Failures on the network can cause messages to arrive multiple times; this happens also if both the sender and receiver have multiple network protocols installed that are used by Message Queuing.

Transactional queues can be used when these guarantees are required:

  • Messages arrive in the same order they have been sent.

  • Messages arrive only once.

With transactional queues, a single transaction doesn’t span the sending and receiving of messages. The nature of Message Queuing is that the time between send and receive can be quite long. In contrast, transactions should be short. With Message Queuing, the first transaction is used to send the message into the queue, the second transaction forwards the message on the network, and the third transaction is used to receive the messages.

The next example shows how to create a transactional message queue and how to send messages using a transaction.

A transactional message queue is created by passing true with the second parameter of the MessageQueue .Create() method.

If you would like to write multiple messages to a queue within a single transaction, you have to instantiate a MessageQueueTransaction object and invoke the Begin() method. When you are finished with sending all messages that belong to the transaction, the Commit() method of the MessageQueueTransaction object must be called. To cancel a transaction (and have no messages written to the queue), the Abort() method must be called, as you can see within the catch block:

 using System; using System.Messaging; namespace Wrox.ProCSharp.Messaging {    class Program     {       static void Main(string[] args)       {          if (!MessageQueue.Exists(@".\MyTransactionalQueue"))          {             MessageQueue.Create(@".\MyTransactionalQueue", true);          }             MessageQueue queue = new MessageQueue(@".\MyTransactionalQueue");             MessageQueueTransaction transaction =                      new MessageQueueTransaction();          try          {             transaction.Begin();             queue.Send("a", transaction);             queue.Send("b", transaction);             queue.Send("c", transaction);             transaction.Commit();          }          catch          {             transaction.Abort();          }       }    } }




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net