Acknowledgments and Time-Outs

Team Fly 

Page 358

specified ID. If no related messages exist in the queue, the PeekByCorrelationID method returns a Nothing value.

There's one simple operation that's fairly expensive in terms of resources, and this is the counting of messages in a queue. There's no method to return the number of messages in a queue. You must call the GetAllMessages method, which accepts no arguments and returns an array with all the messages in the queue, and then examine the array's Length property (or call its GetUpperBound method). This is a fairly expensive operation and its value is questionable, because the number of messages in the queue varies all the time. As with all classes that implement the IEnumerable interface, you should use the appropriate Enumerator to actually process the messages. If you want to look at the messages and then decide which one to process, you can call the GetAllMessages method to retrieve a snapshot of the queue and then retrieve each message you want to process with the ReceiveByID method, or process one or more of the messages returned by the GetAllMessages method and then remove it from the queue with the ReceiveByID method. We'll use the GetAllMessages method in the OrdersServer project later in this chapter to copy all the messages in the queue and allow the user to view them, move back and forth through them, and select which ones to process. The processed messages will be removed from the queue with the RemoveAt method.

Acknowledgments and Time-Outs

Sending a message to a remote queue doesn't mean that the message will actually arrive at its destination. The destination queue may not exist, the computer on which the queue resides may be disconnected, or the application that sent the message may not have the rights to write to the specific queue. Even if a message is delivered to the destination queue, we can't be sure that the message will actually be retrieved from the queue and processed. Sometimes it's critical that a message is processed within a predefined time interval. If the messages represent orders, for example, your application should eventually find out which orders were processed and which not, and take the appropriate action. Delaying the processing of an order for a week is simply unacceptable.

A robust distributed application should be able to know whether a message has reached the destination queue and whether it was retrieved from the queue and processed. MSMQ provides a confirmation mechanism based on acknowledgments. Acknowledgments are messages that are generated automatically by MSMQ in response to events, such as the arrival of a message to a queue or the retrieval of a message from a queue. Acknowledgments are not generated by default; you must request that specific acknowledgments be generated and forwarded to a specific queue. You can also request positive acknowledgments (signifying the successful completion of an operation) or negative acknowledgments (signifying the failure of an operation).

Requesting Message Acknowledgment

To request the acknowledgment of a message, you must first create a queue that will receive the acknowledgment messages. The queue that will accept the acknowledgment messages is a regular queue; there's nothing special about messages sent to this queue, except for the fact that they're generated automatically. To use the acknowledgment queue, you must set certain properties of the message before you send it. These properties are the AdministrationQueue property, which determines where the acknowledgment messages are sent, and the AcknowledgeTypes property, which indicates what

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

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