Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/run-samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions run-samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 31 additions & 0 deletions samples/servicebus/README.md
Original file line number Diff line number Diff line change
@@ -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


50 changes: 50 additions & 0 deletions samples/servicebus/java/app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.azure.spring</groupId>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<artifactId>spring-cloud-azure-sample-servicebus</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>Send EventGridEvent by Event Grid and receive by Service Bus Queue</name>

<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<version.spring.cloud.azure>7.0.0</version.spring.cloud.azure>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-dependencies</artifactId>
<version>${version.spring.cloud.azure}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-stream-binder-servicebus</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
cloud:
azure:
profile:
cloud-type: OTHER
environment:
management-endpoint: "http://localhost:4566"
servicebus:
connection-string: ${AZURE_SERVICEBUS_CONNECTION_STRING}
50 changes: 50 additions & 0 deletions samples/servicebus/java/scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -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