A Queue and a User waiting for a response

I came across an interesting problem the other day where we had to change an interface from SOAP/HTTP to SOAP/MQ. When I originally hit this I saw all sorts off issues with Queues being asynchronous and HTTP was being used synchronously and all that fun. The system was being built in Java and we eventually found some code that showed how to do it using JMS.

Now I was interested to know how to achieve the same outcome in .Net. As a starting point there is a great article from the Enterprise Integration Patterns book site that shows how to implement the Request/Reply pattern in .Net. The key of the request/reply pattern is that the request is put on a request queue and the process then waits on the reply queue for the reply in effect making the whole sequence synchronous.

I needed to extend the pattern such that I would have multiple clients putting request on the queue and only wanting to see the reply to their request. Correlation ID come to the help here in that on the receiving end I can tell the queue that I am only interested in messages with a specific Correlation ID. In this example the replying process uses the Message ID from the request message as the correlation ID for the reply message. The TimeSpan is specifying a timeout for the call, which will raise an exception in the scenario that we don’t receive a response. This is important in the scenario that we were concerned with as we are blocking a http request from a user while we are waiting for a reply message.

replyQueue.ReceiveByCorrelationId(correlationId, TimeSpan.FromSeconds(15));

This is going to be used with a web client, so there is the chance that something has happened so that the reply doesn’t get read off the reply queue. Rather than build up a collection of orphaned replies it is easy enough to give the message an expiry using the TimeToBeReceived property. The other thing is to ensure the UseDeadLetterQueue property is false so once the message expires the message just disappears.

replyMessage.TimeToBeReceived = TimeSpan.FromSeconds(60); replyMessage.UseDeadLetterQueue = false;

Still not sure whether I would specifically design a system to use a Queue in this fashion but it definitely looks like queues can be used in this fashion.

Note: This can only be done with a non-transactional queue



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: