簡體   English   中英

ServiceBusTrigger: session 一次接收一條消息

[英]ServiceBusTrigger: session receiving one message at a time

我有一個帶有 ServiceBusTrigger 觸發器的 Azure Function

它被配置為接收會話,當我插入消息時,它插入了正確的“SessionId”。

但是當執行觸發器時,它一次只執行一條 session 消息。

有人能幫我嗎? 我想一次運行所有 session 條消息。

我在下面留下了關於插入消息和觸發器的代碼片段

服務總線觸發器:

public async Task RunAsync([ServiceBusTrigger("%" + AzureServiceBusConfiguration.ServiceBusBlobMigrationQueueNameSecretName + "%", Connection = AzureServiceBusConfiguration.ServiceBusConnectionSecretName, IsSessionsEnabled = true)] string messageBrokerMessageString, FunctionContext context)
{
//CODE
}

留言插入:

    for (int chunkIndex = 0; chunkIndex < chunks.Count(); chunkIndex++)
    {
        string workerNodeName = _workerManager.GetWorkerNodeName(controlMetadataEntity.GetBlobMigrationStrategyIdentifier(), Convert.ToUInt32(chunkIndex), controlMetadataEntity.RowKey);

        foreach (BlobMetadataEntity blob in chunks.ElementAt(chunkIndex))
        {
            ServiceBusMessage message = new ServiceBusMessage()
            {
                // The message ID is necessary to prevent duplicate entries in message broker.
                // See https://learn.microsoft.com/en-us/azure/service-bus-messaging/duplicate-detection.
                MessageId = string.Join('|', blob.PartitionKey, blob.RowKey),

                // Body will be the JSON of a object converted to bytes.
                Body = new BinaryData(Encoding.UTF8.GetBytes(new BlobExecutionQueueMessage(blob.PartitionKey, blob.RowKey, workerNodeName, null).GetObjectAsJsonAsync())),
                ContentType = MediaTypeNames.Application.Json,

                // This will separate the message in logic queues to control the parallelism per execution.
                // See https://learn.microsoft.com/en-us/azure/service-bus-messaging/message-sessions.
                SessionId = workerNodeName
            };

            // Set user properties to identify this blob in message broker.
            message.ApplicationProperties.Add("ExecutionId", blob.PartitionKey);
            message.ApplicationProperties.Add("BlobId", blob.RowKey);

            // Add the "AddAsync" task to the list of tasks to run them after all
            // messages were enqueued in the list.
            messageBrokerInsertions.Add(_azureServiceBusClient.SendMessageAsync(serviceBusClient, sender, message));
        }
    }

我的主機.json

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingExcludedTypes": "Request",
            "samplingSettings": {
                "isEnabled": true
            }
        },
        "logLevel": {
            "Default": "Information"
        }
    },
    "extensions": {
        "durableTask": {
            "maxConcurrentActivityFunctions": 500,
            "maxConcurrentOrchestratorFunctions": 500
        },
        "serviceBus": {
            "sessionHandlerOptions": {
                "maxConcurrentSessions": 1
            }
        }
    }
}

據我所知,這是為了尊重與消費者相關的會話順序而設計的,這有點棘手。 你可以增加->的數量
“最大並發會話”:16

這將並行運行它們。 此外,如果您正在執行消費計划,則不必擔心縮放問題,並且隊列長度會被計入因式縮放。 如果您不關心訂購,您可以切換到非 session 啟用並分批引入消息。 請注意此處的行:
https://learn.microsoft.com/zh-cn/azure/azure-functions/functions-bindings-service-bus?tabs=in-process%2Cextensionv5%2Cextensionv3&pivots=programming-language-powershell#hostjson-settings


“當您將觸發器上的 isSessionsEnabled 屬性或屬性設置為 true 時,sessionHandlerOptions 將被接受。當您將觸發器上的 isSessionsEnabled 屬性或屬性設置為 false 時,messageHandlerOptions 將被接受。”

留下一些參考鏈接以防萬一閱讀:
//隊列和主題
https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions


//最大並發會話
https://learn.microsoft.com/en-us/do.net/api/microsoft.servicebus.messaging.sessionhandleroptions.maxconcurrentsessions?view=azure-do.net#microsoft-servicebus-messaging-sessionhandleroptions-maxconcurrentsessions

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM