This guide will walk you through the essential steps to get started with Azure Storage Queue, a service that enables you to reliably store and retrieve large numbers of messages.
Azure Queue Storage is a service that lets you store large numbers of unreliable messages to be processed asynchronously. Each message in the queue is typically JSON or XML formatted and can be up to 64 KB in size. Queue Storage is useful for decoupling application components that are developed and timed independently.
You can create a queue using the Azure Portal, Azure CLI, or programmatically. Here's how to do it with the Azure CLI:
az storage queue create \
--name "myqueue" \
--account-name "mystorageaccount" \
--account-key "your_storage_account_key"
Replace "myqueue" with your desired queue name and "mystorageaccount" and "your_storage_account_key" with your storage account name and primary access key. You can find your storage account key in the Azure Portal under your storage account's "Access keys" section.
Messages are added to a queue using the message put command. Each message can be a simple string or a JSON object.
az storage message put \
--queue-name "myqueue" \
--account-name "mystorageaccount" \
--account-key "your_storage_account_key" \
--content "This is my first message."
You can add multiple messages by running the command multiple times or by specifying multiple messages with the --content parameter (separated by newlines).
To view the front message without removing it from the queue, use the message peek command.
az storage message peek \
--queue-name "myqueue" \
--account-name "mystorageaccount" \
--account-key "your_storage_account_key"
To retrieve a message from the queue and make it invisible for a specified period (the visibility timeout), use the message show command. After processing, you can delete it.
# Get the message (sets visibility timeout to 30 seconds by default)
az storage message show \
--queue-name "myqueue" \
--account-name "mystorageaccount" \
--account-key "your_storage_account_key"
# Assuming you got the message ID and pop receipt from the above command
az storage message delete \
--queue-name "myqueue" \
--account-name "mystorageaccount" \
--account-key "your_storage_account_key" \
--ids "your_message_id" \
--pop-receipt "your_pop_receipt"
The message show command returns the message ID and pop receipt, which are required for the message delete command.
If your application crashes or fails to process a message within the visibility timeout, the message will become visible again in the queue and can be retrieved by another worker. This ensures message durability.
To remove all messages from the queue, use the queue clear command.
az storage queue clear \
--name "myqueue" \
--account-name "mystorageaccount" \
--account-key "your_storage_account_key"
When you no longer need the queue, you can delete it.
az storage queue delete \
--name "myqueue" \
--account-name "mystorageaccount" \
--account-key "your_storage_account_key"
For production scenarios, consider using SDKs (e.g., Python, .NET, Java) for more robust and feature-rich interactions with Azure Storage Queues. This provides better error handling, retry logic, and integration with your application's services.
Congratulations! You have successfully completed the Azure Storage Queue quickstart.