Azure Queue Storage with Spring Boot: Producer & Consumer Guide

Diagram Explanation

📌 User sends HTTP requests to QueueProducerController.
📌 QueueProducerService pushes the message into Azure Queue Storage (cylinder).
📌 QueueConsumerService polls messages from Azure Queue Storage and processes them.
📌 After processing, the message is deleted from the queue.


Azure Queue Storage is a cloud messaging service that enables applications to communicate asynchronously by passing messages. In this guide, we'll implement a Spring Boot Producer to send messages and a Spring Boot Consumer to retrieve and process them.


Prerequisites

Before you begin, ensure you have:
✔️ Azure Subscription (Create one here)
✔️ Azure Storage Account with Queue Storage enabled
✔️ Spring Boot installed (Use Spring Initializr)
✔️ Java 17+ installed
✔️ Maven/Gradle for dependency management


Step 1: Create Azure Storage Queue

1️⃣ Log in to the Azure Portal
2️⃣ Search for Storage Accounts → Click Create
3️⃣ Provide Subscription, Resource Group, and Storage Account Name
4️⃣ Choose Region and Performance (Standard/Premium)
5️⃣ Under Advanced → Enable Queue Storage
6️⃣ Click Review + Create → Wait for Deployment
7️⃣ Navigate to Queues → Create a New Queue (e.g., my-queue)
8️⃣ Copy the Connection String from Access Keys


Step 2: Set Up Spring Boot Producer

2.1 Add Dependencies (pom.xml)

<dependencies>
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Azure Storage Queue SDK -->
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-storage-queue</artifactId>
        <version>12.24.0</version>
    </dependency>
</dependencies>

2.2 Configure Application Properties

In src/main/resources/application.properties, add:

azure.storage.queue-name=my-queue
azure.storage.connection-string=DefaultEndpointsProtocol=https;AccountName=<your-storage-account>;AccountKey=<your-key>;EndpointSuffix=core.windows.net

2.3 Implement Queue Producer Service

Create a service to send messages to Azure Queue Storage.

📌 File: QueueProducerService.java

import com.azure.storage.queue.QueueClient;
import com.azure.storage.queue.QueueClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class QueueProducerService {
    
    private final QueueClient queueClient;

    public QueueProducerService(@Value("${azure.storage.connection-string}") String connectionString,
                                @Value("${azure.storage.queue-name}") String queueName) {
        this.queueClient = new QueueClientBuilder()
                .connectionString(connectionString)
                .queueName(queueName)
                .buildClient();
    }

    public void sendMessage(String message) {
        queueClient.sendMessage(message);
    }
}

2.4 Create REST Controller for Sending Messages

📌 File: QueueProducerController.java

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/queue")
public class QueueProducerController {
    
    private final QueueProducerService queueProducerService;

    public QueueProducerController(QueueProducerService queueProducerService) {
        this.queueProducerService = queueProducerService;
    }

    @PostMapping("/send")
    public String sendMessage(@RequestParam String message) {
        queueProducerService.sendMessage(message);
        return "Message sent: " + message;
    }
}

🔹 Test Sending Messages:
Run your Spring Boot application and send a message via Postman or cURL:

curl -X POST "http://localhost:8080/queue/send?message=HelloAzureQueue"

Step 3: Set Up Spring Boot Consumer

3.1 Implement Queue Consumer Service

The consumer service will read messages from the queue at regular intervals.

📌 File: QueueConsumerService.java

import com.azure.storage.queue.QueueClient;
import com.azure.storage.queue.QueueClientBuilder;
import com.azure.storage.queue.models.QueueMessageItem;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QueueConsumerService {

    private final QueueClient queueClient;

    public QueueConsumerService(@Value("${azure.storage.connection-string}") String connectionString,
                                @Value("${azure.storage.queue-name}") String queueName) {
        this.queueClient = new QueueClientBuilder()
                .connectionString(connectionString)
                .queueName(queueName)
                .buildClient();
    }

    @Scheduled(fixedRate = 5000) // Runs every 5 seconds
    public void receiveMessages() {
        List<QueueMessageItem> messages = queueClient.receiveMessages(5, null, null).stream().toList();
        for (QueueMessageItem message : messages) {
            System.out.println("Received: " + message.getMessageText());
            queueClient.deleteMessage(message.getMessageId(), message.getPopReceipt()); // Remove message from queue
        }
    }
}

3.2 Enable Scheduling

📌 File: SpringBootAzureQueueApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SpringBootAzureQueueApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAzureQueueApplication.class, args);
    }
}

Step 4: Run & Test the Application

1️⃣ Start the Spring Boot Application

mvn spring-boot:run

2️⃣ Send a Message

curl -X POST "http://localhost:8080/queue/send?message=HelloWorld"

3️⃣ Observe Consumer Logs
Check the logs to see if the consumer receives and processes the message.

Received: HelloWorld

Conclusion

✅ You have successfully implemented an Azure Queue Storage Producer and Consumer in Spring Boot.
✅ The producer sends messages to the Azure queue, while the consumer polls messages at fixed intervals and processes them.


Next Steps

🚀 Enhance with Azure Functions for event-driven consumption
🔐 Secure Connection Strings using Azure Key Vault
📊 Monitor Messages with Azure Storage Explorer

Comments

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example