use of com.google.cloud.pubsub.v1.stub.SubscriberStub in project spring-cloud-gcp by spring-cloud.
the class PubSubTemplate method pull.
/**
* Pulls messages synchronously, on demand, using the pull request in argument.
*
* <p>This method acknowledges all received messages.
* @param pullRequest pull request containing the subscription name
* @return the list of {@link PubsubMessage} containing the headers and payload
*/
private List<PubsubMessage> pull(PullRequest pullRequest, RetrySettings retrySettings) {
Assert.notNull(pullRequest, "The pull request cannot be null.");
try {
SubscriberStub subscriber = this.subscriberFactory.createSubscriberStub(retrySettings);
Assert.notNull(subscriber, "A SubscriberStub is needed to execute the pull request.");
PullResponse pullResponse = subscriber.pullCallable().call(pullRequest);
// Ack received messages.
if (pullResponse.getReceivedMessagesCount() > 0) {
List<String> ackIds = pullResponse.getReceivedMessagesList().stream().map(ReceivedMessage::getAckId).collect(Collectors.toList());
AcknowledgeRequest acknowledgeRequest = AcknowledgeRequest.newBuilder().setSubscription(pullRequest.getSubscription()).addAllAckIds(ackIds).build();
subscriber.acknowledgeCallable().call(acknowledgeRequest);
}
return pullResponse.getReceivedMessagesList().stream().map(ReceivedMessage::getMessage).collect(Collectors.toList());
} catch (Exception ioe) {
throw new PubSubException("Error pulling messages from subscription " + pullRequest.getSubscription() + ".", ioe);
}
}
Aggregations