Skip to content

Commit c738b12

Browse files
Freshness pass.
1 parent 6fcff4e commit c738b12

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

articles/service-bus-messaging/service-bus-java-how-to-use-queues.md

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
---
2-
title: Get started with Azure Service Bus queues (Java)
2+
title: 'Quickstart: Get Started with Azure Service Bus Queues (Java)'
33
description: This tutorial shows you how to send messages to and receive messages from Azure Service Bus queues using the Java programming language.
4-
ms.date: 02/28/2024
4+
ms.date: 06/12/2025
55
ms.topic: quickstart
66
ms.devlang: java
77
ms.custom: passwordless-java, devx-track-extended-java
8+
#customer intent: As a developer, I want to learn how to send and receive messages with Azure Service Bus queues by using the Java.
89
---
910

1011
# Send messages to and receive messages from Azure Service Bus queues (Java)
12+
1113
> [!div class="op_single_selector" title1="Select the programming language:"]
1214
> * [C#](service-bus-dotnet-get-started-with-queues.md)
1315
> * [Java](service-bus-java-how-to-use-queues.md)
1416
> * [JavaScript](service-bus-nodejs-how-to-use-queues.md)
1517
> * [Python](service-bus-python-how-to-use-queues.md)
1618
17-
In this quickstart, you create a Java app to send messages to and receive messages from an Azure Service Bus queue.
19+
This quick start provides step-by-step instructions for a simple scenario of sending messages to a Service Bus queue and receiving them. You can find prebuilt Java samples for Azure Service Bus in the [Azure SDK for Java repository on GitHub](https://github.com/azure/azure-sdk-for-java/tree/main/sdk/servicebus/azure-messaging-servicebus/src/samples).
1820

19-
> [!NOTE]
20-
> This quick start provides step-by-step instructions for a simple scenario of sending messages to a Service Bus queue and receiving them. You can find pre-built Java samples for Azure Service Bus in the [Azure SDK for Java repository on GitHub](https://github.com/azure/azure-sdk-for-java/tree/main/sdk/servicebus/azure-messaging-servicebus/src/samples).
21+
In this quickstart, you create a Java app to send messages to and receive messages from an Azure Service Bus queue.
2122

2223
> [!TIP]
23-
> If you're working with Azure Service Bus resources in a Spring application, we recommend that you consider [Spring Cloud Azure](/azure/developer/java/spring-framework/) as an alternative. Spring Cloud Azure is an open-source project that provides seamless Spring integration with Azure services. To learn more about Spring Cloud Azure, and to see an example using Service Bus, see [Spring Cloud Stream with Azure Service Bus](/azure/developer/java/spring-framework/configure-spring-cloud-stream-binder-java-app-with-service-bus).
24+
> If you're working with Azure Service Bus resources in a Spring application, we recommend that you consider [Spring Cloud Azure](/azure/developer/java/spring-framework/). Spring Cloud Azure is an open-source project that provides seamless Spring integration with Azure services. To learn more about Spring Cloud Azure, and to see an example using Service Bus, see [Spring Cloud Stream with Azure Service Bus](/azure/developer/java/spring-framework/configure-spring-cloud-stream-binder-java-app-with-service-bus).
2425
2526
## Prerequisites
26-
- An Azure subscription. To complete this tutorial, you need an Azure account. You can activate your [MSDN subscriber benefits](https://azure.microsoft.com/pricing/member-offers/credit-for-visual-studio-subscribers/?WT.mc_id=A85619ABF) or sign up for a [free account](https://azure.microsoft.com/free/?WT.mc_id=A85619ABF).
27-
- Install [Azure SDK for Java][Azure SDK for Java]. If you're using Eclipse, you can install the [Azure Toolkit for Eclipse][Azure Toolkit for Eclipse] that includes the Azure SDK for Java. You can then add the **Microsoft Azure Libraries for Java** to your project. If you're using IntelliJ, see [Install the Azure Toolkit for IntelliJ](/azure/developer/java/toolkit-for-intellij/installation).
2827

28+
- An Azure subscription. To complete this quickstart, you need an Azure account. You can activate your [Monthly Azure credits for Visual Studio subscribers](https://azure.microsoft.com/pricing/member-offers/credit-for-visual-studio-subscribers/?WT.mc_id=A85619ABF) or sign up for a [free account](https://azure.microsoft.com/free/?WT.mc_id=A85619ABF).
29+
30+
- Install [Azure SDK for Java][Azure SDK for Java].
31+
32+
- If you're using Eclipse, you can install the [Azure Toolkit for Eclipse][Azure Toolkit for Eclipse] that includes the Azure SDK for Java. You can then add the **Microsoft Azure Libraries for Java** to your project.
33+
- If you're using IntelliJ, see [Install the Azure Toolkit for IntelliJ](/azure/developer/java/toolkit-for-intellij/installation).
34+
2935
[!INCLUDE [service-bus-create-namespace-portal](./includes/service-bus-create-namespace-portal.md)]
3036

3137
[!INCLUDE [service-bus-create-queue-portal](./includes/service-bus-create-queue-portal.md)]
@@ -34,16 +40,18 @@ In this quickstart, you create a Java app to send messages to and receive messag
3440

3541

3642
## Send messages to a queue
37-
In this section, you'll create a Java console project, and add code to send messages to the queue that you created earlier.
43+
44+
In this section, you create a Java console project, and add code to send messages to the queue that you created earlier.
3845

3946
### Create a Java console project
47+
4048
Create a Java project using Eclipse or a tool of your choice.
4149

4250
### Configure your application to use Service Bus
43-
Add references to Azure Core and Azure Service Bus libraries.
4451

45-
If you're using Eclipse and created a Java console application, convert your Java project to a Maven: right-click the project in the **Package Explorer** window, select **Configure** -> **Convert to Maven project**. Then, add dependencies to these two libraries as shown in the following example.
52+
Add references to Azure Core and Azure Service Bus libraries.
4653

54+
If you're using Eclipse and created a Java console application, convert your Java project to a Maven: right-click the project in the **Package Explorer** window. Select **Configure** > **Convert to Maven project**. Then, add dependencies to these two libraries as shown in the following example.
4755

4856
### [Passwordless (Recommended)](#tab/passwordless)
4957
Update the `pom.xml` file to add dependencies to Azure Service Bus and Azure Identity packages.
@@ -101,7 +109,8 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
101109
import java.util.List;
102110
```
103111
---
104-
2. In the class, define variables to hold connection string and queue name.
112+
113+
1. In the class, define variables to hold connection string and queue name.
105114

106115
### [Passwordless (Recommended)](#tab/passwordless)
107116

@@ -123,7 +132,8 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
123132
> Replace `<NAMESPACE CONNECTION STRING>` with the connection string to your Service Bus namespace, and `<QUEUE NAME>` with the name of the queue.
124133

125134
---
126-
3. Add a method named `sendMessage` in the class to send one message to the queue.
135+
136+
1. Add a method named `sendMessage` in the class to send one message to the queue.
127137

128138
### [Passwordless (Recommended)](#tab/passwordless)
129139

@@ -169,7 +179,8 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
169179
}
170180
```
171181
---
172-
4. Add a method named `createMessages` in the class to create a list of messages. Typically, you get these messages from different parts of your application. Here, we create a list of sample messages.
182+
183+
1. Add a method named `createMessages` in the class to create a list of messages. Typically, you get these messages from different parts of your application. Here, you use a list of sample messages.
173184

174185
```java
175186
static List<ServiceBusMessage> createMessages()
@@ -183,7 +194,8 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
183194
return Arrays.asList(messages);
184195
}
185196
```
186-
5. Add a method named `sendMessageBatch` method to send messages to the queue you created. This method creates a `ServiceBusSenderClient` for the queue, invokes the `createMessages` method to get the list of messages, prepares one or more batches, and sends the batches to the queue.
197+
198+
1. Add a method named `sendMessageBatch` method to send messages to the queue you created. This method creates a `ServiceBusSenderClient` for the queue, invokes the `createMessages` method to get the list of messages, prepares one or more batches, and sends the batches to the queue.
187199

188200
### [Passwordless (Recommended)](#tab/passwordless)
189201

@@ -294,6 +306,7 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
294306
---
295307

296308
## Receive messages from a queue
309+
297310
In this section, you add code to retrieve messages from the queue.
298311

299312
1. Add a method named `receiveMessages` to receive messages from the queue. This method creates a `ServiceBusProcessorClient` for the queue by specifying a handler for processing messages and another one for handling errors. Then, it starts the processor, waits for few seconds, prints the messages that are received, and then stops and closes the processor.
@@ -356,7 +369,8 @@ In this section, you add code to retrieve messages from the queue.
356369
}
357370
```
358371
---
359-
2. Add the `processMessage` method to process a message received from the Service Bus subscription.
372+
373+
1. Add the `processMessage` method to process a message received from the Service Bus subscription.
360374

361375
```java
362376
private static void processMessage(ServiceBusReceivedMessageContext context) {
@@ -365,7 +379,8 @@ In this section, you add code to retrieve messages from the queue.
365379
message.getSequenceNumber(), message.getBody());
366380
}
367381
```
368-
3. Add the `processError` method to handle error messages.
382+
383+
1. Add the `processError` method to handle error messages.
369384

370385
```java
371386
private static void processError(ServiceBusErrorContext context) {
@@ -400,7 +415,8 @@ In this section, you add code to retrieve messages from the queue.
400415
}
401416
}
402417
```
403-
2. Update the `main` method to invoke `sendMessage`, `sendMessageBatch`, and `receiveMessages` methods and to throw `InterruptedException`.
418+
419+
1. Update the `main` method to invoke `sendMessage`, `sendMessageBatch`, and `receiveMessages` methods and to throw `InterruptedException`.
404420

405421
```java
406422
public static void main(String[] args) throws InterruptedException {
@@ -417,17 +433,19 @@ In this section, you add code to retrieve messages from the queue.
417433
1. If you're using Eclipse, right-click the project, select **Export**, expand **Java**, select **Runnable JAR file**, and follow the steps to create a runnable JAR file.
418434
1. If you're signed into the machine using a user account that's different from the user account added to the **Azure Service Bus Data Owner** role, follow these steps. Otherwise, skip this step and move on to run the Jar file in the next step.
419435
420-
1. [Install Azure CLI](/cli/azure/install-azure-cli-windows) on your machine.
421-
1. Run the following CLI command to sign in to Azure. Use the same user account that you added to the **Azure Service Bus Data Owner** role.
436+
1. [Install Azure CLI](/cli/azure/install-azure-cli-windows) on your machine.
437+
1. Run the following CLI command to sign in to Azure. Use the same user account that you added to the **Azure Service Bus Data Owner** role.
438+
439+
```azurecli
440+
az login
441+
```
422442
423-
```azurecli
424-
az login
425-
```
426443
1. Run the Jar file using the following command.
427444
428445
```java
429446
java -jar <JAR FILE NAME>
430447
```
448+
431449
1. You see the following output in the console window.
432450
433451
```console
@@ -458,15 +476,14 @@ Stopping and closing the processor
458476
459477
On the **Overview** page for the Service Bus namespace in the Azure portal, you can see **incoming** and **outgoing** message count. Wait for a minute or so and then refresh the page to see the latest values.
460478
461-
:::image type="content" source="./media/service-bus-java-how-to-use-queues/overview-incoming-outgoing-messages.png" alt-text="Incoming and outgoing message count" lightbox="./media/service-bus-java-how-to-use-queues/overview-incoming-outgoing-messages.png":::
479+
:::image type="content" source="./media/service-bus-java-how-to-use-queues/overview-incoming-outgoing-messages.png" alt-text="Screenshot shows the incoming and outgoing message count." lightbox="./media/service-bus-java-how-to-use-queues/overview-incoming-outgoing-messages.png":::
462480
463481
Select the queue on this **Overview** page to navigate to the **Service Bus Queue** page. You see the **incoming** and **outgoing** message count on this page too. You also see other information such as the **current size** of the queue, **maximum size**, **active message count**, and so on.
464482
465-
:::image type="content" source="./media/service-bus-java-how-to-use-queues/queue-details.png" alt-text="Queue details" lightbox="./media/service-bus-java-how-to-use-queues/queue-details.png":::
466-
467-
483+
:::image type="content" source="./media/service-bus-java-how-to-use-queues/queue-details.png" alt-text="Screenshot shows the queue details for the messages for this queue." lightbox="./media/service-bus-java-how-to-use-queues/queue-details.png":::
468484
469485
## Next Steps
486+
470487
See the following documentation and samples:
471488
472489
- [Azure Service Bus client library for Java - Readme](https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/servicebus/azure-messaging-servicebus/README.md)

0 commit comments

Comments
 (0)