Develop a Spring Integration with Azure Storage Queue application using Visual Studio Code and Azure CLI authentication - hui1110/azure-sdk-for-java GitHub Wiki
Develop a Spring Integration with Azure Storage Queue application using Visual Studio Code and Azure CLI authentication
This blog describes how to develop a Spring Integration with Azure Storage Queue application using Visual Studio Code and Azure CLI authentication.
Azure Queue Storage is a service for storing large numbers of messages. You access messages from anywhere in the world via authenticated calls using HTTP or HTTPS. A queue message can be up to 64 KB in size. A queue may contain millions of messages, up to the total capacity limit of a storage account. Queues are commonly used to create a backlog of work to process asynchronously.
An integrated development environment or IDE is an application that facilitates the development of new applications. They are designed to cover all programming tasks in one place, providing a central interface with all the tools a developer needs, such as a code editor, compiler, debugger, or automation tools.
Since its release, Visual Studio Code (VSCode) has achieved tremendous growth in the developer community due to its multi-platform, multi-language, free, and open code. It is an extension that adds support for almost any programming language and turns it into a powerful IDE.
- An Azure subscription.
- Azure CLI.
- Java Development Kit (JDK). In this guide, we shall use JDK 17.
- Apache Maven, version 3.0 or higher.
- Visual Studio Code.
In VSCode we will need to install the following extensions:
- Extension Pack for Java. Package of extensions adding Java and Maven compatibility.
- Spring Boot Extension Pack. Compatibility with Spring Boot projects.
-
See this article, create a storage account.
-
Create a queue in the newly created storage account.
-
Refer to this article to assign your Azure AD user, group, or service principal (application) the Storage Queue Data Contributor role.
To create the initial structure of the Spring Boot project we will use Spring Initializr, which will generate a Spring base project with all the options we have selected.
We can also use Spring Initializr from VSCode, launching the command palette with Ctrl+Shift+P and executing the Spring Initializr command: Create a Maven Project...

NOTE: When we select both
Azure StorageandSpring Integration, the starterspring-cloud-azure-starter-integration-storage-queuewill be added to the pom.xml file.
We extract the zip file generated with Spring Initializr and open the folder from Visual Studio Code.
For this guide we will use Maven, Java 17, Spring Boot 2.7.5, Azure Storage, Spring Integration and Spring Web as dependencies.
IMPORTANT: Before starting the following, See this article Authenticating with Azure CLI.
-
Configure the following properties in
application.yml:spring.cloud.azure.storage.queue.account-name=<your_storage_account_name> -
Write your Java code.
Controller code can refer to the following:
@RestController
public class SendController {
private static final Logger LOGGER = LoggerFactory.getLogger(SendController.class);
private static final String STORAGE_QUEUE_NAME ="<storage_queue_name>";
private static final String OUTPUT_CHANNEL = "output";
@Autowired
StorageQueueOutboundGateway storageQueueOutboundGateway;
@PostMapping("/messages")
public String send(@RequestParam("message") String message) {
storageQueueOutboundGateway.send(message);
return message;
}
@Bean
@ServiceActivator(inputChannel = OUTPUT_CHANNEL)
public MessageHandler messageSender(StorageQueueTemplate storageQueueTemplate) {
DefaultMessageHandler handler = new DefaultMessageHandler(STORAGE_QUEUE_NAME, storageQueueTemplate);
handler.setSendCallback(new ListenableFutureCallback<Void>() {
@Override
public void onSuccess(Void result) {
LOGGER.info("Message was sent successfully.");
}
@Override
public void onFailure(Throwable ex) {
LOGGER.info("There was an error sending the message.");
}
});
return handler;
}
@MessagingGateway(defaultRequestChannel = OUTPUT_CHANNEL)
public interface StorageQueueOutboundGateway {
void send(String text);
}
}The receiving message code can refer to the following:
@Service
public class ReceiveService {
private static final Logger LOGGER = LoggerFactory.getLogger(ReceiveService.class);
private static final String STORAGE_QUEUE_NAME = "<storage_queue_name>";
private static final String INPUT_CHANNEL = "input";
@Bean
@InboundChannelAdapter(channel = INPUT_CHANNEL, poller = @Poller(fixedDelay = "1000"))
public StorageQueueMessageSource storageQueueMessageSource(StorageQueueTemplate storageQueueTemplate) {
return new StorageQueueMessageSource(STORAGE_QUEUE_NAME, storageQueueTemplate);
}
@ServiceActivator(inputChannel = INPUT_CHANNEL)
public void messageReceiver(byte[] payload, @Header(AzureHeaders.CHECKPOINTER) Checkpointer checkpointer) {
String message = new String(payload);
LOGGER.info("New message received: '{}'", message);
checkpointer.success()
.doOnError(Throwable::printStackTrace)
.doOnSuccess(t -> LOGGER.info("Message '{}' successfully checkpointed", message))
.block();
}
}See storage-queue-integration-sample for more details.
In your terminal, run mvn clean spring-boot:run to start the application.
- Send a POST request
curl -X POST http://localhost:8080/messages?message=hello
- Verify in your app’s logs that similar messages were posted:
New message received: 'hello'
Message 'hello' successfully checkpointed
As we have seen, if the DefaultAzureCredential account has been authenticated via the Azure CLI az login command, then that account will be used for authentication.
DefaultAzureCredential is suitable for most scenarios where the application ends up running in the Azure cloud, it will authenticate through the following mechanisms in order.

The ChainedTokenCredential class provides the ability to chain together multiple credential instances so that they are tried sequentially when authenticating, as described here.