Azure Service Bus Topic Receiving messages - azure

Referring to https://github.com/Azure/azure-service-bus/tree/master/samples/DotNet/GettingStarted/Microsoft.Azure.ServiceBus/BasicSendReceiveUsingTopicSubscriptionClient, I understand how Azure Service Bus Topics work in general, my question is more about how it actually works.
When a MesageHandler is registered (subscriptionClient.RegisterMessageHandler), it starts receiving messages as I see in
Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
However, my question is whether the client actually receives the messages using a pull mode or it is a push from the Service Bus? Is there a continuous polling done by the Client to receive the messages - how does this work internally?

The client is performing a long-poll. I.e. it will ask for a message and wait for it. If after a timeout period of one minute nothing is returned, it will poll again. In case a message is available before timeout expires, the message will be given to the message handler and polling will start again. Azure Service Bus does not push messages to the clients.

Related

How can I make the "Azure Service Bus" message be handled by a single server instance from the instance pool?

I have an Asp.Net core Web Api running in the Azure App Service.
I also want to make a BackgroundWorker that will periodically browse the database. And if there are necessary records send a message via Azure Bus to WebApi, WebApi should send notifications to mobile devices when receiving the message.
In case there is only one instance of WebApi, there is no question. But autoscaling is enabled. That is, with a high load there can be a lot of WebApi. And when a message is sent by BackgroundWorker , it will be received by many services, and repeatedly duplicate sending notification to the user.
Is there already a ready mechanism which is used in such situations? A lot of subscribers to the message, all of them can receive it, but if one received it, no one else should receive it.
There are two Recieve modes:
ReceiveAndDelete
PeekLock
Both will work in your case, in that the first consumer to get the message, gets it, and the others will not see it.
The difference is in what happens if something crashes. With ReceiveAndDelete the message is lost and will not be processed. With PeekLock depending on how far it got before the crash, the message may be processed twice.
https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions

How can a ServiceBus guarantee the FIFO order if the receiving Azure Function stopped?

I have a client that pushes messages into a Service Bus. I
have an Azure Function on the other side that is bound to the Service Bus so that it is automatically called when a message is posted in the Service Bus.
(Client) ---Message---> (Service Bus) ---Message---> (Azure Function)
The Service Bus guarantees that the first message in the queue will be the first to get out (FIFO).
Now, I assume that, under a certain load, several Azure Functions will be started to process the messages from the Service Bus.
Let's say one of these Azure Functions crashes (for whatever reasons) and is unable to process a message.
Question
Does the Service Bus put back the unprocessed message back in the first place and does that mean that, technically, even if this message was the first in the queue, it can indeed be processed after a more recent message?
You need to properly set the SessionID to guarantee order. For session-aware entities, this application-defined value specifies the session affiliation of the message. Messages with the same session identifier are subject to summary locking and enable exact in-order processing and demultiplexing. For session-unaware entities, this value is ignored. See Message Sessions.
https://learn.microsoft.com/en-us/azure/service-bus-messaging/message-sessions

How to configure Azure Service Bus Queues to push messages to clients without polling?

I am new to Azure Service Bus and I found that I need to continuously poll for Queue messages in order to receive them. However, I want the Queue to push the message to some sort of listener on the client-side of things without it having to poll for messages.
I have read that polling is optional in Azure Service Bus but I couldn't find how to receive messages without it.
Please help if you can. Thank you
I have read that polling is optional in Azure Service Bus but I couldn't find how to receive messages without it.
Polling is optional if you start receiving on demand. Otherwise, it's not optional and long-polling will take place.
I want the Queue to push the message to some sort of listener on the client-side of things without it having to poll for messages.
There's a way to achieve exactly that using Event Grid integration with Azure Service Bus. Service Bus will emit an event to notify about messages awaiting processing and no active listeners. That way your application/system reacts and doesn't have to poll.
Note that the key scenario for this feature is low volume of messages that do not need to have a receiver that polls for messages continuously.
It should be possible. Take a look on the following to get an idea on how it could be done:
Get started with Service Bus topics
Get started with Service Bus queues
Hope it helps!

Processing Service Bus Queue Messages One-By-One

I've relatively new to Azure Service Bus Queues and am building a project that needs to process messages in a queue in the order in which they arrive (FIFO).
Using Microsoft's documentation, I was able to figure that part out. From what I understand I need to turn sessions on for the queue?
What I'm struggling with is determining what would be the best approach / service stack to perform the following set of ordered tasks against the queue.
First let's assume that we have a FIFO-based Service Bus Queue in place that has n number of messages. How might I:
Pick up the first message from the queue.
Process the message using an Azure Function.
Send a payload to IoT Hub that will deliver to an external device (C2D).
This is the part that I can't figure out...Wait for either a Completed indicator to return from IoT Hub or wait until the TTL expires for the outgoing IoT Hub Message.
Now complete the item in the queue.
Start back at 1.
I would imagine that perhaps a Logic App might help me achieve what I'm attempting to do. Everything seems straight forward up until #4. I can't figure out how to have the logic app 1. Wait until IoT Hub acknowledges that the Cloud-to-Device message was sent or expired and 2. Don't process the next message in the queue until the IoT Hub acknowledgment has been received and I've marked the current queue message as complete.
Please be advised: The reason why I'm so specific about this is that the devices receiving the outgoing IoT Hub C2D messages care about order. If they receive messages out of order, it throws the process off.
Any suggestions is greatly appreciated.
Azure Service bus Queues with Session can be used for achieving ordered processing of the message.
From the question, I can understand that the message from the Service Bus Queue should be removed only after the acknowledgement is received from IOT Hub.
I can sense a problem that may happen with this flow.
First of all, you need to understand about lock duration property in Service Bus Queue. Based on the value set to this property, the messages will be locked for x minutes or seconds. Whenever a message is received from a Queue, a lock will be applied for the message. So that the message will not be available for any other receivers for x minutes or seconds. Complete operation should happen before the lock gets expired. Once the lock is expired, the message will be available for other receivers to process. In your case, there is a chance for the same Azure Function to receive the message once again (resulting in duplication)
The maximum value that can be set for the lock duration property is 5 minutes.
So, if you are sure that the IOT Hub responds with in 5 minutes, you can proceed with this implementation. Still, there will be no option to let the Function know when to process the next message.

Azure web job, service bus trigger not triggered for old messages available in the queue

I have implemented the azure web job to consume messages from azure service bus. Service bus trigger works fine for new messages arrived to service bus but its not picking up the messages that were already available on the service bus before service start. Is there any configuration to get existing messages to the web job?
Due to some reason, some messages got abandon. What are the best ways to reprocess these messages.
If you get messsage and can't complete or abandon it 10 times(default), the message will go dead queue.
You can get dead queue like this.
_messagingFactory.CreateQueueClient(QueueClient.FormatDeadLetterPath(QueueDescription.Path), ReceiveMode.PeekLock);

Resources