简体   繁体   English

如何在没有队列触发器的情况下从 Azure function 中的 Azure 队列读取

[英]How to read from an Azure queue in an Azure function, without a Queue Trigger

If I have an Azure Function that produces data for a queue, it's very simple: I just set it up as a parameter to the function:如果我有一个 Azure Function 为队列生成数据,这很简单:我只需将它设置为 function 的参数:

[Queue("myQueue")] ICollector<MyType> myQueue

Is there any analogous way to read data back out of the queue?有没有类似的方法从队列中读回数据? All the information I can find on reading from queues in Azure Functions talks about Queue Triggers, which is not what I'm trying to do;我在 Azure 函数中从队列中读取时可以找到的所有信息都在谈论队列触发器,这不是我想要做的; I want a timer-triggered function that will batch-process elements from a queue.我想要一个定时器触发的 function 来批处理队列中的元素。 How do I get a "queue reader" in my function?如何在我的 function 中获得“队列阅读器”?

It sounds like you want a dequeuing process instead of a triggering process.听起来您想要一个出队过程而不是触发过程。 Try this example demonstrated in the Microsoft docs.试试 Microsoft 文档中演示的这个示例。

https://learn.microsoft.com/en-us/azure/storage/queues/storage-tutorial-queues?tabs=do.net%2Cenvironment-variable-windows#dequeue-messages https://learn.microsoft.com/en-us/azure/storage/queues/storage-tutorial-queues?tabs=do.net%2Cenvironment-variable-windows#dequeue-messages

Extracted below摘录如下

static async Task<string> RetrieveNextMessageAsync(QueueClient theQueue){
if (await theQueue.ExistsAsync())
{
    QueueProperties properties = await theQueue.GetPropertiesAsync();

    if (properties.ApproximateMessagesCount > 0)
    {
        QueueMessage[] retrievedMessage = await theQueue.ReceiveMessagesAsync(1);
        string theMessage = retrievedMessage[0].Body.ToString();
        await theQueue.DeleteMessageAsync(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);
        return theMessage;
    }

    return null;
}

return null;
}

It looks like you can also adjust the max batch size as well.看起来您也可以调整最大批量大小。

I read your question as if you need a timer triggered fuction , to take messages (dequeue) of an queue.我读了你的问题,就好像你需要一个定时器触发的功能,来获取队列的消息(出队)。

So you need to create an timer triggred function like so:所以你需要像这样创建一个定时器触发 function :

[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
    DequeueMessage("nyConnection");
}

That then calls your function to dequeue a message of the queue like so from the docs然后调用您的 function 从文档dequeue一条队列消息

public void DequeueMessage(string queueName)
{
    // Get the connection string from app settings
    string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

    // Instantiate a QueueClient which will be used to manipulate the queue
    QueueClient queueClient = new QueueClient(connectionString, queueName);

    if (queueClient.Exists())
    {
        // Get the next message
        QueueMessage[] retrievedMessage = queueClient.ReceiveMessages();

        // Process (i.e. print) the message in less than 30 seconds
        Console.WriteLine($"Dequeued message: '{retrievedMessage[0].Body}'");

        // Delete the message
        queueClient.DeleteMessage(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure函数队列触发器 - Azure function Queue Trigger Azure Service Bus和Azure功能队列触发器 - Azure Service Bus and Azure Function Queue Trigger 如何从 Azure 存储队列的消息文本字段中读取 JSON 字符串(使用 Azure 函数)? - How do I read a JSON string from the Message Text field in an Azure Storage queue (using an Azure function)? Azure Function 队列触发器添加 Nuget Z209802FB858E2C836205027DBBB5D9 - Azure Function Queue Trigger Adding Nuget Package 在Azure函数中使用BrokeredMessage和ServiceBus队列触发器 - Using BrokeredMessage with ServiceBus Queue Trigger in Azure Function Azure 函数 - 队列触发器将 DateTimeOffset 返回为 null - Azure Function - Queue Trigger returns DateTimeOffset as null 如何在队列触发器中的azure函数中获取主机URL作为输入? - How to get host url in azure function in queue trigger as input? 从 Azure Function 中取出 Azure 存储队列 - Dequeue Azure Storage queue from Azure Function 从队列触发器启动Azure Worker角色 - Launch Azure Worker Role from Queue Trigger Azure 队列触发器 function 应用程序如何仅在完成另一个队列消息后选择消息 - how Azure queue trigger function app can pick message after completion of another queue message only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM