From 44eb38fb6025062ff1359ffff6673f494e9cff57 Mon Sep 17 00:00:00 2001 From: Moary Chen Date: Fri, 24 Jan 2025 15:21:21 +0800 Subject: [PATCH] Add sample for DLQ consumption of Service Bus Queue --- README.md | 27 +-- pom.xml | 1 + .../servicebus-jms-dlq-queue/README.md | 216 ++++++++++++++++++ .../servicebus-jms-dlq-queue/pom.xml | 51 +++++ .../sample/jms/queue/QueueReceiveService.java | 40 ++++ .../sample/jms/queue/QueueSendController.java | 30 +++ .../ServiceBusJMSDLQQueueApplication.java | 17 ++ .../azure/spring/sample/jms/queue/User.java | 32 +++ .../src/main/resources/application.yaml | 12 + .../src/main/resources/log4j.properties | 7 + .../terraform/main.tf | 77 +++++++ .../terraform/outputs.tf | 15 ++ .../terraform/setup_env.ps1 | 5 + .../terraform/setup_env.sh | 5 + .../terraform/variables.tf | 23 ++ 15 files changed, 545 insertions(+), 13 deletions(-) create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/README.md create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/pom.xml create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/QueueReceiveService.java create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/QueueSendController.java create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/ServiceBusJMSDLQQueueApplication.java create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/User.java create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/resources/application.yaml create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/resources/log4j.properties create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/main.tf create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/outputs.tf create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/setup_env.ps1 create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/setup_env.sh create mode 100644 servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/variables.tf diff --git a/README.md b/README.md index 79a7675cd..c60696ea6 100644 --- a/README.md +++ b/README.md @@ -102,19 +102,20 @@ ### 2.10 Azure Service Bus -| Sample Project | Spring Boot 3.x | Spring Boot 2.x | -|-------------------------------|-----------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| servicebus-queue | ✅ [link](servicebus/spring-cloud-azure-starter-servicebus/servicebus-queue) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-servicebus/servicebus-queue) | -| servicebus-topic | ✅ [link](servicebus/spring-cloud-azure-starter-servicebus/servicebus-topic) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-servicebus/servicebus-topic) | -| servicebus-jms-queue | ✅ [link](servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-queue) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-queue) | -| servicebus-jms-topic | ✅ [link](servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-topic) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-topic) | -| single-namespace | ✅ [link](servicebus/spring-cloud-azure-starter-integration-servicebus/single-namespace) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-integration-servicebus/single-namespace) | -| multiple-namespaces | ✅ [link](servicebus/spring-cloud-azure-starter-integration-servicebus/multiple-namespaces) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-integration-servicebus/multiple-namespaces) | -| servicebus-queue-binder | ✅ [link](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder) | -| servicebus-queue-multibinders | ✅ [link](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-multibinders) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-multibinders) | -| servicebus-topic-binder | ✅ [link](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-topic-binder) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-topic-binder) | -| servicebus-queue-binder-arm | ✅ [link](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder-arm) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder-arm) | -| servicebus-spring-messaging | ✅ [link](servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging) | +| Sample Project | Spring Boot 3.x | Spring Boot 2.x | +|-------------------------------|----------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| servicebus-queue | ✅ [link](servicebus/spring-cloud-azure-starter-servicebus/servicebus-queue) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-servicebus/servicebus-queue) | +| servicebus-topic | ✅ [link](servicebus/spring-cloud-azure-starter-servicebus/servicebus-topic) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-servicebus/servicebus-topic) | +| servicebus-jms-dlq-queue | ✅ [link](servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue) | ❌ | +| servicebus-jms-queue | ✅ [link](servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-queue) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-queue) | +| servicebus-jms-topic | ✅ [link](servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-topic) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-topic) | +| single-namespace | ✅ [link](servicebus/spring-cloud-azure-starter-integration-servicebus/single-namespace) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-integration-servicebus/single-namespace) | +| multiple-namespaces | ✅ [link](servicebus/spring-cloud-azure-starter-integration-servicebus/multiple-namespaces) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-starter-integration-servicebus/multiple-namespaces) | +| servicebus-queue-binder | ✅ [link](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder) | +| servicebus-queue-multibinders | ✅ [link](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-multibinders) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-multibinders) | +| servicebus-topic-binder | ✅ [link](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-topic-binder) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-topic-binder) | +| servicebus-queue-binder-arm | ✅ [link](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder-arm) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder-arm) | +| servicebus-spring-messaging | ✅ [link](servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging) | ✅ [link](https://github.com/Azure-Samples/azure-spring-boot-samples/tree/spring-boot-2.x/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging) | ### 2.11 Azure Storage diff --git a/pom.xml b/pom.xml index 9c6f50705..f8c0d3a3a 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,7 @@ postgresql/spring-cloud-azure-starter-jdbc-postgresql/spring-cloud-azure-postgresql-sample servicebus/spring-cloud-azure-starter-servicebus/servicebus-queue servicebus/spring-cloud-azure-starter-servicebus/servicebus-topic + servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-queue servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-topic servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/README.md b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/README.md new file mode 100644 index 000000000..d5070cb96 --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/README.md @@ -0,0 +1,216 @@ +--- +page_type: sample +languages: +- java +products: +- azure-service-bus +name: Sending and Receiving Message (DLQ) by Azure Service Bus (Queue) And Jms using Passwordless Connections +description: This sample demonstrates how to send and receive message (DLQ) by Azure Service Bus (queue) and JMS using passwordless connections +--- + +# Sending and Receiving (DLQ) Message by Azure Service Bus (Queue) And Jms using Passwordless connections + +This sample project demonstrates how to use Spring JMS for Azure Service Bus Queue (`spring-cloud-azure-starter-servicebus-jms`) to consume DLQ message of the queue using passwordless connections. + + +## What You Will Build +You will build an application using Spring JMS to send and receive messages (DLQ) for Azure Service Bus Queue. + +## What You Need + +- [An Azure subscription](https://azure.microsoft.com/free/) +- [Terraform](https://www.terraform.io/) +- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli) +- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later +- Maven +- You can also import the code straight into your IDE: + - [IntelliJ IDEA](https://www.jetbrains.com/idea/download) + +## Provision Azure Resources Required to Run This Sample +This sample will create Azure resources using Terraform. If you choose to run it without using Terraform to provision resources, please pay attention to: +> [!IMPORTANT] +> If you choose to use a security principal to authenticate and authorize with Microsoft Entra ID for accessing an Azure resource +> please refer to [Authorize access with Microsoft Entra ID](https://learn.microsoft.com/azure/developer/java/spring-framework/authentication#authorize-access-with-microsoft-entra-id) to make sure the security principal has been granted the sufficient permission to access the Azure resource. + +### Authenticate Using the Azure CLI +Terraform must authenticate to Azure to create infrastructure. + +In your terminal, use the Azure CLI tool to setup your account permissions locally. + +```shell +az login +``` + +Your browser window will open and you will be prompted to enter your Azure login credentials. After successful authentication, your terminal will display your subscription information. You do not need to save this output as it is saved in your system for Terraform to use. + +```shell +You have logged in. Now let us find all the subscriptions to which you have access... + +[ + { + "cloudName": "AzureCloud", + "homeTenantId": "home-Tenant-Id", + "id": "subscription-id", + "isDefault": true, + "managedByTenants": [], + "name": "Subscription-Name", + "state": "Enabled", + "tenantId": "0envbwi39-TenantId", + "user": { + "name": "your-username@domain.com", + "type": "user" + } + } +] +``` + +If you have more than one subscription, specify the subscription-id you want to use with command below: +```shell +az account set --subscription +``` + +### Provision the Resources + +After login Azure CLI with your account, now you can use the terraform script to create Azure Resources. + +#### Run with Bash + +```shell +# In the root directory of the sample +# Initialize your Terraform configuration +terraform -chdir=./terraform init + +# Apply your Terraform Configuration +terraform -chdir=./terraform apply -auto-approve + +``` + +#### Run with Powershell + +```shell +# In the root directory of the sample +# Initialize your Terraform configuration +terraform -chdir=terraform init + +# Apply your Terraform Configuration +terraform -chdir=terraform apply -auto-approve + +``` + +It may take a few minutes to run the script. After successful running, you will see prompt information like below: + +```shell +azurecaf_name.azurecaf_name_servicebus: Creating... +azurecaf_name.resource_group: Creating... +azurecaf_name.azurecaf_name_servicebus: Creation complete after 0s [id=qikkuohqscnhospw] +azurecaf_name.resource_group: Creation complete after 0s [id=hsgmdikkcuwtxecs] +azurerm_resource_group.main: Creating... +azurerm_resource_group.main: Creation complete after 5s ... +azurerm_servicebus_namespace.servicebus_namespace: Creating... +... +azurerm_servicebus_namespace.servicebus_namespace: Creation complete after ... +azurerm_servicebus_queue.queue: Creating... +azurerm_servicebus_queue.queue: Creation complete ... + +Apply complete! Resources: 5 added, 0 changed, 0 destroyed. + +Outputs: + +... + +``` + +You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created. + +### Export Output to Your Local Environment +Running the command below to export environment values: + +#### Run with Bash + +```shell +source ./terraform/setup_env.sh +``` + +#### Run with Powershell + +```shell +terraform\setup_env.ps1 +``` + +If you want to run the sample in debug mode, you can save the output value. + +```shell +SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE=... +PRICING_TIER=... +``` + +## Run Locally + +### Run the sample with Maven + +In your terminal, run `mvn clean spring-boot:run`. + +```shell +mvn clean spring-boot:run +``` + +### Run the sample in IDEs + +You can debug your sample by adding the saved output values to the tool's environment variables or the sample's `application.yaml` file. + +* If your tool is `IDEA`, please refer to [Debug your first Java application](https://www.jetbrains.com/help/idea/debugging-your-first-java-application.html) and [add environment variables](https://www.jetbrains.com/help/objc/add-environment-variables-and-program-arguments.html#add-environment-variables). + +* If your tool is `ECLIPSE`, please refer to [Debugging the Eclipse IDE for Java Developers](https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php) and [Eclipse Environment Variable Setup](https://examples.javacodegeeks.com/desktop-java/ide/eclipse/eclipse-environment-variable-setup-example/). + +## Verify This Sample + +1. Send a POST request with a normal message to Service Bus queue: + + ``` + curl -X POST http://localhost:8080/queue?message=hello + ``` + + Verify in your app's logs that a similar message was posted: + + ``` + Sending message: hello + ... + Received message from queue: User{name='hello'}. + ``` + +2. Send a POST request with an invalid message to DLQ of theService Bus queue: + + ``` + curl -X POST http://localhost:8080/queue?message=hello-invalid + ``` + + Verify in your app's logs that a similar message was posted: + + ``` + Sending message: hello-invalid + Received message from queue: User{name='hello-invalid'}. + Move message into dead letter queue: User{name='hello-invalid'}. + Received message from dead letter queue: User{name='hello-invalid'}. + ``` + +## Clean Up Resources +After running the sample, if you don't want to run the sample, remember to destroy the Azure resources you created to avoid unnecessary billing. + +The terraform destroy command terminates resources managed by your Terraform project. +To destroy the resources you created. + +#### Run with Bash + +```shell +terraform -chdir=./terraform destroy -auto-approve +``` + +#### Run with Powershell + +```shell +terraform -chdir=terraform destroy -auto-approve +``` + +## Deploy to Azure Spring Apps + +Now that you have the Spring Boot application running locally, it's time to move it to production. [Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/overview) makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see [Deploy your first application to Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/quickstart?tabs=Azure-CLI). diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/pom.xml b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/pom.xml new file mode 100644 index 000000000..5f7440871 --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.4.1 + + + + com.azure.spring + servicebus-jms-dlq-queue + 1.0.0 + jar + + Communicate to DLQ queue of Service Bus Queue via JMS API + + + 17 + 17 + 5.19.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-starter-servicebus-jms + + + + diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/QueueReceiveService.java b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/QueueReceiveService.java new file mode 100644 index 000000000..01497a657 --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/QueueReceiveService.java @@ -0,0 +1,40 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.sample.jms.queue; + +import jakarta.jms.JMSException; +import org.apache.qpid.jms.message.JmsMessageSupport; +import org.apache.qpid.jms.message.JmsObjectMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.jms.annotation.JmsListener; +import org.springframework.stereotype.Service; + +import static org.apache.qpid.jms.message.JmsMessageSupport.REJECTED; + +@Service +public class QueueReceiveService { + + private final Logger LOGGER = LoggerFactory.getLogger(QueueReceiveService.class); + private static final String QUEUE_NAME = "dlq-sample"; + private static final String DEAD_LETTER_QUEUE_NAME_SUFFIX = "/$deadletterqueue"; + private static final String DEAD_LETTER_QUEUE_NAME = QUEUE_NAME + DEAD_LETTER_QUEUE_NAME_SUFFIX; + + @JmsListener(destination = QUEUE_NAME, containerFactory = "jmsListenerContainerFactory") + public void receiveMessage(JmsObjectMessage message) throws JMSException { + User user = (User) message.getObject(); + LOGGER.info("Received message from queue: {}.", user); + if (user.getName().toLowerCase().contains("invalid")) { + message.setIntProperty(JmsMessageSupport.JMS_AMQP_ACK_TYPE, REJECTED); + message.acknowledge(); + LOGGER.info("Move message into dead letter queue: {}.", user); + } + } + + @JmsListener(destination = DEAD_LETTER_QUEUE_NAME, containerFactory = "jmsListenerContainerFactory") + public void receiveDeadLetterMessage(JmsObjectMessage message) throws JMSException { + User user = (User) message.getObject(); + LOGGER.info("Received message from dead letter queue: {}.", user); + } +} diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/QueueSendController.java b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/QueueSendController.java new file mode 100644 index 000000000..356cc75e6 --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/QueueSendController.java @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.sample.jms.queue; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.jms.core.JmsTemplate; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class QueueSendController { + + private static final Logger LOGGER = LoggerFactory.getLogger(QueueSendController.class); + private static final String QUEUE_NAME = "dlq-sample"; + private final JmsTemplate jmsTemplate; + + public QueueSendController(JmsTemplate jmsTemplate) { + this.jmsTemplate = jmsTemplate; + } + + @PostMapping("/queue") + public String postMessage(@RequestParam("message") String message) { + LOGGER.info("Sending message: {}", message); + jmsTemplate.convertAndSend(QUEUE_NAME, new User(message)); + return message; + } +} diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/ServiceBusJMSDLQQueueApplication.java b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/ServiceBusJMSDLQQueueApplication.java new file mode 100644 index 000000000..6c99194ce --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/ServiceBusJMSDLQQueueApplication.java @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.sample.jms.queue; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.jms.annotation.EnableJms; + +@SpringBootApplication +@EnableJms +public class ServiceBusJMSDLQQueueApplication { + + public static void main(String[] args) { + SpringApplication.run(ServiceBusJMSDLQQueueApplication.class, args); + } +} diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/User.java b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/User.java new file mode 100644 index 000000000..7a6482f31 --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/java/com/azure/spring/sample/jms/queue/User.java @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.sample.jms.queue; + +import java.io.Serializable; + +public class User implements Serializable { + + private static final long serialVersionUID = -295422703255886286L; + private String name; + + User(String name) { + setName(name); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "User{" + + "name='" + name + '\'' + + '}'; + } + +} diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/resources/application.yaml b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/resources/application.yaml new file mode 100644 index 000000000..219356192 --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/resources/application.yaml @@ -0,0 +1,12 @@ +spring: + application: + name: servicebus-jms-dlq-queue + jms: + listener: + session: + acknowledge-mode: CLIENT + transacted: false + servicebus: + passwordless-enabled: true + namespace: ${SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE} + pricing-tier: ${PRICING_TIER} diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/resources/log4j.properties b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/resources/log4j.properties new file mode 100644 index 000000000..0e61ac79e --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/src/main/resources/log4j.properties @@ -0,0 +1,7 @@ +log4j.rootLogger=ERROR, stdout +log4j.logger.org.apache.qpid.jms=ERROR + +# CONSOLE appender +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] - %-5p %-30.30c{1} - %m%n diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/main.tf b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/main.tf new file mode 100644 index 000000000..e3a15b012 --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/main.tf @@ -0,0 +1,77 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "3.9.0" + } + azurecaf = { + source = "aztfmod/azurecaf" + version = "1.2.16" + } + } +} + +provider "azurerm" { + features {} +} + +data "azurerm_client_config" "current" { +} + +resource "azurecaf_name" "resource_group" { + name = var.application_name + resource_type = "azurerm_resource_group" + random_length = 5 + clean_input = true +} + +resource "azurerm_resource_group" "main" { + name = azurecaf_name.resource_group.result + location = var.location + + tags = { + "terraform" = "true" + "application-name" = var.application_name + "spring-cloud-azure-sample" = var.sample_tag_value + } +} + +resource "azurecaf_name" "azurecaf_name_servicebus" { + name = var.application_name + resource_type = "azurerm_servicebus_namespace" + random_length = 5 + clean_input = true +} + +resource "azurerm_servicebus_namespace" "servicebus_namespace" { + name = azurecaf_name.azurecaf_name_servicebus.result + location = var.location + resource_group_name = azurerm_resource_group.main.name + + sku = var.pricing_tier + zone_redundant = false + + tags = { + terraform = "true" + application-name = var.application_name + spring-cloud-azure-sample = var.sample_tag_value + } +} + +resource "azurerm_servicebus_queue" "queue" { + name = "dlq-sample" + namespace_id = azurerm_servicebus_namespace.servicebus_namespace.id + + enable_partitioning = false + max_delivery_count = 10 + lock_duration = "PT30S" + max_size_in_megabytes = 1024 + requires_session = false + default_message_ttl = "P14D" +} + +resource "azurerm_role_assignment" "role_service_bus_data_owner" { + scope = azurerm_servicebus_namespace.servicebus_namespace.id + role_definition_name = "Azure Service Bus Data Owner" + principal_id = data.azurerm_client_config.current.object_id +} diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/outputs.tf b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/outputs.tf new file mode 100644 index 000000000..4a543007d --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/outputs.tf @@ -0,0 +1,15 @@ +output "SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE" { + value = azurerm_servicebus_namespace.servicebus_namespace.name + description = "The name of servicebus namespace." + sensitive = true +} + +output "PRICING_TIER" { + value = var.pricing_tier + description = "The pricing tier of Service Bus." +} + +output "RESOURCE_GROUP_NAME" { + value = azurerm_resource_group.main.name + description = "The resource group name." +} diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/setup_env.ps1 b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/setup_env.ps1 new file mode 100644 index 000000000..15ddd1f2d --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/setup_env.ps1 @@ -0,0 +1,5 @@ +$env:SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE=$(terraform -chdir=terraform output -raw SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE) +$env:PRICING_TIER=$(terraform -chdir=terraform output -raw PRICING_TIER) + +echo SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE=$env:SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE +echo PRICING_TIER=$env:PRICING_TIER diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/setup_env.sh b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/setup_env.sh new file mode 100644 index 000000000..439267caa --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/setup_env.sh @@ -0,0 +1,5 @@ +export SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE=$(terraform -chdir=./terraform output -raw SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE) +export PRICING_TIER=$(terraform -chdir=./terraform output -raw PRICING_TIER) + +echo SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE=$SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE +echo PRICING_TIER=$PRICING_TIER diff --git a/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/variables.tf b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/variables.tf new file mode 100644 index 000000000..25757844d --- /dev/null +++ b/servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue/terraform/variables.tf @@ -0,0 +1,23 @@ +variable "application_name" { + type = string + description = "The name of your application." + default = "servicebus-jms-dlq-queue" +} + +variable "location" { + type = string + description = "The Azure region where all resources in this example should be created." + default = "eastus" +} + +variable "sample_tag_value" { + type = string + description = "The value of spring-cloud-azure-sample tag." + default = "true" +} + +variable "pricing_tier" { + type = string + description = "The pricing tier of Service Bus." + default = "Standard" +}