0
votes

I have several client applications waiting to take message jobs off of an Azure Service Bus queue.

Once a client has the job I don't want any other client application to receive the job.

If all the clients implement Queue.OnMessage() to be notified of arriving messages, how can I be sure that 2 or more clients don't pick up the message?

2
Can you make it clear ? Do you ask 1 consumer should just get one type message but the others ones shouldn't or when One consumer get any message the other ones shouldn't? If you make it clear and give an example it would be better.Erkan Demirel

2 Answers

1
votes

My understanding is that as soon as one of ur workers got an item from the queue it becomes invisible for others for a certain configurable amount of time (lease). If a worker completed the job successfully it notifies the queue that the item can be deleted. If the worker fails it can notify the queue to move the item to the dead letter queue, if the worker dies the item becomes visible after the lease time for other workers. There should be a property within the item (set by ServiceBus) indicating how many times an item was delivered.There is also a TTL (time to live) after which an item will be automatically moved to the dead letter queue.

0
votes

In order to avoid this using queues you can use the session functionality of the Azure Service Bus.

You need to create a session aware queue first of all before you do anything else, which can be done as follows.

// Create a queue with duplicate detection
// with a detection history window of one hour,
// and requires sessions.

QueueDescription rfidCheckoutQueueDescription = new QueueDescription("rfidcheckout")
{

    RequiresSession = true,

    RequiresDuplicateDetection = true,

    DuplicateDetectionHistoryTimeWindow = new TimeSpan(0, 1, 0)

};

// Create a queue that supports duplicate detection.
namespaceMgr.CreateQueue(rfidCheckoutQueueDescription);

When the session aware queue is setup you must set the SessionId property for a message and recieve the message using MessageSession instead of QueueClient.

Some code samples can be found for this: Sending Messages, Receiving Messages.