Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure function service bus trigger: How to stop batches of data from event stream and only allow one message from the queue at a time?

This is my first time using Azure functions and service bus so please forgive my ignorance.

I'm trying to build a function app in visual studio and i am able to connect to the queue topic (which I don't control). The problem is that every time I enable a breakpoint, 10s of messages are processed at once in VS which is making local testing very difficult (not to mention the problems arising from database pools).

How do I ensure that only 1 message gets processed at once until I hit complete?

public static void Run([ServiceBusTrigger("xxx", "yyy", AccessRights.Manage)]BrokeredMessage msg, TraceWriter log)
{
     // do something here for one message at a time.
}
like image 366
90abyss Avatar asked Jan 25 '18 02:01

90abyss


3 Answers

Set maxConcurrentCalls to 1 in the host.json. You also could get answer from host.json reference for Azure Functions

maxConcurrentCalls 16 The maximum number of concurrent calls to the callback that the message pump should initiate. By default, the Functions runtime processes multiple messages concurrently. To direct the runtime to process only a single queue or topic message at a time, set maxConcurrentCalls to 1

like image 164
Tom Sun - MSFT Avatar answered Sep 26 '22 08:09

Tom Sun - MSFT


In case someone stumbles on this question in the future, for function apps 2.0 you need to to update the host.json like this:

{
  "version": "2.0",
  ...
  "extensions": {
    "serviceBus": {
      "messageHandlerOptions": {
        "maxConcurrentCalls": 1
      }
    }
  }
}
like image 45
Francois du Plessis Avatar answered Sep 22 '22 08:09

Francois du Plessis


Since the host.json file is usually published to Azure along with the function itself, it is preferable not to modify it for debugging and development purposes. Any change to host.json can be made to local.settings.json instead.

Here is how to set maxConcurrentCalls to 1:

{
  "IsEncrypted": false,
  "Values": {
    ...
    "AzureFunctionsJobHost:Extensions:ServiceBus:MessageHandlerOptions:MaxConcurrentCalls": 1
  }
}

This override functionality is described here: https://docs.microsoft.com/en-us/azure/azure-functions/functions-host-json#override-hostjson-values

like image 41
Björn Jarisch Avatar answered Sep 22 '22 08:09

Björn Jarisch