Search in sources :

Example 51 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project flink by apache.

the class PubsubHelper method acknowledgeIds.

private void acknowledgeIds(SubscriberStub subscriber, String subscriptionName, List<ReceivedMessage> receivedMessages) {
    if (receivedMessages.isEmpty()) {
        return;
    }
    List<String> ackIds = receivedMessages.stream().map(ReceivedMessage::getAckId).collect(Collectors.toList());
    // acknowledge received messages
    AcknowledgeRequest acknowledgeRequest = AcknowledgeRequest.newBuilder().setSubscription(subscriptionName).addAllAckIds(ackIds).build();
    // use acknowledgeCallable().futureCall to asynchronously perform this operation
    subscriber.acknowledgeCallable().call(acknowledgeRequest);
}
Also used : AcknowledgeRequest(com.google.pubsub.v1.AcknowledgeRequest)

Example 52 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project flink by apache.

the class PubsubHelper method subscribeToSubscription.

public Subscriber subscribeToSubscription(String project, String subscription, MessageReceiver messageReceiver) {
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(project, subscription);
    Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageReceiver).setChannelProvider(channelProvider).setCredentialsProvider(EmulatorCredentialsProvider.create()).build();
    subscriber.startAsync();
    return subscriber;
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Subscriber(com.google.cloud.pubsub.v1.Subscriber)

Example 53 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project spring-cloud-gcp by spring-cloud.

the class PubSubSampleApplicationTests method getMessagesFromSubscription.

private List<String> getMessagesFromSubscription(String subscriptionName) {
    String projectSubscriptionName = ProjectSubscriptionName.format(projectName, subscriptionName);
    PullRequest pullRequest = PullRequest.newBuilder().setReturnImmediately(true).setMaxMessages(10).setSubscription(projectSubscriptionName).build();
    PullResponse pullResponse = subscriptionAdminClient.getStub().pullCallable().call(pullRequest);
    return pullResponse.getReceivedMessagesList().stream().map((message) -> message.getMessage().getData().toStringUtf8()).collect(Collectors.toList());
}
Also used : Topic(com.google.pubsub.v1.Topic) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Assume.assumeThat(org.junit.Assume.assumeThat) BeforeClass(org.junit.BeforeClass) WebEnvironment(org.springframework.boot.test.context.SpringBootTest.WebEnvironment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) ListTopicsPagedResponse(com.google.cloud.pubsub.v1.TopicAdminClient.ListTopicsPagedResponse) ServiceOptions(com.google.cloud.ServiceOptions) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) StreamSupport(java.util.stream.StreamSupport) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) SpringRunner(org.springframework.test.context.junit4.SpringRunner) PullResponse(com.google.pubsub.v1.PullResponse) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Awaitility.await(org.awaitility.Awaitility.await) IOException(java.io.IOException) Test(org.junit.Test) PullRequest(com.google.pubsub.v1.PullRequest) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) LocalServerPort(org.springframework.boot.web.server.LocalServerPort) List(java.util.List) Rule(org.junit.Rule) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) OutputCaptureRule(org.springframework.boot.test.system.OutputCaptureRule) TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) ListSubscriptionsPagedResponse(com.google.cloud.pubsub.v1.SubscriptionAdminClient.ListSubscriptionsPagedResponse) Subscription(com.google.pubsub.v1.Subscription) ResponseEntity(org.springframework.http.ResponseEntity) Matchers.is(org.hamcrest.Matchers.is) PushConfig(com.google.pubsub.v1.PushConfig) TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) ProjectName(com.google.pubsub.v1.ProjectName) PullResponse(com.google.pubsub.v1.PullResponse) PullRequest(com.google.pubsub.v1.PullRequest)

Example 54 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project spring-cloud-gcp by spring-cloud.

the class PubSubChannelProvisioner method provisionConsumerDestination.

@Override
public ConsumerDestination provisionConsumerDestination(String topicName, String group, ExtendedConsumerProperties<PubSubConsumerProperties> properties) {
    // topicName may be either the short or fully-qualified version.
    String topicShortName = TopicName.isParsableFrom(topicName) ? TopicName.parse(topicName).getTopic() : topicName;
    Optional<Topic> topic = ensureTopicExists(topicName, properties.getExtension().isAutoCreateResources());
    String subscriptionName;
    Subscription subscription;
    if (StringUtils.hasText(group)) {
        subscriptionName = topicShortName + "." + group;
        subscription = this.pubSubAdmin.getSubscription(subscriptionName);
    } else {
        // Generate anonymous random group since one wasn't provided
        subscriptionName = "anonymous." + topicShortName + "." + UUID.randomUUID().toString();
        subscription = this.pubSubAdmin.createSubscription(subscriptionName, topicName);
        this.anonymousGroupSubscriptionNames.add(subscriptionName);
    }
    if (subscription == null) {
        if (properties.getExtension().isAutoCreateResources()) {
            this.pubSubAdmin.createSubscription(subscriptionName, topicName);
        } else {
            throw new ProvisioningException("Non-existing '" + subscriptionName + "' subscription.");
        }
    } else if (topic.isPresent() && !subscription.getTopic().equals(topic.get().getName())) {
        throw new ProvisioningException("Existing '" + subscriptionName + "' subscription is for a different topic '" + subscription.getTopic() + "'.");
    }
    return new PubSubConsumerDestination(subscriptionName);
}
Also used : ProvisioningException(org.springframework.cloud.stream.provisioning.ProvisioningException) Topic(com.google.pubsub.v1.Topic) Subscription(com.google.pubsub.v1.Subscription)

Example 55 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project spring-cloud-gcp by spring-cloud.

the class PubSubTemplateDocumentationTests method testCreatePublishPullNextAndDelete.

@Test
public void testCreatePublishPullNextAndDelete() {
    pubSubTest((PubSubTemplate pubSubTemplate, String subscriptionName, String topicName) -> {
        // tag::publish[]
        Map<String, String> headers = Collections.singletonMap("key1", "val1");
        pubSubTemplate.publish(topicName, "message", headers).get();
        // end::publish[]
        PubsubMessage pubsubMessage = pubSubTemplate.pullNext(subscriptionName);
        assertThat(pubsubMessage.getData()).isEqualTo(ByteString.copyFromUtf8("message"));
        assertThat(pubsubMessage.getAttributesCount()).isEqualTo(1);
        assertThat(pubsubMessage.getAttributesOrThrow("key1")).isEqualTo("val1");
    });
}
Also used : ByteString(com.google.protobuf.ByteString) PubSubTemplate(org.springframework.cloud.gcp.pubsub.core.PubSubTemplate) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ConvertedAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage) AcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage) Test(org.junit.Test)

Aggregations

SubscriptionName (com.google.pubsub.v1.SubscriptionName)31 Test (org.junit.Test)27 Subscription (com.google.pubsub.v1.Subscription)13 TopicName (com.google.pubsub.v1.TopicName)11 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)10 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)9 ByteString (com.google.protobuf.ByteString)9 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)9 PushConfig (com.google.pubsub.v1.PushConfig)9 StatusRuntimeException (io.grpc.StatusRuntimeException)9 ApiException (com.google.api.gax.grpc.ApiException)8 PubsubMessage (com.google.pubsub.v1.PubsubMessage)7 Subscriber (com.google.cloud.pubsub.v1.Subscriber)5 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)5 Empty (com.google.protobuf.Empty)4 ArrayList (java.util.ArrayList)4 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)3 MessageReceiver (com.google.cloud.pubsub.spi.v1.MessageReceiver)3 Subscriber (com.google.cloud.pubsub.spi.v1.Subscriber)3 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)3