6

I have an issue with Azure Function Service Bus trigger. The issue is Azure function cannot wait a message done before process a new message. It process Parallel, it not wait 5s before get next message. But i need it process sequencecy (as image bellow). How can i do that?

[FunctionName("HttpStartSingle")]
    public static void Run(
 [ServiceBusTrigger("MyServiceBusQueue", Connection = "Connection")]string myQueueItem,
[OrchestrationClient] DurableOrchestrationClient starter,
ILogger log)
    {
        Console.WriteLine($"MessageId={myQueueItem}");
        Thread.Sleep(5000);
    }

enter image description here

TuanDPH
  • 461
  • 5
  • 14
  • Take a look [here](https://stackoverflow.com/questions/50249832/processing-service-bus-messages-in-order-without-concurrent-calls-in-an-azure) – Dimi Takis May 31 '19 at 10:16

3 Answers3

7

I resolved my problem by using this config in my host.json

{
"version": "2.0",
"extensions": {
    "serviceBus": {
        "messageHandlerOptions": {
            "maxConcurrentCalls": 1
        }
    }
}}
Ameya Deshpande
  • 3,580
  • 4
  • 30
  • 46
TuanDPH
  • 461
  • 5
  • 14
0

There are two approaches you can accomplish this,

(1) You are looking for Durable Function with function chaining

For background jobs you often need to ensure that only one instance of a particular orchestrator runs at a time. This can be done in Durable Functions by assigning a specific instance ID to an orchestrator when creating it.

(2) Based on the messages that you are writing to Queue, you need to partition the data, that will automatically handle the order of messages which you do not need to handle manually by azure function

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • It still not wait a message finish before process new message. My updated code in my question. Please review it if i wrong in somewhere – TuanDPH May 31 '19 at 10:18
  • https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-sequence – Sajeetharan May 31 '19 at 10:22
0

In general, ordered messaging is not something I'd be striving to implement since the order can and at some point will be distorted. Saying that, in some scenarios, it's required. For that, you should either use Durable Function to orchestrate your messages or use Service Bus message Sessions.

Azure Functions has recently added support for ordered message delivery (accent on the delivery part as processing can still fail). It's almost the same as the normal Function, with a slight change that you need to instruct the SDK to utilize sessions.

public async Task Run(
  [ServiceBusTrigger("queue", 
   Connection = "ServiceBusConnectionString",
   IsSessionsEnabled = true)] Message message, // Enable Sessions
   ILogger log)
{
    log.LogInformation($"C# ServiceBus queue trigger function processed message: {Encoding.UTF8.GetString(message.MessageId)}");
    await _cosmosDbClient.Save(...);
}

Here's a post for more detials.

Warning: using sessions will require messages to be sent with a session ID, potentially requiring a change on the sending side.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80