Browse DevX
Sign up for e-mail newsletters from DevX


MSMQ for .NET Developers (Part 1) : Page 3

MSMQ provides robust and easy-to-use messaging capabilities that let you send and receive messages between applications running on one machine, different machines, and even across the Internet, both synchronously and asynchronously. Unfortunately, MSMQ is notoriously underused. Find out how to get started with MSMQ and what you can do with it.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Developer-friendly Application Features
MSMQ provides several common features that you'll use in most projects. These features have been part of MSMQ from the beginning; without them, MSMQ developers would probably need to build these features into a "wrapper" or management application to provide the same functionality.

The CorrelationID Property
You use the CorrelationId property of a message to correlate two or more messages with one another. The typical usage scenario involves a client sending a request to a server for processing, and the server sending a message to the client when processing is complete. If this process is managed asynchronously, you will need a way to correlate responses with their respective requests—enter the CorrelationId property. In the scenario above, the client would retrieve the MSMQ-assigned ID property of the message being sent, and store it in an application-specific state table. On the server side, as the application creates response messages, it can set the CorrelationId of the response message to the value of the ID property of the request message. When the client application receives response messages from the server, it compares the CorrelationId property of the response message to the value of the message ID it had stored previously. The primary value of the CorrelationId is in correlating requests and responses.

In the code sample below, the client stores the original ID of the request message. The server code uses the request message's ID property to set the CorrelationId property of the response message. Then, when the client receives the response message, it uses the original request message ID to match the response message CorrelationId being returned from the server process.

For example, here's a client implementation that sends a request.

Private Sub SendRequestMessage(ByVal InternalState As String) Dim RequestQueue As System.Messaging.MessageQueue Dim RequestMessage As System.Messaging.Message Dim MessageId As String ' open send queue RequestQueue = GetRequestQueue() ' create message and set properties RequestMessage = New Message RequestMessage.Body = GetRequest(InternalState) MessageId = RequestMessage.Id() ' send message RequestQueue.Send(RequestMessage) ' store message state information StoreReqeust(InternalState, MessageId) End Sub

This server implementation sets the response message's CorrelationId when processing the request.

Private Sub SendResponse(ByVal RequestMessage As Message, _ ByVal Response As String) Dim ResponseMessage As New System.Messaging.Message(Response) Dim ResponseQueue As System.Messaging.MessageQueue ResponseQueue = GetResponseQueue() ' set the response message correlationId ResponseMessage.CorrelationId = RequestMessage.Id ResponseMessage.Body = Response ' send the response message ResponseQueue.Send(ResponseMessage) End Sub

Finally, here's how the client implementation would handle the response.

Private Sub NewMessageArrived(ByVal ResponseMessage As Message) Dim InternalState As String InternalState = LookupRequest(ResponseMessage.CorrelationId) ProcessResponse(InternalState, ResponseMessage) End Sub

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date