Azure Queue Storage Triggered-Webjob - 一次出列多条消息

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

我有一个 Azure 队列存储触发的 Web 作业。我的 webjob 执行的过程是将数据索引到 Azure 搜索中。出于性能原因,Azure 搜索的最佳做法是将多个项目一起索引而不是一次索引一个项目(索引可能需要一些时间才能完成)。

出于这个原因,我希望我的 webjob 将多条消息一起出队,这样我就可以遍历、处理它们,然后将它们一起索引到 Azure 搜索中。

但是我不知道如何让我的 webjob 一次出列多个。如何实现?

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.

根据你的描述,我建议你可以尝试使用Microsoft.Azure.WebJobs.Extensions.GroupQueueTrigger来达到你的要求。

此扩展程序将使您能够触发功能并接收消息组,而不是像 [QueueTrigger] 那样接收单个消息。

更多细节,您可以参考下面的代码示例和article

安装:

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

结果:


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

在我看来,这是一个简单的改变。

您可以按照以下步骤操作:

1.Install 来自 Nuget 包管理器的包 Microsoft.Azure.WebJobs.Extensions.GroupQueueTrigger。

2.Change program.cs 文件启用 UseGroupQueueTriggers。

3.Change 网络作业根据您的旧触发函数运行。

注意:群队列消息触发器必须使用列表。

如我的代码示例所示:

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

此函数一次将从 "queue3" 获取 10 条消息,因此在此函数中,您可以更改消息列表的函数循环并处理它们,然后将它们一起索引到 Azure 搜索中。

4.Publish 您的 web 作业到 azure web 应用程序。