Azure Functions provides a Queue trigger that allows you to easily create functions that run in response to messages arriving on an Azure Storage Queue or an Azure Service Bus Queue.
How it Works
When a message is added to a configured queue, the Queue trigger automatically invokes your function. The function receives the message content and can perform actions based on it. The trigger handles polling the queue and processing messages.
Supported Queue Types
- Azure Storage Queue: A simple, cost-effective queuing service.
- Azure Service Bus Queue: Offers more advanced features like sessions, transactions, and dead-lettering.
Configuration
The queue trigger is configured in your function.json file (for JavaScript, Python, etc.) or via attributes/decorators (for C#, Java).
function.json Example (Azure Storage Queue)
{
"scriptFile": "../run.js",
"bindings": [
{
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in",
"queueName": "myqueue-items",
"connection": "AzureWebJobsStorage"
}
]
}
function.json Example (Azure Service Bus Queue)
{
"scriptFile": "../run.js",
"bindings": [
{
"name": "message",
"type": "queueTrigger",
"direction": "in",
"queueName": "my-servicebus-queue",
"connection": "ServiceBusConnection"
}
]
}
Key Properties
| Property | Description | Required |
|---|---|---|
name |
The name of the variable that holds the queue message in your function code. | Yes |
type |
Must be queueTrigger. |
Yes |
direction |
Must be in for a trigger. |
Yes |
queueName |
The name of the queue to monitor. | Yes |
connection |
The name of an app setting that contains the queue connection string. | Yes |
Processing Messages
When your function is triggered, the message payload is passed to your function based on the name property defined in the binding.
Example: JavaScript Function
This function logs the content of a message from an Azure Storage Queue.
module.exports = async function (context, myQueueItem) {
context.log('JavaScript queue trigger function processed work item', myQueueItem);
// You can access the message content via the myQueueItem variable
// For example, if the message is JSON:
// const data = JSON.parse(myQueueItem);
// context.log('Parsed data:', data);
};
Example: C# Function
This example shows a C# function triggered by an Azure Storage Queue message.
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace MyCompany.MyFunctions
{
public class QueueTriggerFunction
{
private readonly ILogger _logger;
public QueueTriggerFunction(ILogger logger)
{
_logger = logger;
}
[Function("QueueTriggerCSharp")]
public void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem)
{
_logger.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
// Process the message content
}
}
}
Message Deletion
By default, the Queue trigger automatically deletes messages from the queue after they have been successfully processed by your function. If your function throws an unhandled exception, the message may be reprocessed (depending on queue configuration and retry policies).
Advanced Scenarios
- Batching: For high-throughput scenarios, consider using batch triggers to process multiple messages at once.
- Poison Messages: Implement strategies to handle messages that repeatedly cause function failures (e.g., moving them to a poison queue).
- Service Bus Features: Leverage Service Bus features like sessions, dead-letter queues, and message deferral for more robust message processing.