Azure Storage Documentation

Using Azure Storage Queues with Java

Azure Storage Queues provide a robust and scalable way to decouple application components. This guide will walk you through the process of using Azure Storage Queues with the Azure SDK for Java.

Prerequisites:

  • An Azure subscription.
  • A Storage Account in your Azure subscription.
  • Java Development Kit (JDK) installed.
  • Maven or Gradle for dependency management.

1. Set Up Your Project

First, add the necessary Azure Storage Queue dependency to your project's build file (e.g., pom.xml for Maven).

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-storage-queue</artifactId>
    <version>12.17.1</version> <!-- Check for the latest version -->
</dependency>

2. Connect to Your Storage Account

You'll need your storage account name and its access key or a connection string. It's recommended to use Azure Identity for authentication in production environments.

import com.azure.storage.queue.QueueClient;
import com.azure.storage.queue.QueueClientBuilder;
import com.azure.storage.queue.models.QueueMessage;
import com.azure.storage.queue.models.SendMessageResponse;

public class QueueExample {

    public static void main(String[] args) {
        // Replace with your actual connection string
        String connectionString = System.getenv("AZURE_STORAGE_CONNECTION_STRING");
        // Or use account name and key
        // String accountName = "YOUR_STORAGE_ACCOUNT_NAME";
        // String accountKey = "YOUR_STORAGE_ACCOUNT_KEY";

        String queueName = "my-java-queue";

        QueueClient queueClient = new QueueClientBuilder()
            .connectionString(connectionString)
            .queueName(queueName)
            .buildClient();

        // Ensure the queue exists
        if (!queueClient.exists()) {
            queueClient.create();
            System.out.println("Queue created: " + queueName);
        } else {
            System.out.println("Queue already exists: " + queueName);
        }

        // ... proceed with operations
    }
}

3. Send a Message to the Queue

Sending a message is straightforward. Messages can be up to 64 KB in size.

// ... inside main method after client setup
String messageText = "Hello from Java!";
SendMessageResponse sendMessageResponse = queueClient.sendMessage(messageText);
System.out.println("Message sent with ID: " + sendMessageResponse.getMessageId());

4. Receive and Process Messages

You can peek at messages without deleting them, or receive them, which makes them invisible for a specified duration (visibility timeout) until they are explicitly deleted.

// ... inside main method

// Receive up to 5 messages
// The messages will be invisible for 30 seconds (visibility timeout)
int maxMessages = 5;
int visibilityTimeoutSeconds = 30;

System.out.println("Receiving messages...");
for (QueueMessage message : queueClient.receiveMessages(maxMessages, visibilityTimeoutSeconds).getValue()) {
    System.out.println("Received message:");
    System.out.println("  ID: " + message.getMessageId());
    System.out.println("  Content: " + message.getMessageText());
    System.out.println("  Pop receipt: " + message.getPopReceipt());

    // Process the message here...

    // Delete the message after successful processing
    queueClient.deleteMessage(message.getMessageId(), message.getPopReceipt());
    System.out.println("Message deleted.");
}

Important: Always delete a message after you have successfully processed it. If you don't delete it within the visibility timeout, it will become visible again and might be processed multiple times. Implement robust error handling and idempotency for message processing.

5. Other Operations

By following these steps, you can effectively integrate Azure Storage Queues into your Java applications for reliable message queuing and inter-component communication.

For more advanced scenarios and detailed API references, please refer to the official Azure Queue Storage documentation.