Search in sources :

Example 1 with SubscriberStubSettings

use of com.google.cloud.pubsub.v1.stub.SubscriberStubSettings in project flink by apache.

the class PubsubHelper method pullMessages.

// 
// Mostly copied from the example on https://cloud.google.com/pubsub/docs/pull
// Licensed under the Apache 2.0 License to "Google LLC" from
// https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberSnippets.java.
// 
public List<ReceivedMessage> pullMessages(String projectId, String subscriptionId, int maxNumberOfMessages) throws Exception {
    SubscriberStubSettings subscriberStubSettings = SubscriberStubSettings.newBuilder().setTransportChannelProvider(channelProvider).setCredentialsProvider(EmulatorCredentialsProvider.create()).build();
    try (SubscriberStub subscriber = GrpcSubscriberStub.create(subscriberStubSettings)) {
        String subscriptionName = ProjectSubscriptionName.format(projectId, subscriptionId);
        PullRequest pullRequest = PullRequest.newBuilder().setMaxMessages(maxNumberOfMessages).setSubscription(subscriptionName).build();
        List<ReceivedMessage> receivedMessages = subscriber.pullCallable().call(pullRequest).getReceivedMessagesList();
        acknowledgeIds(subscriber, subscriptionName, receivedMessages);
        return receivedMessages;
    }
}
Also used : PullRequest(com.google.pubsub.v1.PullRequest) SubscriberStub(com.google.cloud.pubsub.v1.stub.SubscriberStub) GrpcSubscriberStub(com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub) ReceivedMessage(com.google.pubsub.v1.ReceivedMessage) SubscriberStubSettings(com.google.cloud.pubsub.v1.stub.SubscriberStubSettings)

Aggregations

GrpcSubscriberStub (com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub)1 SubscriberStub (com.google.cloud.pubsub.v1.stub.SubscriberStub)1 SubscriberStubSettings (com.google.cloud.pubsub.v1.stub.SubscriberStubSettings)1 PullRequest (com.google.pubsub.v1.PullRequest)1 ReceivedMessage (com.google.pubsub.v1.ReceivedMessage)1