Skip to content

Commit 98b7692

Browse files
authored
Add sample for DLQ consumption of Service Bus Queue using Spring JMS (#771)
1 parent 7bb00fa commit 98b7692

File tree

15 files changed

+547
-13
lines changed

15 files changed

+547
-13
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,20 @@
102102

103103
### 2.10 Azure Service Bus
104104

105-
| Sample Project | Spring Boot 3.x | Spring Boot 2.x |
106-
|-------------------------------|-----------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
107-
| 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) |
108-
| 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) |
109-
| 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) |
110-
| 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) |
111-
| 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) |
112-
| 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) |
113-
| 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) |
114-
| 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) |
115-
| 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) |
116-
| 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) |
117-
| 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) |
105+
| Sample Project | Spring Boot 3.x | Spring Boot 2.x |
106+
|-------------------------------|----------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
107+
| 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) |
108+
| 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) |
109+
| servicebus-jms-dlq-queue |[link](servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue) ||
110+
| 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) |
111+
| 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) |
112+
| 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) |
113+
| 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) |
114+
| 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) |
115+
| 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) |
116+
| 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) |
117+
| 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) |
118+
| 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) |
118119

119120
### 2.11 Azure Storage
120121

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<module>postgresql/spring-cloud-azure-starter-jdbc-postgresql/spring-cloud-azure-postgresql-sample</module>
6161
<module>servicebus/spring-cloud-azure-starter-servicebus/servicebus-queue</module>
6262
<module>servicebus/spring-cloud-azure-starter-servicebus/servicebus-topic</module>
63+
<module>servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-dlq-queue</module>
6364
<module>servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-queue</module>
6465
<module>servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-topic</module>
6566
<module>servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder</module>
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- java
5+
products:
6+
- azure-service-bus
7+
name: Sending and Receiving Message (DLQ) by Azure Service Bus (Queue) And Jms using Passwordless Connections
8+
description: This sample demonstrates how to send and receive message (DLQ) by Azure Service Bus (queue) and JMS using passwordless connections
9+
---
10+
11+
# Sending and Receiving (DLQ) Message by Azure Service Bus (Queue) And Jms using Passwordless connections
12+
13+
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.
14+
15+
16+
## What You Will Build
17+
You will build an application using Spring JMS to send and receive messages (DLQ) for Azure Service Bus Queue.
18+
19+
## What You Need
20+
21+
- [An Azure subscription](https://azure.microsoft.com/free/)
22+
- [Terraform](https://www.terraform.io/)
23+
- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli)
24+
- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later
25+
- Maven
26+
- You can also import the code straight into your IDE:
27+
- [IntelliJ IDEA](https://www.jetbrains.com/idea/download)
28+
29+
## Provision Azure Resources Required to Run This Sample
30+
This sample will create Azure resources using Terraform. If you choose to run it without using Terraform to provision resources, please pay attention to:
31+
> [!IMPORTANT]
32+
> If you choose to use a security principal to authenticate and authorize with Microsoft Entra ID for accessing an Azure resource
33+
> 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.
34+
35+
### Authenticate Using the Azure CLI
36+
Terraform must authenticate to Azure to create infrastructure.
37+
38+
In your terminal, use the Azure CLI tool to setup your account permissions locally.
39+
40+
```shell
41+
az login
42+
```
43+
44+
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.
45+
46+
```shell
47+
You have logged in. Now let us find all the subscriptions to which you have access...
48+
49+
[
50+
{
51+
"cloudName": "AzureCloud",
52+
"homeTenantId": "home-Tenant-Id",
53+
"id": "subscription-id",
54+
"isDefault": true,
55+
"managedByTenants": [],
56+
"name": "Subscription-Name",
57+
"state": "Enabled",
58+
"tenantId": "0envbwi39-TenantId",
59+
"user": {
60+
"name": "[email protected]",
61+
"type": "user"
62+
}
63+
}
64+
]
65+
```
66+
67+
If you have more than one subscription, specify the subscription-id you want to use with command below:
68+
```shell
69+
az account set --subscription <your-subscription-id>
70+
```
71+
72+
### Provision the Resources
73+
74+
After login Azure CLI with your account, now you can use the terraform script to create Azure Resources.
75+
76+
#### Run with Bash
77+
78+
```shell
79+
# In the root directory of the sample
80+
# Initialize your Terraform configuration
81+
terraform -chdir=./terraform init
82+
83+
# Apply your Terraform Configuration
84+
terraform -chdir=./terraform apply -auto-approve
85+
86+
```
87+
88+
#### Run with Powershell
89+
90+
```shell
91+
# In the root directory of the sample
92+
# Initialize your Terraform configuration
93+
terraform -chdir=terraform init
94+
95+
# Apply your Terraform Configuration
96+
terraform -chdir=terraform apply -auto-approve
97+
98+
```
99+
100+
It may take a few minutes to run the script. After successful running, you will see prompt information like below:
101+
102+
```shell
103+
azurecaf_name.azurecaf_name_servicebus: Creating...
104+
azurecaf_name.resource_group: Creating...
105+
azurecaf_name.azurecaf_name_servicebus: Creation complete after 0s [id=qikkuohqscnhospw]
106+
azurecaf_name.resource_group: Creation complete after 0s [id=hsgmdikkcuwtxecs]
107+
azurerm_resource_group.main: Creating...
108+
azurerm_resource_group.main: Creation complete after 5s ...
109+
azurerm_servicebus_namespace.servicebus_namespace: Creating...
110+
...
111+
azurerm_servicebus_namespace.servicebus_namespace: Creation complete after ...
112+
azurerm_servicebus_queue.queue: Creating...
113+
azurerm_servicebus_queue.queue: Creation complete ...
114+
115+
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
116+
117+
Outputs:
118+
119+
...
120+
121+
```
122+
123+
You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created.
124+
125+
### Export Output to Your Local Environment
126+
Running the command below to export environment values:
127+
128+
#### Run with Bash
129+
130+
```shell
131+
source ./terraform/setup_env.sh
132+
```
133+
134+
#### Run with Powershell
135+
136+
```shell
137+
terraform\setup_env.ps1
138+
```
139+
140+
If you want to run the sample in debug mode, you can save the output value.
141+
142+
```shell
143+
SERVICEBUS_NAMESPACE_NAME_DLQ_QUQUE=...
144+
PRICING_TIER=...
145+
```
146+
147+
## Run Locally
148+
149+
### Run the sample with Maven
150+
151+
In your terminal, run `mvn clean spring-boot:run`.
152+
153+
```shell
154+
mvn clean spring-boot:run
155+
```
156+
157+
### Run the sample in IDEs
158+
159+
You can debug your sample by adding the saved output values to the tool's environment variables or the sample's `application.yaml` file.
160+
161+
* 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).
162+
163+
* 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/).
164+
165+
## Verify This Sample
166+
167+
1. Send a POST request with a normal message to Service Bus queue:
168+
169+
```
170+
curl -X POST http://localhost:8080/queue?message=hello
171+
```
172+
173+
Verify in your app's logs that a similar message was posted:
174+
175+
```
176+
Sending message: hello
177+
...
178+
Received message from queue: User{name='hello'}.
179+
```
180+
181+
2. Send a POST request with an invalid message to DLQ of theService Bus queue:
182+
183+
```
184+
curl -X POST http://localhost:8080/queue?message=hello-invalid
185+
```
186+
187+
Verify in your app's logs that a similar message was posted:
188+
189+
```
190+
Sending message: hello-invalid
191+
Received message from queue: User{name='hello-invalid'}.
192+
Move message into dead letter queue: User{name='hello-invalid'}.
193+
Received message from dead letter queue: User{name='hello-invalid'}.
194+
```
195+
196+
## Clean Up Resources
197+
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.
198+
199+
The terraform destroy command terminates resources managed by your Terraform project.
200+
To destroy the resources you created.
201+
202+
#### Run with Bash
203+
204+
```shell
205+
terraform -chdir=./terraform destroy -auto-approve
206+
```
207+
208+
#### Run with Powershell
209+
210+
```shell
211+
terraform -chdir=terraform destroy -auto-approve
212+
```
213+
214+
## Deploy to Azure Spring Apps
215+
216+
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).
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>3.4.1</version>
11+
<relativePath/> <!-- lookup parent from repository -->
12+
</parent>
13+
14+
<groupId>com.azure.spring</groupId>
15+
<artifactId>servicebus-jms-dlq-queue</artifactId>
16+
<version>1.0.0</version>
17+
<packaging>jar</packaging>
18+
19+
<name>Communicate to DLQ queue of Service Bus Queue via JMS API</name>
20+
21+
<properties>
22+
<maven.compiler.source>17</maven.compiler.source>
23+
<maven.compiler.target>17</maven.compiler.target>
24+
<version.spring.cloud.azure>5.19.0</version.spring.cloud.azure>
25+
</properties>
26+
27+
<dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>com.azure.spring</groupId>
31+
<artifactId>spring-cloud-azure-dependencies</artifactId>
32+
<version>${version.spring.cloud.azure}</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-web</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>com.azure.spring</groupId>
47+
<artifactId>spring-cloud-azure-starter-servicebus-jms</artifactId>
48+
</dependency>
49+
</dependencies>
50+
51+
</project>

0 commit comments

Comments
 (0)