From ebcae5d1ee14f4af69e4bc20e1a254e9bd0b103b Mon Sep 17 00:00:00 2001 From: Tianzi Cai Date: Wed, 2 Feb 2022 16:23:52 -0800 Subject: [PATCH 1/3] samples: exit early if no messages are pulled --- .../src/main/java/pubsub/SubscribeSyncExample.java | 8 ++++++++ .../java/pubsub/SubscribeSyncWithLeaseExample.java | 10 ++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java b/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java index 1ece29c37..f1dcc6444 100644 --- a/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java +++ b/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java @@ -60,12 +60,20 @@ public static void subscribeSyncExample( // Use pullCallable().futureCall to asynchronously perform this operation. PullResponse pullResponse = subscriber.pullCallable().call(pullRequest); + + // Exit the program if the pull response is empty. + if (pullResponse.getReceivedMessagesList().isEmpty()) { + System.out.println("No message was pulled. Exiting."); + System.exit(0); + } + List ackIds = new ArrayList<>(); for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) { // Handle received message // ... ackIds.add(message.getAckId()); } + // Acknowledge received messages. AcknowledgeRequest acknowledgeRequest = AcknowledgeRequest.newBuilder() diff --git a/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java b/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java index e074aa3fa..0b2375062 100644 --- a/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java +++ b/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java @@ -38,9 +38,6 @@ public static void main(String... args) throws Exception { String subscriptionId = "your-subscription-id"; Integer numOfMessages = 10; - projectId = "tz-playground-bigdata"; - subscriptionId = "uno"; - subscribeSyncWithLeaseExample(projectId, subscriptionId, numOfMessages); } @@ -68,8 +65,13 @@ public static void subscribeSyncWithLeaseExample( // Use pullCallable().futureCall to asynchronously perform this operation. PullResponse pullResponse = subscriber.pullCallable().call(pullRequest); - List ackIds = new ArrayList<>(); + // Exit the program if the pull response is empty. + if (pullResponse.getReceivedMessagesList().isEmpty()) { + System.out.println("No message was pulled. Exiting."); + System.exit(0); + } + List ackIds = new ArrayList<>(); for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) { ackIds.add(message.getAckId()); From f7cc282f26c9a27904f9fd52dda8f268eb9ec34e Mon Sep 17 00:00:00 2001 From: Tianzi Cai Date: Thu, 3 Feb 2022 08:54:47 -0800 Subject: [PATCH 2/3] address kurtis's comments --- .../snippets/src/main/java/pubsub/SubscribeSyncExample.java | 4 ++-- .../src/main/java/pubsub/SubscribeSyncWithLeaseExample.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java b/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java index f1dcc6444..0758091e7 100644 --- a/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java +++ b/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java @@ -61,10 +61,10 @@ public static void subscribeSyncExample( // Use pullCallable().futureCall to asynchronously perform this operation. PullResponse pullResponse = subscriber.pullCallable().call(pullRequest); - // Exit the program if the pull response is empty. + // Discontinue the program if the pull response is empty. if (pullResponse.getReceivedMessagesList().isEmpty()) { System.out.println("No message was pulled. Exiting."); - System.exit(0); + return; } List ackIds = new ArrayList<>(); diff --git a/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java b/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java index 0b2375062..2c5f2d498 100644 --- a/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java +++ b/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java @@ -65,10 +65,10 @@ public static void subscribeSyncWithLeaseExample( // Use pullCallable().futureCall to asynchronously perform this operation. PullResponse pullResponse = subscriber.pullCallable().call(pullRequest); - // Exit the program if the pull response is empty. + // Discontinue the program if the pull response is empty. if (pullResponse.getReceivedMessagesList().isEmpty()) { System.out.println("No message was pulled. Exiting."); - System.exit(0); + return; } List ackIds = new ArrayList<>(); From 3bcf8885d33df55de6edcfa448acdd9a6344d351 Mon Sep 17 00:00:00 2001 From: Tianzi Cai Date: Thu, 3 Feb 2022 08:58:31 -0800 Subject: [PATCH 3/3] update comment --- .../snippets/src/main/java/pubsub/SubscribeSyncExample.java | 3 ++- .../src/main/java/pubsub/SubscribeSyncWithLeaseExample.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java b/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java index 0758091e7..e3a034044 100644 --- a/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java +++ b/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java @@ -61,7 +61,8 @@ public static void subscribeSyncExample( // Use pullCallable().futureCall to asynchronously perform this operation. PullResponse pullResponse = subscriber.pullCallable().call(pullRequest); - // Discontinue the program if the pull response is empty. + // Stop the program if the pull response is empty to avoid acknowledging + // an empty list of ack IDs. if (pullResponse.getReceivedMessagesList().isEmpty()) { System.out.println("No message was pulled. Exiting."); return; diff --git a/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java b/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java index 2c5f2d498..67b604203 100644 --- a/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java +++ b/samples/snippets/src/main/java/pubsub/SubscribeSyncWithLeaseExample.java @@ -65,7 +65,8 @@ public static void subscribeSyncWithLeaseExample( // Use pullCallable().futureCall to asynchronously perform this operation. PullResponse pullResponse = subscriber.pullCallable().call(pullRequest); - // Discontinue the program if the pull response is empty. + // Stop the program if the pull response is empty to avoid acknowledging + // an empty list of ack IDs. if (pullResponse.getReceivedMessagesList().isEmpty()) { System.out.println("No message was pulled. Exiting."); return;