Receiving Results


With the current version of the sample application, the sending application never knows if the message is ever dealt with. To get results from the receiver, acknowledgement queues or response queues can be used.

Acknowledgement Queues

With an acknowledgement queue, the sending application can get information about the status of the message. With the acknowledgements you can define if you would like to receive an answer, if everything went OK, or if something went wrong. For example, acknowledgements can be sent when the message reaches the destination queue or when the message is read or if it didn't reach the destination queue or was not read before a timeout elapsed.

In the example the AdministrationQueue of the Message class is set to the CourseOrderAck queue. This queue must be created similar to a normal queue. This queue is just used the other way around: the original sender receives acknowledgements. The AcknowledgementType property is set to AcknowledgementTypes.FullReceive to get an acknowledgement when the message is read:

 Message message = new Message(order); message.AdministrationQueue =  new MessageQueue(@".\CourseOrderAck"); message.AcknowledgementType = AcknowledgementTypes.FullReceive; queue.Send(message, "Course Order {" +  order.Customer.Company + "}"); string id = message.Id; 

The correlation id is used to determine what acknowledgement message belongs to which message sent. Every message that is sent has an id, and the acknowledgement message that is sent in response to that message holds the id of the originating message as its correlation id. The messages from the acknowledgement queue can be read using MessageQueue.ReceiveByCorrelationId() to receive the associated acknowledgement.

Instead of using acknowledgements, the dead-letter queue can be used for messages that didn't arrive at their destination. By setting the UseDeadLetterQueue property of the Message class to true, the message is copied to the dead-letter queue if it didn't arrive at the target queue before the timeout was reached.

Timeouts can be set with the Message properties TimeToReachQueue and TimeToBeReceived.

Response Queues

If more information than an acknowledgement is needed from the receiving application, a response queue can be used. A response queue is like a normal queue, but the original sender uses the queue as a receiver and the original receiver uses the response queue as a sender.

The sender must assign the response queue with the ResponseQueue property of the Message class. The sample code here shows how the receiver uses the response queue to return a response message. With the response message responseMessage the property CorrelationId is set to the id of the original message. This way the client application knows what message the answer belongs to. This is similar to acknowledgement queues. The response message is sent with the Send() method of the MessageQueue object that is returned from the ResponseQueue property:

 public void ReceiveMessage(Message message) { Message responseMessage = new Message("response"); responseMessage.CorrelationId = message.Id; message.ReesponseQueue.Send(responseMessage); } 




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

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