Skip to content

Latest commit

 

History

History
87 lines (59 loc) · 5.03 KB

File metadata and controls

87 lines (59 loc) · 5.03 KB

Azure Service Bus with Spring Boot

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.

Architecture

The solution is composed of the following Azure resources:

  1. Azure Resource Group: A logical container scoping all resources in this sample.
  2. Azure Service Bus Namespace: The messaging namespace that hosts the queue used by the application.
  3. Azure Service Bus Queue: The myqueue queue 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.

Prerequisites

Deployment

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-alpha

Start 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-interception

Deploy 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.

How It Works

The deploy script performs the following steps:

  1. Creates a resource group.
  2. Creates a Service Bus namespace.
  3. Creates a Service Bus queue (myqueue).
  4. Retrieves the namespace connection string and exports it as AZURE_SERVICEBUS_CONNECTION_STRING.
  5. Starts the Spring Boot application via mvn clean spring-boot:run.

The application then:

  1. Connects to the configured Service Bus namespace using the connection string.
  2. Sends a Hello, World! message to the myqueue queue.
  3. Receives the message via a @ServiceBusListener consumer.
  4. Shuts down after receiving the message.

References