Search code examples
azure-functionsazureservicebusazure-servicebus-queuesazure-servicebus-topics

Azure Function Service Bus trigger fires more times than messages in the queue


I have two Service Bus Queues and Azure Service Bus Queue Trigger.

The function reads 1000 messages from the queue and forwards them to another (for test) When publishing to Azure (plan consumption) I get 1030+ function hits even though I only have 1000 messages in the queue. I think this has to do with the number of function instances.

Q: How to process 1 unique message only once for 1 function instance?

Queue message count image

[FunctionName("Function1")]
public static async Task Run([ServiceBusTrigger("export-queue",
    Connection = "Connection")]
    string myQueueItem, ILogger log)
    {
        log.LogInformation($"Message: {myQueueItem}:{DateTime.Now}");
        Thread.Sleep(2000);
        queueClient = new QueueClient(ServiceBusConnectionString, QueueName);

        var message = new Message(Encoding.UTF8.GetBytes(myQueueItem));
        await queueClient.SendAsync(message);
        await queueClient.CloseAsync();
    }

Solution

  • It works for me.

    Add extensions for your host.json :

    "extensions": {
    
        "serviceBus": {
          "prefetchCount": 1,
          "messageHandlerOptions": {
            "autoComplete": true,
            "maxConcurrentCalls": 1,
            "maxAutoRenewDuration": "00:05:00"
          },
          "sessionHandlerOptions": {
            "autoComplete": true,
            "messageWaitTimeout": "00:00:30",
            "maxAutoRenewDuration": "00:55:00",
            "maxConcurrentSessions": 10
          }
        }
      }