Search in sources :

Example 1 with SubscriberStub

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);
    }
}
Also used : PullResponse(com.google.pubsub.v1.PullResponse) AcknowledgeRequest(com.google.pubsub.v1.AcknowledgeRequest) SubscriberStub(com.google.cloud.pubsub.v1.stub.SubscriberStub) ByteString(com.google.protobuf.ByteString)

Aggregations

SubscriberStub (com.google.cloud.pubsub.v1.stub.SubscriberStub)1 ByteString (com.google.protobuf.ByteString)1 AcknowledgeRequest (com.google.pubsub.v1.AcknowledgeRequest)1 PullResponse (com.google.pubsub.v1.PullResponse)1