Ryan
Ryan

Reputation: 7959

What good is a QueueTriggerAttribute when you can't dequeue the exact queued message? Or can you?

I'm trying to understand something about the Azure Functions' QueueTriggerAttribute for use with the Queue Storage. I can see that the QueueTriggerAttribute allows me to bind an Azure Function to a Queue Storage event for when a new item is added to the queue - my Function calls with the contents of that new message.

How is this useful, though?

There is still no way to dequeue that exact Queue Storage item within that triggered Function, right? The best that you could do is just pop whatever the next available item is off of the Queue, which may not be the one that triggered the Function.

I guess in theory 1 single push to the Queue Storage would trigger 1 single Function call in which you could make 1 single pop call. So at the end of the day you still can leverage these triggers to process all of the items in the queue - so long as there is no interruptions or anything that would cause a trigger to go unhandled, resulting in items stuck in the queue.

Am I missing something here? I'm looking at Queue Storage in conjunction with Azure Functions and a QueueTrigger. I'm trying to conceptualize a queue-driven workflow that executes functions, but I feel like this doesn't seem correct - or I'm not understanding something here.

Upvotes: 0

Views: 471

Answers (1)

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35134

It seems you are understanding something wrong, but I'm not exactly sure what your confusion is.

When you send 1 single message to a Queue, the Function listening to that queue will fire, and the contents of that message will be passed as input parameter to the code that you wrote.

Your code needn't and shouldn't "pop" anything from the queue explicitly - this is done behind the scenes by Azure Functions runtime. One invocation will process just that single message and will quit as soon as possible.

Look at this code:

[FunctionName("QueueTrigger")]
public static void QueueTrigger(
    [QueueTrigger("myqueue-items")] string myQueueItem, 
    TraceWriter log)
{
    log.Info($"C# function processed: {myQueueItem}");
}

Apart from attributes, no code works with the Queue. It just gets triggered, once per message.

Upvotes: 3

Related Questions