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

末鹿安然 提交于 2021-02-05 09:42:58

问题


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();
        }

回答1:


By default, ServiceBusTrigger is using PeekLock mode. This technically means a message can be processed more than once if the message lock has been locked. This would explain why for 1,000 incoming messages you get more than 1,000 invocations.

Another angle would be the duration of your function execution combined with the prefetch size. If the time it takes to execute is long enough to lose the lock on some of the prefetched messages, decrease the prefetch.

Also, consider designing your solution in a way where you know a message is processed at-least-once, not exactly-once.




回答2:


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


来源:https://stackoverflow.com/questions/64901015/azure-function-service-bus-trigger-fires-more-times-than-messages-in-the-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!