In this post I discussed receiving, processing and acknowledging a message using the Azure Service Bus. There are two ways to acknowledge a message received from the queue (which are common to all message broker systems that I’ve used so far). That is, you either take the message, process it, and then go back to the broker to tell it you’re done (explicit acknowledgement); or, you remove the queue and then process it (implicit acknowledgement).
Explicit Acknowledgement / PeekLock
If the message is not processed within a period of time, then it will be unlocked and returned to the queue to be picked up by the next client request.
The code for this is as follows (it is also the default behaviour in Azure Service Bus):
QueueClient queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName, ReceiveMode.PeekLock);
Remember that, with this code, if you don’t call:
message.Complete();
Then you will repeatedly read the same message over and over again.
Implicit Acknowledgement / ReadAndDelete
Here, if the message is not processed within a period of time, or fails to process, then it is likely lost. So, why would you ever use this method of acknowledgement? Well, speed is the main reason; because you don’t need to go back to the server, you potentially increase the whole transaction speed; furthermore, there is clearly work involved for the broker in maintaining the state of a message on the queue, expiring the message lock, etc.
The code for the implicit acknowledgement is:
QueueClient queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName, ReceiveMode.ReceiveAndDelete);
References
https://docs.microsoft.com/en-us/rest/api/servicebus/peek-lock-message-non-destructive-read