I need to read and process messages from an Azure Service Bus Queue by an "Azure function". The messages should be handled in the right order so I need to avoid concurrent calls.
I use an Azure Function service bus trigger for this (it's the only subscriber to the queue). According to the documentation I configured the "servicebus/maxConcurrentCalls" (in host.json) setting to 1. On top of this I decorated the function with the "Singleton" attribute. Besides all this the messages seem to be treated in a random order by different threads. What do I miss here? Or did I misunderstand something?
Documentation I used: https://github.com/Azure/azure-webjobs-sdk/wiki/Singleton
host.json:
{
"serviceBus": {
"maxConcurrentCalls": 1
}
}
Azure Function:
using System;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;
[Singleton]
public static void Run(BrokeredMessage myQueueItem, TraceWriter log)
{
Stream stream = myQueueItem.GetBody<Stream>();
StreamReader reader = new StreamReader(stream);
string messageContentStr = reader.ReadToEnd();
log.Info($"New TEST message: {messageContentStr} on thread {System.Threading.Thread.CurrentThread.ManagedThreadId}");
System.Threading.Thread.Sleep(2000);
}
Here is an excerpt of the logging. As you can see there are different threads. And, for example, "Message 19" comes before "Message 10". And yes, I'm sure I put the messages in the right order in the queue.
....
2018-05-09T09:09:33.686 [Info] New TEST message: Message 19 on thread 33
2018-05-09T09:09:35.702 [Info] Function completed (Success, Id=007eccd0-b5db-466a-91c1-4f53ec5a7b3a, Duration=2013ms)
2018-05-09T09:09:36.390 [Info] Function started (Id=b7160487-d10d-47a6-bab3-78da68a93498)
2018-05-09T09:09:36.420 [Info] New TEST message: Message 10 on thread 39
...