This sample demonstrates a Java Spring Boot application that sends and receives messages via Azure Service Bus. The application uses the Spring Cloud Azure Service Bus Stream Binder to connect to a Service Bus queue, send a Hello, World! message, and receive it back, and then exits.
Note
At this time, the Azure Web Apps and Azure Function Apps emulators in LocalStack for Azure do not support Java applications. The Spring Boot sample application must be executed directly on the host machine and cannot be deployed to the emulator.
The solution is composed of the following Azure resources:
- Azure Resource Group: A logical container scoping all resources in this sample.
- Azure Service Bus Namespace: The messaging namespace that hosts the queue used by the application.
- Azure Service Bus Queue: The
myqueuequeue used to send and receive messages.
Note The Java application currently runs on the host machine. In a future iteration, it will be deployed to an emulator-hosted web app.
- Azure Subscription
- Azure CLI
- Azlocal CLI: LocalStack Azure CLI wrapper
- Java 21+
- Maven 3.8+
- Terraform, if you plan to deploy the sample via Terraform.
- Bicep extension, if you plan to deploy the sample via Bicep.
Set up the Azure emulator using the LocalStack for Azure Docker image. Before starting, ensure you have a valid LOCALSTACK_AUTH_TOKEN to access the Azure emulator. Refer to the Auth Token guide to obtain your Auth Token and set it in the LOCALSTACK_AUTH_TOKEN environment variable. The Azure Docker image is available on the LocalStack Docker Hub. To pull the image, execute:
docker pull localstack/localstack-azure-alphaStart the LocalStack Azure emulator by running:
# Set the authentication token
export LOCALSTACK_AUTH_TOKEN=<your_auth_token>
# Start the LocalStack Azure emulator
IMAGE_NAME=localstack/localstack-azure-alpha localstack start -d
localstack wait -t 60
# Route all Azure CLI calls to the LocalStack Azure emulator
azlocal start-interceptionDeploy the application to LocalStack for Azure using one of these methods:
All deployment methods have been fully tested against Azure and the LocalStack for Azure local emulator.
Note When you deploy the application to LocalStack for Azure for the first time, the initialization process involves downloading and building Docker images. This is a one-time operation—subsequent deployments will be significantly faster. Depending on your internet connection and system resources, this initial setup may take several minutes.
The deploy script performs the following steps:
- Creates a resource group.
- Creates a Service Bus namespace.
- Creates a Service Bus queue (
myqueue). - Retrieves the namespace connection string and exports it as
AZURE_SERVICEBUS_CONNECTION_STRING. - Starts the Spring Boot application via
mvn clean spring-boot:run.
The application then:
- Connects to the configured Service Bus namespace using the connection string.
- Sends a
Hello, World!message to themyqueuequeue. - Receives the message via a
@ServiceBusListenerconsumer. - Shuts down after receiving the message.