Queue Triggers
Queue triggers allow you to create Azure Functions that execute in response to messages being added to an Azure Storage Queue or an Azure Service Bus Queue.
When to Use Queue Triggers
Queue triggers are ideal for implementing background processing, asynchronous workflows, and decoupling applications. When a message appears in a monitored queue, your function automatically runs to process that message.
Supported Queue Providers
- Azure Storage Queues: A simple, cost-effective queueing service.
- Azure Service Bus Queues: Offers more advanced features like transactions, duplicate detection, and session support.
Configuring a Queue Trigger
A queue trigger is defined by the {queueName} binding expression and connection string settings.
Azure Storage Queue Trigger Example (C#)
This example shows a C# function triggered by messages in a storage queue named myqueue-items.
using System;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace MyCompany.MyFunctions
{
    public class QueueTriggerCSharp
    {
        private readonly ILogger _logger;
        public QueueTriggerCSharp(ILogger logger)
        {
            _logger = logger;
        }
        [Function("QueueTriggerCSharp")]
        public void Run(
            [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem)
        {
            _logger.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
            // Add your message processing logic here
            // For example, parse the message, update a database, send an email, etc.
        }
    }
}
              Azure Service Bus Queue Trigger Example (JavaScript)
This example shows a JavaScript function triggered by messages in a Service Bus queue named my-servicebus-queue.
module.exports = async function (context, mySbMsg) {
    context.log('JavaScript ServiceBus queue trigger function processed work item', mySbMsg);
    context.log(`body is: ${mySbMsg.body}`);
    context.log(`message id is: ${mySbMsg.messageId}`);
    // Add your message processing logic here
};
            Connection Strings
The Connection property in the attribute (e.g., "AzureWebJobsStorage") refers to an application setting that contains the connection string for the queue service. For Azure Storage Queues, this is typically the default connection string pointing to your Storage Account.
For Service Bus, you'll need to configure a specific connection string setting, such as ServiceBusConnectionString.
Important Note on Processing
Queue triggers process messages one at a time by default. If a function execution fails, the message will typically remain in the queue to be retried later (depending on your queue configuration and retry policies). For critical operations, consider implementing idempotency in your function to handle potential duplicate message processing.
Key Concepts
- Queue Name: The name of the queue to monitor.
- Connection: The name of the application setting that holds the queue connection string.
- Message Content: The data from the queue message is passed to your function. The type depends on the language and runtime.
- Poison Queue Handling: For Storage Queues, failed messages can be moved to a "poison queue" after a configurable number of retries to prevent blocking the main queue.
Advanced Scenarios
- Batching: Some runtimes and bindings support processing multiple messages in a single function invocation for increased efficiency.
- Custom POCOs: You can define custom Plain Old C# Objects (POCOs) to deserialize the queue message content for type-safe processing.
- Service Bus Session Support: For Service Bus queues that use sessions, your function can be configured to process messages within a session.