You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
18
20
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.
21
22
22
23
> [!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).
24
25
25
26
## 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).
28
27
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,16 +40,18 @@ In this quickstart, you create a Java app to send messages to and receive messag
34
40
35
41
36
42
## 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.
38
45
39
46
### Create a Java console project
47
+
40
48
Create a Java project using Eclipse or a tool of your choice.
41
49
42
50
### Configure your application to use Service Bus
43
-
Add references to Azure Core and Azure Service Bus libraries.
44
51
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.
46
53
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.
@@ -169,7 +179,8 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
169
179
}
170
180
```
171
181
---
172
-
4.Add a method named `createMessages` in the classto 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 classto create a list of messages. Typically, you get these messages from different parts of your application. Here, you use a list of sample messages.
173
184
174
185
```java
175
186
static List<ServiceBusMessage> createMessages()
@@ -183,7 +194,8 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
183
194
return Arrays.asList(messages);
184
195
}
185
196
```
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.
@@ -294,6 +306,7 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
294
306
---
295
307
296
308
## Receive messages from a queue
309
+
297
310
Inthis section, you add code to retrieve messages from the queue.
298
311
299
312
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.
356
369
}
357
370
```
358
371
---
359
-
2.Add the `processMessage` method to process a message received from the ServiceBus subscription.
372
+
373
+
1.Add the `processMessage` method to process a message received from the ServiceBus subscription.
@@ -417,17 +433,19 @@ In this section, you add code to retrieve messages from the queue.
417
433
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
434
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.
419
435
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
+
```
422
442
423
-
```azurecli
424
-
az login
425
-
```
426
443
1. Run the Jar file using the following command.
427
444
428
445
```java
429
446
java -jar <JAR FILE NAME>
430
447
```
448
+
431
449
1. You see the following output in the console window.
432
450
433
451
```console
@@ -458,15 +476,14 @@ Stopping and closing the processor
458
476
459
477
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.
460
478
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":::
462
480
463
481
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.
:::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":::
468
484
469
485
## Next Steps
486
+
470
487
See the following documentation and samples:
471
488
472
489
- [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