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, acknowledgment queues or response queues can be used.

Acknowledgment Queues

With an acknowledgment queue, the sending application can get information about the status of the message. With the acknowledgments, you can define if you would like to receive an answer, if everything went OK, or if something went wrong. For example, acknowledgments 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 acknowledgments. The AcknowledgmentType property is set to AcknowledgementTypes .FullReceive to get an acknowledgment 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 acknowledgment message belongs to which message sent. Every message that is sent has an ID, and the acknowledgment message that is sent in response to that message holds the id of the originating message as its correlation ID. The messages from the acknowledgment queue can be read using MessageQueue.ReceiveByCorrelationId() to receive the associated acknowledgment.

Instead of using acknowledgments, 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 acknowledgment 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 acknowledgment 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 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