Skip to content

Commit 4fd4740

Browse files
Merge pull request #267579 from spelluru/sbusjava0228
No need of CountDownLatch
2 parents 57c7e9b + 57bbb10 commit 4fd4740

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: 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: 04/12/2023
4+
ms.date: 02/28/2024
55
ms.topic: quickstart
66
ms.devlang: java
77
ms.custom: passwordless-java, devx-track-extended-java
@@ -34,7 +34,7 @@ In this quickstart, you create a Java app to send messages to and receive messag
3434

3535

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

3939
### Create a Java console project
4040
Create a Java project using Eclipse or a tool of your choice.
@@ -86,7 +86,6 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
8686
import com.azure.messaging.servicebus.*;
8787
import com.azure.identity.*;
8888

89-
import java.util.concurrent.CountDownLatch;
9089
import java.util.concurrent.TimeUnit;
9190
import java.util.Arrays;
9291
import java.util.List;
@@ -97,7 +96,6 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
9796
```java
9897
import com.azure.messaging.servicebus.*;
9998

100-
import java.util.concurrent.CountDownLatch;
10199
import java.util.concurrent.TimeUnit;
102100
import java.util.Arrays;
103101
import java.util.List;
@@ -310,8 +308,6 @@ In this section, you add code to retrieve messages from the queue.
310308
// handles received messages
311309
static void receiveMessages() throws InterruptedException
312310
{
313-
CountDownLatch countdownLatch = new CountDownLatch(1);
314-
315311
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
316312
.build();
317313

@@ -320,8 +316,8 @@ In this section, you add code to retrieve messages from the queue.
320316
.credential(credential)
321317
.processor()
322318
.queueName(queueName)
323-
.processMessage(QueueTest::processMessage)
324-
.processError(context -> processError(context, countdownLatch))
319+
.processMessage(context -> processMessage(context))
320+
.processError(context -> processError(context))
325321
.buildProcessorClient();
326322

327323
System.out.println("Starting the processor");
@@ -342,15 +338,13 @@ In this section, you add code to retrieve messages from the queue.
342338
// handles received messages
343339
static void receiveMessages() throws InterruptedException
344340
{
345-
CountDownLatch countdownLatch = new CountDownLatch(1);
346-
347341
// Create an instance of the processor through the ServiceBusClientBuilder
348342
ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder()
349343
.connectionString(connectionString)
350344
.processor()
351345
.queueName(queueName)
352346
.processMessage(QueueTest::processMessage)
353-
.processError(context -> processError(context, countdownLatch))
347+
.processError(context -> processError(context))
354348
.buildProcessorClient();
355349

356350
System.out.println("Starting the processor");
@@ -374,7 +368,7 @@ In this section, you add code to retrieve messages from the queue.
374368
3. Add the `processError` method to handle error messages.
375369

376370
```java
377-
private static void processError(ServiceBusErrorContext context, CountDownLatch countdownLatch) {
371+
private static void processError(ServiceBusErrorContext context) {
378372
System.out.printf("Error when receiving messages from namespace: '%s'. Entity: '%s'%n",
379373
context.getFullyQualifiedNamespace(), context.getEntityPath());
380374

@@ -391,8 +385,6 @@ In this section, you add code to retrieve messages from the queue.
391385
|| reason == ServiceBusFailureReason.UNAUTHORIZED) {
392386
System.out.printf("An unrecoverable error occurred. Stopping processing with reason %s: %s%n",
393387
reason, exception.getMessage());
394-
395-
countdownLatch.countDown();
396388
} else if (reason == ServiceBusFailureReason.MESSAGE_LOCK_LOST) {
397389
System.out.printf("Message lock lost for message: %s%n", context.getException());
398390
} else if (reason == ServiceBusFailureReason.SERVICE_BUSY) {
@@ -423,7 +415,7 @@ In this section, you add code to retrieve messages from the queue.
423415
### [Passwordless (Recommended)](#tab/passwordless)
424416

425417
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.
426-
1. If you are 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.
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.
427419
428420
1. [Install Azure CLI](/cli/azure/install-azure-cli-windows) on your machine.
429421
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.
@@ -464,7 +456,7 @@ Stopping and closing the processor
464456
```
465457
---
466458
467-
On the **Overview** page for the Service Bus namespace in the Azure portal, you can see **incoming** and **outgoing** message count. You may need to wait for a minute or so and then refresh the page to see the latest values.
459+
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.
468460
469461
:::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":::
470462

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Get started with Azure Service Bus topics (Java)
33
description: This tutorial shows you how to send messages to Azure Service Bus topics and receive messages from topics' subscriptions using the Java programming language.
4-
ms.date: 04/12/2023
4+
ms.date: 02/28/2024
55
ms.topic: quickstart
66
ms.devlang: java
77
ms.custom: mode-api, devx-track-extended-java
@@ -86,7 +86,6 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
8686
import com.azure.messaging.servicebus.*;
8787
import com.azure.identity.*;
8888

89-
import java.util.concurrent.CountDownLatch;
9089
import java.util.concurrent.TimeUnit;
9190
import java.util.Arrays;
9291
import java.util.List;
@@ -97,7 +96,6 @@ Update the `pom.xml` file to add a dependency to the Azure Service Bus package.
9796
```java
9897
import com.azure.messaging.servicebus.*;
9998

100-
import java.util.concurrent.CountDownLatch;
10199
import java.util.concurrent.TimeUnit;
102100
import java.util.Arrays;
103101
import java.util.List;
@@ -311,8 +309,6 @@ In this section, you add code to retrieve messages from a subscription to the to
311309
// handles received messages
312310
static void receiveMessages() throws InterruptedException
313311
{
314-
CountDownLatch countdownLatch = new CountDownLatch(1);
315-
316312
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
317313
.build();
318314

@@ -323,8 +319,8 @@ In this section, you add code to retrieve messages from a subscription to the to
323319
.processor()
324320
.topicName(topicName)
325321
.subscriptionName(subName)
326-
.processMessage(ServiceBusTopicTest::processMessage)
327-
.processError(context -> processError(context, countdownLatch))
322+
.processMessage(context -> processMessage(context))
323+
.processError(context -> processError(context))
328324
.buildProcessorClient();
329325

330326
System.out.println("Starting the processor");
@@ -345,16 +341,14 @@ In this section, you add code to retrieve messages from a subscription to the to
345341
// handles received messages
346342
static void receiveMessages() throws InterruptedException
347343
{
348-
CountDownLatch countdownLatch = new CountDownLatch(1);
349-
350344
// Create an instance of the processor through the ServiceBusClientBuilder
351345
ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder()
352346
.connectionString(connectionString)
353347
.processor()
354348
.topicName(topicName)
355349
.subscriptionName(subName)
356-
.processMessage(ServiceBusTopicTest::processMessage)
357-
.processError(context -> processError(context, countdownLatch))
350+
.processMessage(context -> processMessage(context))
351+
.processError(context -> processError(context))
358352
.buildProcessorClient();
359353

360354
System.out.println("Starting the processor");
@@ -378,7 +372,7 @@ In this section, you add code to retrieve messages from a subscription to the to
378372
3. Add the `processError` method to handle error messages.
379373

380374
```java
381-
private static void processError(ServiceBusErrorContext context, CountDownLatch countdownLatch) {
375+
private static void processError(ServiceBusErrorContext context) {
382376
System.out.printf("Error when receiving messages from namespace: '%s'. Entity: '%s'%n",
383377
context.getFullyQualifiedNamespace(), context.getEntityPath());
384378

@@ -395,8 +389,6 @@ In this section, you add code to retrieve messages from a subscription to the to
395389
|| reason == ServiceBusFailureReason.UNAUTHORIZED) {
396390
System.out.printf("An unrecoverable error occurred. Stopping processing with reason %s: %s%n",
397391
reason, exception.getMessage());
398-
399-
countdownLatch.countDown();
400392
} else if (reason == ServiceBusFailureReason.MESSAGE_LOCK_LOST) {
401393
System.out.printf("Message lock lost for message: %s%n", context.getException());
402394
} else if (reason == ServiceBusFailureReason.SERVICE_BUSY) {
@@ -428,7 +420,7 @@ Run the program to see the output similar to the following output:
428420
### [Passwordless (Recommended)](#tab/passwordless)
429421

430422
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.
431-
1. If you are 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.
423+
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.
432424
433425
1. [Install Azure CLI](/cli/azure/install-azure-cli-windows) on your machine.
434426
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.
@@ -466,7 +458,7 @@ Processing message. Session: 7bd3bd3e966a40ebbc9b29b082da14bb, Sequence #: 4. Co
466458
```
467459
---
468460
469-
On the **Overview** page for the Service Bus namespace in the Azure portal, you can see **incoming** and **outgoing** message count. You may need to wait for a minute or so and then refresh the page to see the latest values.
461+
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.
470462
471463
:::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":::
472464

0 commit comments

Comments
 (0)