Question

I am trying to evaluate if Azure Service Bus will fit my requirements.

I need to be able to have multiple clients listening for messages but I want to ensure that only one of those clients can process a message at any given time. So in effect when I want to lock the whole queue for reading whilst a message is being processed. (The message basically is a signal to initiate some database aggregation jobs)

So if message A arrives and client 1 starts processing it, then if message B arrives, I do not want client 2 to start processing it unless client 1 has finished.

I realise I can achieve this by only having 1 client listening. My other option is to look at instance id and only start the listener if instance id = 0, but I wondered if there was a way of avoiding this instance id specific logic.

Was it helpful?

Solution

You can achieve this using Sessions over a Service Bus Queue. From a sender perspective you can add a string SessionId property on each message and designate which session they belong to. On the receiver side you call AcceptMessageSession and essentially lock the entire session. Any messages that show up for that session later will only be delivered to the receiver that has the session locked by them calling Receive. Other clients can also be calling AcceptMessageSession on the queue but say if all messages were going to a single session (all had that same sessionid) then only one of the receiver would see it. A samples using sessions is available here: http://code.msdn.microsoft.com/windowsazure/Brokered-Messaging-Session-41c43fb4

The scenario you are describing is not clear as to the purpose so do look into Azure storage Blob lease etc. as they may solve the same locking semantics you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top