Azure Queue Storage Triggered-Webjob - Dequeue Multiple Messages at a time

一世执手 提交于 2021-02-07 20:01:29

问题


I have an Azure Queue Storage-Triggered Webjob. The process my webjob performs is to index the data into Azure Search. Best practice for Azure Search is to index multiple items together instead of one at a time, for performance reasons (indexing can take some time to complete).

For this reason, I would like for my webjob to dequeue multiple messages together so I can loop through, process them, and then index them all together into Azure Search.

However I can't figure out how to get my webjob to dequeue more than one at a time. How can this be accomplished?


回答1:


For this reason, I would like for my webjob to dequeue multiple messages together so I can loop through, process them, and then index them all together into Azure Search.

According to your description, I suggest you could try to use Microsoft.Azure.WebJobs.Extensions.GroupQueueTrigger to achieve your requirement.

This extension will enable you to trigger functions and receive the group of messages instead of a single message like with [QueueTrigger].

More details, you could refer to below code sample and article.

Install:

Install-Package Microsoft.Azure.WebJobs.Extensions.GroupQueueTrigger

Program.cs:

  static void Main()
        {
            var config = new JobHostConfiguration
            {
                StorageConnectionString = "...",
                DashboardConnectionString = "...."
            };
            config.UseGroupQueueTriggers();
            var host = new JobHost(config);
            host.RunAndBlock();
        }

Function.cs:

 //Receive 10 messages at one time
 public static void MyFunction([GroupQueueTrigger("queue3", 10)]List<string> messages)
        {
            foreach (var item in messages)
            {
                Console.WriteLine(item);
            } 
        }

Result:


How would I get that changed to a GroupQueueTrigger? Is it an easy change?

In my opinion, it is an easy change.

You could follow below steps:

1.Install the package Microsoft.Azure.WebJobs.Extensions.GroupQueueTrigger from Nuget Package manager.

2.Change the program.cs file enable UseGroupQueueTriggers.

3.Change the webjobs functions according to your old triggered function.

Note:The group queue message trigger must use list.

As my code sample shows:

 public static void MyFunction([GroupQueueTrigger("queue3", 10)]List<string> messages)

This function will get 10 messages from the "queue3" one time, so in this function you could change the function loop the list of the messages and process them, then index them all together into Azure Search.

4.Publish your webjobs to azure web apps.



来源:https://stackoverflow.com/questions/44378877/azure-queue-storage-triggered-webjob-dequeue-multiple-messages-at-a-time

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