Message Pump in Azure Service Bus
Despite the assumptions that an Azure Service Bus Queue must be polled and then dequeued manually for messages in client code, there is a tiny and neat way to automatically dequeue messages as soon as they arrive. It is an alternative to setting up a Service Bus Triggered Azure Function which would have the same effect. We call this automatic method to respond like this a Message Pump, as it is an event driven way to pick our message without writing additional polling logic.
Here is a quick example of this:
And as we push messages from message producers, they get automatically dequeued in our client code. However it's important to remember that if you choose to set the messageHandlerOptions AutoComplete flag to False, you will need to Complete each message with:
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
Completing the message, either through the AutoComplete flag or through manually Completing it from the queue client will make sure it is not received again by your client if it is sent again. Here, I use the Service Bus Explorer in Azure (from the Service Bus instance's menu) to easily send messages through to the Service Bus Queue:
To conclude, it should be remembered that queue systems in the cloud are generally geared to assist us in load levelling. Often times, we want to lighten the load on our processing systems by delaying when messages can be process and allow them to sit in the queue for their Time To Live period rather than processing them as soon as we receive them in the queue.
Once finished, remember to DELETE any unused resources in Azure.