gpt4 book ai didi

c# - 使用 Azure Function 队列为每个触发器设置 "batchSize"

转载 作者:行者123 更新时间:2023-12-03 01:58:43 24 4
gpt4 key购买 nike

我有一个带有多个队列触发器的 .NET Azure 函数。我配置了 host.json 文件(见下文),该文件一次最多使 4 条消息出队。

{
"queues": {
"maxPollingInterval": "00:00:03",
"visibilityTimeout": "00:00:30",
"batchSize": 4,
"maxDequeueCount": 5,
"newBatchThreshold": 2,
"messageEncoding": "base64"
}
}

这对于所有触发器来说都很好,除了我需要一次出队 1 条消息的触发器。

是否可以在每个触发器的基础上覆盖“batchSize”属性?我的替代方案是将此触发器拆分为它自己的函数。

根据我的研究,一些人认为这在functions.json 文件中可能是可能的。但我没有functions.json 文件,所以不知道从哪里开始。

最佳答案

如果您想为特定队列触发器使不同的消息出队,您需要创建两个具有不同批量大小的单独函数,如下所示:-

我的消息队列:-

队列 1:

enter image description here

队列2:

enter image description here

代码:

function1.cs:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class Function1
{
[FunctionName("QueueTriggerFunction1")]
public static void Run(
[QueueTrigger("kaqueue1", Connection = "AzureWebJobsStorage")] string myQueueItem,
ILogger log)
{
log.LogInformation($"Function1 processed: {myQueueItem}");
}
}

Function1/host.json:

{
"queues": {
"maxPollingInterval": "00:00:03",
"visibilityTimeout": "00:00:30",
"batchSize": 4,
"maxDequeueCount": 5,
"newBatchThreshold": 2,
"messageEncoding": "base64"
}
}

Function2/Function2.cs:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class Function2
{
[FunctionName("QueueTriggerFunction2")]
public static void Run(
[QueueTrigger("kaqueue2", Connection = "AzureWebJobsStorage")] string myQueueItem,
ILogger log)
{
log.LogInformation($"Function2 processed: {myQueueItem}");
}
}

Function2/host.json:

{
"queues": {
"maxPollingInterval": "00:00:03",
"visibilityTimeout": "00:00:30",
"batchSize": 1,
"maxDequeueCount": 5,
"newBatchThreshold": 2,
"messageEncoding": "base64"
}
}

local.settings.json:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<connec_string>",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}

输出:

enter image description here

enter image description here

或者,您可以在 Function.cs 中使用以下代码动态设置批量大小:

消息队列:

enter image description here

代码:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
public static class MyQueueFunction
{
[FunctionName("MyQueueFunction")]
public static void Run([QueueTrigger("kaqueue", Connection = "AzureWebJobsStorage")] string message,
int dequeueCount,
ILogger log)
{
int batchSize = 4;
{
batchSize = 1;
}
log.LogInformation($"Processing message: {message} with batch size: {batchSize}");
for (int j = 0; j < batchSize; j++)
{
}
}
}
}

输出:

enter image description here

关于c# - 使用 Azure Function 队列为每个触发器设置 "batchSize",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77243791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com