From b3c1f84ad6ac58210e8e96eeb70bd7e2c46e4d0b Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Thu, 26 Feb 2026 11:54:28 -0100 Subject: [PATCH 1/2] Sample on how to connect to ServiceBus using the Java (Spring-Boot) SDK --- .github/workflows/run-samples.yml | 5 ++ run-samples.sh | 1 + samples/servicebus/README.md | 31 +++++++++++ samples/servicebus/java/app/pom.xml | 50 ++++++++++++++++++ .../ServiceBusSampleApplication.java | 52 +++++++++++++++++++ .../app/src/main/resources/application.yaml | 9 ++++ samples/servicebus/java/scripts/deploy.sh | 50 ++++++++++++++++++ 7 files changed, 198 insertions(+) create mode 100644 samples/servicebus/README.md create mode 100644 samples/servicebus/java/app/pom.xml create mode 100644 samples/servicebus/java/app/src/main/java/com/azure/spring/sample/servicebus/ServiceBusSampleApplication.java create mode 100644 samples/servicebus/java/app/src/main/resources/application.yaml create mode 100755 samples/servicebus/java/scripts/deploy.sh diff --git a/.github/workflows/run-samples.yml b/.github/workflows/run-samples.yml index 9ec183d..afbd66b 100644 --- a/.github/workflows/run-samples.yml +++ b/.github/workflows/run-samples.yml @@ -94,6 +94,11 @@ jobs: with: dotnet-version: '10.0' + - uses: actions/setup-java@v5 + with: + distribution: 'temurin' + java-version: '25' + - name: Install System Dependencies # Essential tools for script execution, app packaging, and database connectivity. # jq: for parsing JSON responses from Azure CLI. diff --git a/run-samples.sh b/run-samples.sh index 4253a7e..ae6b220 100755 --- a/run-samples.sh +++ b/run-samples.sh @@ -29,6 +29,7 @@ fi # 1. Define Samples (placed before tool checks so --list works without dependencies) SAMPLES=( + "samples/servicebus/java|bash scripts/deploy.sh" "samples/function-app-front-door/python|bash scripts/deploy_all.sh --name-prefix testafd --use-localstack|" "samples/function-app-managed-identity/python|bash scripts/user-managed-identity.sh|bash scripts/validate.sh && bash scripts/test.sh" "samples/function-app-storage-http/dotnet|bash scripts/deploy.sh|bash scripts/validate.sh && bash scripts/call-http-triggers.sh" diff --git a/samples/servicebus/README.md b/samples/servicebus/README.md new file mode 100644 index 0000000..3e7476e --- /dev/null +++ b/samples/servicebus/README.md @@ -0,0 +1,31 @@ +# Azure ServiceBus (Java SDK) + +This sample creates a minimal Spring Boot application that sends and receive messages via Azure ServiceBus. + +## Overview + + - **`app`**: A folder that contains the Java application + - **`scripts/deploy.sh`**: One script that provisions the required resources for this sample and runs the application + +## Quick Start + +To deploy the scenario against a LocalStack Emulator: +```bash +bash ./scripts/deploy.sh +``` + +The script will: + 1. Creates a ResourceGroup + 2. Creates a Servicebus Namespace + 3. Creates a Servicebus Queue + 4. Starts the Java app + + The app then: + 1. Connects to the configured ServiceBus + 2. Sends a message to a queue + 3. Receives the message, and shuts down the application + + After the application has shutdown, the script will then: + 5. Deletes the resource group with all of it's resources + + diff --git a/samples/servicebus/java/app/pom.xml b/samples/servicebus/java/app/pom.xml new file mode 100644 index 0000000..5e46701 --- /dev/null +++ b/samples/servicebus/java/app/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + com.azure.spring + + + org.springframework.boot + spring-boot-starter-parent + 4.0.2 + + + + spring-cloud-azure-sample-servicebus + 1.0.0 + jar + + Send EventGridEvent by Event Grid and receive by Service Bus Queue + + + ${java.version} + ${java.version} + 7.0.0 + + + + + + com.azure.spring + spring-cloud-azure-dependencies + ${version.spring.cloud.azure} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + com.azure.spring + spring-cloud-azure-stream-binder-servicebus + + + + diff --git a/samples/servicebus/java/app/src/main/java/com/azure/spring/sample/servicebus/ServiceBusSampleApplication.java b/samples/servicebus/java/app/src/main/java/com/azure/spring/sample/servicebus/ServiceBusSampleApplication.java new file mode 100644 index 0000000..cdeced6 --- /dev/null +++ b/samples/servicebus/java/app/src/main/java/com/azure/spring/sample/servicebus/ServiceBusSampleApplication.java @@ -0,0 +1,52 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.spring.sample.servicebus; + +import com.azure.spring.messaging.servicebus.implementation.core.annotation.ServiceBusListener; +import org.springframework.stereotype.Component; +import com.azure.messaging.servicebus.ServiceBusClientBuilder; +import com.azure.messaging.servicebus.ServiceBusMessage; +import com.azure.messaging.servicebus.ServiceBusSenderClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ServiceBusSampleApplication { + + private static final Logger LOGGER = LoggerFactory.getLogger(ServiceBusSampleApplication.class); + public static void main(String[] args) { + SpringApplication.run(ServiceBusSampleApplication.class, args); + sendMessage(); + } + + static void sendMessage() + { + String connectionString = System.getenv("AZURE_SERVICEBUS_CONNECTION_STRING"); + String queueName = "myqueue"; + + // create a Service Bus Sender client for the queue + ServiceBusSenderClient senderClient = new ServiceBusClientBuilder() + .connectionString(connectionString) + .sender() + .queueName(queueName) + .buildClient(); + + // send one message to the queue + senderClient.sendMessage(new ServiceBusMessage("Hello, World!")); + LOGGER.info("Sent a single message to the queue: " + queueName); + } +} + +@Component +class ServiceBusMessageListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(ServiceBusMessageListener.class); + + @ServiceBusListener(destination = "myqueue") + public void receiveMessage(String message) { + LOGGER.info("Received message from Azure Service Bus: " + message); + System.exit(0); + } +} \ No newline at end of file diff --git a/samples/servicebus/java/app/src/main/resources/application.yaml b/samples/servicebus/java/app/src/main/resources/application.yaml new file mode 100644 index 0000000..326bc14 --- /dev/null +++ b/samples/servicebus/java/app/src/main/resources/application.yaml @@ -0,0 +1,9 @@ +spring: + cloud: + azure: + profile: + cloud-type: OTHER + environment: + management-endpoint: "http://localhost:4566" + servicebus: + connection-string: ${AZURE_SERVICEBUS_CONNECTION_STRING} \ No newline at end of file diff --git a/samples/servicebus/java/scripts/deploy.sh b/samples/servicebus/java/scripts/deploy.sh new file mode 100755 index 0000000..04ed85a --- /dev/null +++ b/samples/servicebus/java/scripts/deploy.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +# Variables +LOCATION='westeurope' +RESOURCE_GROUP_NAME="local-rg-$RANDOM" +CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)" +SERVICEBUS_NAMESPACE_NAME="ls-sb-ns-$RANDOM" +# Queue name is hardcoded in application properties +SERVICEBUS_QUEUE_NAME="myqueue" + + +# Change the current directory to the script's directory +cd "$CURRENT_DIR" || exit + +# Redirect AZ calls to LocalStack +azlocal start_interception + +# Create a resource group +echo "Creating resource group [$RESOURCE_GROUP_NAME]..." +az group create \ + --name $RESOURCE_GROUP_NAME \ + --location $LOCATION \ + --only-show-errors 1>/dev/null + +# Create a ServiceBus Queue +az servicebus namespace create \ + --name $SERVICEBUS_NAMESPACE_NAME \ + --resource-group $RESOURCE_GROUP_NAME + +queue_id=$(az servicebus queue create --resource-group $RESOURCE_GROUP_NAME --namespace-name $SERVICEBUS_NAMESPACE_NAME --name $SERVICEBUS_QUEUE_NAME --query 'id' --output tsv) + +# Register connection string to use with our Application +export AZURE_SERVICEBUS_CONNECTION_STRING=$(az servicebus namespace authorization-rule keys list \ + --resource-group $RESOURCE_GROUP_NAME \ + --namespace-name $SERVICEBUS_NAMESPACE_NAME \ + --name RootManageSharedAccessKey \ + --query primaryConnectionString \ + --output tsv) + +# START JAVA APP +cd ../app && mvn clean spring-boot:run + +# Tear down all resources +az group delete \ + --name $RESOURCE_GROUP_NAME \ + --yes + +azlocal stop_interception + + From 71e6826e033a361c91f112b58c508b55d65d2d5c Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Wed, 4 Mar 2026 12:12:51 -0100 Subject: [PATCH 2/2] List ServiceBus/Java sample in the main README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5539981..a20d600 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ This repository contains comprehensive sample projects demonstrating how to deve | [Web App and CosmosDB for NoSQL API ](./samples/web-app-cosmosdb-nosql-api/python/README.md) | Azure Web App using CosmosDB for NoSQL API | | [Web App and Managed Identities](./samples/web-app-managed-identity/python/README.md) | Azure Web App using Managed Identities | | [Web App and SQL Database ](./samples/web-app-sql-database/python/README.md) | Azure Web App using SQL Database | +| [ServiceBus ](./samples/servicebus/README.md) | Azure ServiceBus used by a Spring Boot application | ## Sample Structure