90abyss
90abyss

Reputation: 7347

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.

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.
}

Upvotes: 14

Views: 4477

Answers (3)

Francois du Plessis
Francois du Plessis

Reputation: 2590

For function apps 2.0 you need to to update the host.json like this:

{
  "version": "2.0",
  ...
  "extensions": {
    "serviceBus": {
      "messageHandlerOptions": {
        "maxConcurrentCalls": 1
      }
    }
  }
}

Upvotes: 8

Björn Jarisch
Björn Jarisch

Reputation: 345

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://learn.microsoft.com/en-us/azure/azure-functions/functions-host-json#override-hostjson-values

Upvotes: 3

Tom Sun
Tom Sun

Reputation: 24569

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

Upvotes: 10

Related Questions