Home > Net >  Way to consume bulk messages from Service Bus Queue instead of triggering it for each message
Way to consume bulk messages from Service Bus Queue instead of triggering it for each message

Time:04-25

We have a Service Bus Queue; we are thinking to handle messages through Service Bus Queue Trigger in the Azure Function. But we want to consume bulk messages (in a loop) instead of re-triggering Function App for each message. What should be the way to achieve this and by using which handler. Should Azure Function be only used or we can host an App Service for consuming the same.

CodePudding user response:

You can loop the multiple messages using IAsyncCoollector:

for (var i = 0; i < 10; i  )
{
   var message = new ServiceBusMessage($"Message #{i}");
   await collector.AddAsync(serviceBusMessage);
}

This is for in-Process SDK. Implementation is based on the SDK you're using when sending multiple messages with Functions. Refer this blog for more information.

Official doc of Service Bus Binding the batch configuration in host.json to Azure Functions shows the required configuration for sending and receiving the batch messages.

Refer to this GitHub Issues (Issue 921) which contains the few resolutions of issues arrived in Azure Functions Service Bus Bindings.

CodePudding user response:

You should use batches. Simply declare the variable as an array.

public static class ProcessOrders
{
    [FunctionName("ProcessOrders")]
    public static void Run(
        [ServiceBusTrigger("orders", Connection = "ServiceBusConnection")]
        Message[] orders, // <-- array
        ILogger log)
    {
        log.LogInformation($"Number of orders: {orders.Length}");
    }
}

You can further configure batch size and such using the host.json file.

  • Related