Skip to content

Commit 643757c

Browse files
Merge pull request #301223 from TimShererWithAquent/us439212-02
Freshness Edit: Azure Messaging Service (2 of 10)
2 parents 0cc52eb + de6b0e4 commit 643757c

File tree

1 file changed

+45
-31
lines changed

1 file changed

+45
-31
lines changed

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

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
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.
18-
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).
19+
This quickstart provides step-by-step instructions for a simple scenario of sending messages to a Service Bus queue and receiving them. You create a Java app to send messages to and receive messages from an Azure Service Bus queue. 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).
2120

2221
> [!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).
22+
> 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).
2423
2524
## 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).
2825

26+
- 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).
27+
28+
- Install [Azure SDK for Java][Azure SDK for Java].
29+
30+
- 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.
31+
- If you're using IntelliJ, see [Install the Azure Toolkit for IntelliJ](/azure/developer/java/toolkit-for-intellij/installation).
32+
2933
[!INCLUDE [service-bus-create-namespace-portal](./includes/service-bus-create-namespace-portal.md)]
3034

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

3539

3640
## 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.
41+
42+
In this section, you create a Java console project, and add code to send messages to the queue that you created earlier.
3843

3944
### Create a Java console project
45+
4046
Create a Java project using Eclipse or a tool of your choice.
4147

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

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.
50+
Add references to Azure Core and Azure Service Bus libraries.
4651

52+
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.
4753

4854
### [Passwordless (Recommended)](#tab/passwordless)
4955
Update the `pom.xml` file to add dependencies to Azure Service Bus and Azure Identity packages.
@@ -101,7 +107,8 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
101107
import java.util.List;
102108
```
103109
---
104-
2. In the class, define variables to hold connection string and queue name.
110+
111+
1. In the class, define variables to hold the connection string and queue name.
105112

106113
### [Passwordless (Recommended)](#tab/passwordless)
107114

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

125132
---
126-
3. Add a method named `sendMessage` in the class to send one message to the queue.
133+
134+
1. Add a method named `sendMessage` in the class to send one message to the queue.
127135

128136
### [Passwordless (Recommended)](#tab/passwordless)
129137

@@ -169,7 +177,8 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
169177
}
170178
```
171179
---
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.
180+
181+
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. In this example, you use a list of sample messages.
173182

174183
```java
175184
static List<ServiceBusMessage> createMessages()
@@ -183,7 +192,8 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
183192
return Arrays.asList(messages);
184193
}
185194
```
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.
195+
196+
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.
187197

188198
### [Passwordless (Recommended)](#tab/passwordless)
189199

@@ -294,6 +304,7 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
294304
---
295305

296306
## Receive messages from a queue
307+
297308
In this section, you add code to retrieve messages from the queue.
298309

299310
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.
@@ -302,7 +313,6 @@ In this section, you add code to retrieve messages from the queue.
302313

303314
> [!IMPORTANT]
304315
> - Replace `NAMESPACENAME` with the name of your Service Bus namespace.
305-
> - Replace `QueueTest` in `QueueTest::processMessage` in the code with the name of your class.
306316

307317
```java
308318
// handles received messages
@@ -356,7 +366,8 @@ In this section, you add code to retrieve messages from the queue.
356366
}
357367
```
358368
---
359-
2. Add the `processMessage` method to process a message received from the Service Bus subscription.
369+
370+
1. Add the `processMessage` method to process a message received from the Service Bus subscription.
360371

361372
```java
362373
private static void processMessage(ServiceBusReceivedMessageContext context) {
@@ -365,7 +376,8 @@ In this section, you add code to retrieve messages from the queue.
365376
message.getSequenceNumber(), message.getBody());
366377
}
367378
```
368-
3. Add the `processError` method to handle error messages.
379+
380+
1. Add the `processError` method to handle error messages.
369381

370382
```java
371383
private static void processError(ServiceBusErrorContext context) {
@@ -400,7 +412,8 @@ In this section, you add code to retrieve messages from the queue.
400412
}
401413
}
402414
```
403-
2. Update the `main` method to invoke `sendMessage`, `sendMessageBatch`, and `receiveMessages` methods and to throw `InterruptedException`.
415+
416+
1. Update the `main` method to invoke `sendMessage`, `sendMessageBatch`, and `receiveMessages` methods and to throw `InterruptedException`.
404417

405418
```java
406419
public static void main(String[] args) throws InterruptedException {
@@ -415,19 +428,21 @@ In this section, you add code to retrieve messages from the queue.
415428
### [Passwordless (Recommended)](#tab/passwordless)
416429

417430
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.
418-
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.
431+
1. If you're signed under a user account that's different from the one that you added to the **Azure Service Bus Data Owner** role, follow these steps. Otherwise, move on to run the Jar file in the next step.
432+
433+
1. [Install Azure CLI](/cli/azure/install-azure-cli-windows) on your machine.
434+
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.
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+
```azurecli
437+
az login
438+
```
422439
423-
```azurecli
424-
az login
425-
```
426440
1. Run the Jar file using the following command.
427441
428442
```java
429443
java -jar <JAR FILE NAME>
430444
```
445+
431446
1. You see the following output in the console window.
432447
433448
```console
@@ -458,15 +473,14 @@ Stopping and closing the processor
458473
459474
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.
460475
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":::
462-
463-
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.
476+
:::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":::
464477
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":::
478+
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, and **maximum size**, **active message count**.
466479
480+
:::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":::
467481
482+
## Related content
468483
469-
## Next Steps
470484
See the following documentation and samples:
471485
472486
- [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)