Azure Functions Developers

Build and run event-driven applications without explicit infrastructure management.

Azure Queue Storage Trigger

The Queue trigger allows you to run a function in response to messages appearing in an Azure Queue Storage queue. When a message is added to the queue, the trigger invokes your function, passing the message content as input.

When to Use a Queue Trigger

  • Processing messages asynchronously from a queue.
  • Implementing background tasks that don't require an immediate response.
  • Decoupling different parts of an application.
  • Handling variable workloads by scaling functions based on queue depth.

How it Works

The Queue trigger periodically checks the specified queue for new messages. If messages are found, the trigger reads them and passes them to your function for processing. After your function successfully processes a message, the trigger automatically deletes it from the queue to prevent reprocessing. If your function throws an unhandled exception, the message will typically remain in the queue and can be processed by a retry or by a poison queue mechanism.

Configuration

The Queue trigger is configured using attributes in your code or within the function.json file.

Core Attributes (C# Example)


[FunctionName("QueueTriggerFunction")]
public static void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
    ILogger log)
{
    log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
}
                    
  • FunctionName: Defines the name of your function.
  • QueueTrigger:
    • The first argument is the name of the queue to monitor (e.g., "myqueue-items").
    • Connection: Specifies the name of an app setting that contains the connection string for the storage account. By default, it uses "AzureWebJobsStorage".
  • The function parameters typically include the message content (e.g., string myQueueItem) and an ILogger for logging.

Core Properties (function.json Example)


{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
                    
  • name: The name of the input variable in your function code.
  • type: Must be "queueTrigger".
  • direction: Must be "in" for a trigger.
  • queueName: The name of the queue to monitor.
  • connection: The name of an app setting containing the storage account connection string.

Programming Language Examples

C#


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

namespace Company.Function
{
    public static class QueueTriggerCSharp
    {
        [FunctionName("QueueTriggerCSharp")]
        public static void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log)
        {
            log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
        }
    }
}
                    

Node.js


module.exports = async function (context, myQueueItem) {
    context.log('JavaScript queue trigger function processed work item', myQueueItem);
};
                    

Python


import logging
import azure.functions as func

def main(msg: func.QueueMessage) -> None:
    logging.info('Python queue trigger function processed a queue item: %s',
                 msg.get_body().decode('utf-8'))
                    

Advanced Scenarios

  • Batching: You can configure the trigger to process multiple messages at once for improved efficiency.
  • Poison Queues: Implement strategies to move messages that fail processing repeatedly to a separate "poison" queue for investigation.
  • Customizing Message Processing: Access message metadata and configure retry policies.

Best Practices

  • Keep your message processing functions idempotent to ensure that reprocessing messages doesn't cause unintended side effects.
  • Use logging effectively to monitor function execution and troubleshoot errors.
  • Configure appropriate connection strings and monitoring for your queues.

For more details, refer to the official Azure Functions documentation.