Skip to content

Commit b035b57

Browse files
committed
samples: check for no messages case to avoid error out
1 parent 9a1cc0e commit b035b57

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public static void main(String... args) throws Exception {
3636
String projectId = "your-project-id";
3737
String subscriptionId = "your-subscription-id";
3838
Integer numOfMessages = 10;
39+
projectId = "tz-playground-bigdata";
40+
subscriptionId = "sub-two";
41+
numOfMessages = 10;
3942

4043
subscribeSyncExample(projectId, subscriptionId, numOfMessages);
4144
}
@@ -66,16 +69,21 @@ public static void subscribeSyncExample(
6669
// ...
6770
ackIds.add(message.getAckId());
6871
}
69-
// Acknowledge received messages.
70-
AcknowledgeRequest acknowledgeRequest =
71-
AcknowledgeRequest.newBuilder()
72-
.setSubscription(subscriptionName)
73-
.addAllAckIds(ackIds)
74-
.build();
7572

76-
// Use acknowledgeCallable().futureCall to asynchronously perform this operation.
77-
subscriber.acknowledgeCallable().call(acknowledgeRequest);
78-
System.out.println(pullResponse.getReceivedMessagesList());
73+
if (ackIds.isEmpty()) {
74+
System.out.println("No message was pulled.");
75+
} else {
76+
// Acknowledge received messages.
77+
AcknowledgeRequest acknowledgeRequest =
78+
AcknowledgeRequest.newBuilder()
79+
.setSubscription(subscriptionName)
80+
.addAllAckIds(ackIds)
81+
.build();
82+
83+
// Use acknowledgeCallable().futureCall to asynchronously perform this operation.
84+
subscriber.acknowledgeCallable().call(acknowledgeRequest);
85+
System.out.println(pullResponse.getReceivedMessagesList());
86+
}
7987
}
8088
}
8189
}

0 commit comments

Comments
 (0)