This tutorial guides you through retrieving messages from an Azure Storage Queue using various client libraries. Processing messages from a queue is a fundamental operation for building decoupled, asynchronous applications on Azure.
When you retrieve messages from a queue, you have two main options:
Peeking is ideal for read-only operations. It doesn't alter the queue's state.
Note: Peeked messages remain visible in the queue and can be retrieved again by other clients or subsequent peek operations.
from azure.storage.queue import QueueClient
connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
queue_name = "myqueue"
queue_client = QueueClient.from_connection_string(connection_string, queue_name)
try:
messages = queue_client.peek_messages()
if messages:
print(f"Found {len(messages)} messages to peek:")
for msg in messages:
print(f" Message ID: {msg.id}, Content: {msg.content}")
else:
print("Queue is empty.")
except Exception as e:
print(f"An error occurred: {e}")
This is the most common scenario for queue-based processing. A message is retrieved, made invisible, processed, and then deleted.
Important: If your application crashes or fails to delete a message within the visibility timeout, the message will automatically become visible again in the queue, allowing another instance of your application to process it. This ensures message durability.
const { QueueServiceClient } = require("@azure/storage-queue");
async function processMessages() {
const connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
const queueName = "myqueue";
const queueServiceClient = QueueServiceClient.fromConnectionString(connectionString);
const queueClient = queueServiceClient.getQueueClient(queueName);
try {
// Get up to 5 messages, with a visibility timeout of 60 seconds
const response = await queueClient.receiveMessages({ numberOfMessages: 5, visibilityTimeout: 60 });
if (response.receivedMessages.length > 0) {
console.log(`Received ${response.receivedMessages.length} messages:`);
for (const message of response.receivedMessages) {
console.log(` Message ID: ${message.messageId}, Pop Receipt: ${message.popReceipt}, Content: ${message.messageText}`);
// Simulate processing the message
console.log(` Processing message ${message.messageId}...`);
await new Promise(resolve => setTimeout(resolve, 2000)); // Simulate work
// Delete the message after successful processing
await queueClient.deleteMessage(message.messageId, message.popReceipt);
console.log(` Message ${message.messageId} deleted.`);
}
} else {
console.log("No messages received.");
}
} catch (error) {
console.error("An error occurred:", error);
}
}
processMessages();
The visibilityTimeout parameter in the receiveMessages (or equivalent in other SDKs) operation is crucial. It defines how long a message will be hidden from other consumers after it's retrieved. During this time, the processing application should complete its work and delete the message.
Robust applications should include comprehensive error handling and retry mechanisms:
Explore other Azure Storage Queue operations: