Skip to content

Commit

Permalink
samples: exit early if no messages are returned (#989)
Browse files Browse the repository at this point in the history
* samples: exit early if no messages are pulled

* address kurtis's comments

* update comment
  • Loading branch information
anguillanneuf committed Feb 3, 2022
1 parent 9a1cc0e commit ab828a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Expand Up @@ -60,12 +60,21 @@ public static void subscribeSyncExample(

// Use pullCallable().futureCall to asynchronously perform this operation.
PullResponse pullResponse = subscriber.pullCallable().call(pullRequest);

// 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;
}

List<String> ackIds = new ArrayList<>();
for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) {
// Handle received message
// ...
ackIds.add(message.getAckId());
}

// Acknowledge received messages.
AcknowledgeRequest acknowledgeRequest =
AcknowledgeRequest.newBuilder()
Expand Down
Expand Up @@ -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);
}

Expand Down Expand Up @@ -68,8 +65,14 @@ public static void subscribeSyncWithLeaseExample(
// Use pullCallable().futureCall to asynchronously perform this operation.
PullResponse pullResponse = subscriber.pullCallable().call(pullRequest);

List<String> ackIds = new ArrayList<>();
// 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;
}

List<String> ackIds = new ArrayList<>();
for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) {
ackIds.add(message.getAckId());

Expand Down

0 comments on commit ab828a0

Please sign in to comment.