[英]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.