15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Transactional Queues ❘ 1377<br />

to that message holds the ID of the originating message as its correlation ID. The messages from the<br />

acknowledgment queue can be read using MessageQueue.ReceiveByCorrelationId() to receive<br />

the associated acknowledgment.<br />

Instead of using acknowledgments, the dead-letter queue can be used for messages that didn’t arrive at<br />

their destination. With the UseDeadLetterQueue property of the Message class set to true, the message is<br />

copied to the dead-letter queue if it didn’t arrive at the target queue before the timeout was reached.<br />

Timeouts can be set with the Message properties TimeToReachQueue <strong>and</strong> TimeToBeReceived.<br />

response queues<br />

If more information than an acknowledgment is needed from the receiving application, a response queue can<br />

be used. A response queue is like a normal queue, but the original sender uses the queue as a receiver <strong>and</strong> the<br />

original receiver uses the response queue as a sender.<br />

The sender must assign the response queue with the ResponseQueue property of the Message class. The<br />

sample code here shows how the receiver uses the response queue to return a response message. With<br />

the response message responseMessage, the property CorrelationId is set to the ID of the original<br />

message. This way the client application knows to which message the answer belongs. This is similar to<br />

acknowledgment queues. The response message is sent with the Send() method of the MessageQueue object<br />

that is returned from the ResponseQueue property:<br />

public void ReceiveMessage(Message message)<br />

{<br />

Message responseMessage = new Message("response");<br />

responseMessage.CorrelationId = message.Id;<br />

}<br />

message.ResponseQueue.Send(responseMessage);<br />

TransaCTional queues<br />

With recoverable messages, it is not guaranteed that the messages will arrive in order <strong>and</strong> just once. Failures<br />

on the network can cause messages to arrive multiple times; this happens also if both the sender <strong>and</strong> receiver<br />

have multiple network protocols installed that are used by Message Queuing.<br />

Transactional queues can be used when these guarantees are required:<br />

➤<br />

➤<br />

Messages arrive in the same order they have been sent.<br />

Messages arrive only once.<br />

With transactional queues, a single transaction doesn’t span the sending <strong>and</strong> receiving of messages. The<br />

nature of Message Queuing is that the time between send <strong>and</strong> receive can be quite long. In contrast,<br />

transactions should be short. With Message Queuing, the first transaction is used to send the message into<br />

the queue, the second transaction forwards the message on the network, <strong>and</strong> the third transaction is used to<br />

receive the messages.<br />

The next example shows how to create a transactional message queue <strong>and</strong> how to send messages using a<br />

transaction.<br />

A transactional message queue is created by passing true with the second parameter of the MessageQueue.<br />

Create() method.<br />

If you want to write multiple messages to a queue within a single transaction, you have to instantiate a<br />

MessageQueueTransaction object <strong>and</strong> invoke the Begin() method. When you are finished with sending<br />

all messages that belong to the transaction, the Commit() method of the MessageQueueTransaction object<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!