Search in sources :

Example 1 with DeadLetterPolicy

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

the class PubSubChannelProvisionerTests method testProvisionConsumerDestination_deadLetterQueue.

@Test
public void testProvisionConsumerDestination_deadLetterQueue() {
    PubSubConsumerProperties.DeadLetterPolicy dlp = new PubSubConsumerProperties.DeadLetterPolicy();
    dlp.setDeadLetterTopic("deadLetterTopic");
    dlp.setMaxDeliveryAttempts(12);
    when(this.pubSubConsumerProperties.getDeadLetterPolicy()).thenReturn(dlp);
    when(this.pubSubAdminMock.getTopic("deadLetterTopic")).thenReturn(null);
    when(this.pubSubAdminMock.createTopic("deadLetterTopic")).thenReturn(Topic.newBuilder().setName("projects/test-project/topics/deadLetterTopic").build());
    this.pubSubChannelProvisioner.provisionConsumerDestination("topic_A", "group_A", this.properties);
    ArgumentCaptor<Subscription.Builder> argCaptor = ArgumentCaptor.forClass(Subscription.Builder.class);
    verify(this.pubSubAdminMock).createSubscription(argCaptor.capture());
    Subscription.Builder sb = argCaptor.getValue();
    assertThat(sb.getName()).isEqualTo("topic_A.group_A");
    assertThat(sb.getTopic()).isEqualTo("topic_A");
    assertThat(sb.getDeadLetterPolicy()).isNotNull();
    DeadLetterPolicy policy = sb.getDeadLetterPolicy();
    assertThat(policy.getDeadLetterTopic()).isEqualTo("projects/test-project/topics/deadLetterTopic");
    assertThat(policy.getMaxDeliveryAttempts()).isEqualTo(12);
}
Also used : PubSubConsumerProperties(com.google.cloud.spring.stream.binder.pubsub.properties.PubSubConsumerProperties) DeadLetterPolicy(com.google.pubsub.v1.DeadLetterPolicy) Subscription(com.google.pubsub.v1.Subscription) Test(org.junit.Test)

Example 2 with DeadLetterPolicy

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

the class PubSubChannelProvisioner method createSubscription.

private Subscription createSubscription(String subscriptionName, String topicName, PubSubConsumerProperties.DeadLetterPolicy deadLetterPolicy, boolean autoCreate) {
    Subscription.Builder builder = Subscription.newBuilder().setName(subscriptionName).setTopic(topicName);
    if (deadLetterPolicy != null) {
        String dlTopicName = deadLetterPolicy.getDeadLetterTopic();
        Assert.hasText(dlTopicName, "Dead letter policy cannot have null or empty topic");
        Topic dlTopic = ensureTopicExists(dlTopicName, autoCreate);
        DeadLetterPolicy.Builder dlpBuilder = DeadLetterPolicy.newBuilder().setDeadLetterTopic(dlTopic.getName());
        Integer maxAttempts = deadLetterPolicy.getMaxDeliveryAttempts();
        if (maxAttempts != null) {
            dlpBuilder.setMaxDeliveryAttempts(maxAttempts);
        }
        builder.setDeadLetterPolicy(dlpBuilder);
    }
    return this.pubSubAdmin.createSubscription(builder);
}
Also used : DeadLetterPolicy(com.google.pubsub.v1.DeadLetterPolicy) Subscription(com.google.pubsub.v1.Subscription) Topic(com.google.pubsub.v1.Topic)

Example 3 with DeadLetterPolicy

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

the class PubSubChannelProvisioner method provisionConsumerDestination.

@Override
public ConsumerDestination provisionConsumerDestination(String topicName, String group, ExtendedConsumerProperties<PubSubConsumerProperties> properties) {
    String subscriptionName;
    Subscription subscription;
    String customName = properties.getExtension().getSubscriptionName();
    boolean autoCreate = properties.getExtension().isAutoCreateResources();
    PubSubConsumerProperties.DeadLetterPolicy deadLetterPolicy = properties.getExtension().getDeadLetterPolicy();
    // topicName may be either the short or fully-qualified version.
    String topicShortName = TopicName.isParsableFrom(topicName) ? TopicName.parse(topicName).getTopic() : topicName;
    Topic topic = ensureTopicExists(topicName, autoCreate);
    if (StringUtils.hasText(customName)) {
        if (StringUtils.hasText(group)) {
            LOGGER.warn("Either subscriptionName or group can be specified, but not both. " + "Using subscriptionName '" + customName + "'.");
        }
        subscription = this.pubSubAdmin.getSubscription(customName);
        subscriptionName = customName;
    } else 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.createSubscription(subscriptionName, topicName, deadLetterPolicy, autoCreate);
        this.anonymousGroupSubscriptionNames.add(subscriptionName);
    }
    if (subscription == null) {
        if (autoCreate) {
            this.createSubscription(subscriptionName, topicName, deadLetterPolicy, autoCreate);
        } else {
            throw new ProvisioningException("Non-existing '" + subscriptionName + "' subscription.");
        }
    } else if (!subscription.getTopic().equals(topic.getName())) {
        throw new ProvisioningException("Existing '" + subscriptionName + "' subscription is for a different topic '" + subscription.getTopic() + "'.");
    }
    return new PubSubConsumerDestination(subscriptionName);
}
Also used : PubSubConsumerProperties(com.google.cloud.spring.stream.binder.pubsub.properties.PubSubConsumerProperties) ProvisioningException(org.springframework.cloud.stream.provisioning.ProvisioningException) Subscription(com.google.pubsub.v1.Subscription) Topic(com.google.pubsub.v1.Topic)

Example 4 with DeadLetterPolicy

use of com.google.pubsub.v1.DeadLetterPolicy in project native-image-support-java by GoogleCloudPlatform.

the class PubSubSampleApplication method updateSubscriptionDeadLetterTopic.

private static void updateSubscriptionDeadLetterTopic(String projectId, String subscriptionId, String topicId, String deadLetterTopicId) throws IOException {
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
        TopicName topicName = TopicName.of(projectId, topicId);
        TopicName deadLetterTopicName = TopicName.of(projectId, deadLetterTopicId);
        DeadLetterPolicy deadLetterPolicy = DeadLetterPolicy.newBuilder().setDeadLetterTopic(deadLetterTopicName.toString()).setMaxDeliveryAttempts(20).build();
        Subscription subscription = Subscription.newBuilder().setName(subscriptionName.toString()).setTopic(topicName.toString()).setDeadLetterPolicy(deadLetterPolicy).build();
        FieldMask updateMask = FieldMask.newBuilder().addPaths("dead_letter_policy").build();
        UpdateSubscriptionRequest request = UpdateSubscriptionRequest.newBuilder().setSubscription(subscription).setUpdateMask(updateMask).build();
        Subscription response = subscriptionAdminClient.updateSubscription(request);
        System.out.println("Updated subscription " + response.getName());
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) DeadLetterPolicy(com.google.pubsub.v1.DeadLetterPolicy) UpdateSubscriptionRequest(com.google.pubsub.v1.UpdateSubscriptionRequest) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) Subscription(com.google.pubsub.v1.Subscription) FieldMask(com.google.protobuf.FieldMask) TopicName(com.google.pubsub.v1.TopicName) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName)

Example 5 with DeadLetterPolicy

use of com.google.pubsub.v1.DeadLetterPolicy in project java-pubsub by googleapis.

the class NativeImagePubSubSample method updateSubscriptionDeadLetterTopic.

static void updateSubscriptionDeadLetterTopic(String projectId, String subscriptionId, String topicId, String deadLetterTopicId) throws IOException {
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
        TopicName topicName = TopicName.of(projectId, topicId);
        TopicName deadLetterTopicName = TopicName.of(projectId, deadLetterTopicId);
        DeadLetterPolicy deadLetterPolicy = DeadLetterPolicy.newBuilder().setDeadLetterTopic(deadLetterTopicName.toString()).setMaxDeliveryAttempts(20).build();
        Subscription subscription = Subscription.newBuilder().setName(subscriptionName.toString()).setTopic(topicName.toString()).setDeadLetterPolicy(deadLetterPolicy).build();
        FieldMask updateMask = FieldMask.newBuilder().addPaths("dead_letter_policy").build();
        UpdateSubscriptionRequest request = UpdateSubscriptionRequest.newBuilder().setSubscription(subscription).setUpdateMask(updateMask).build();
        Subscription response = subscriptionAdminClient.updateSubscription(request);
        System.out.println("Updated subscription " + response.getName());
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) DeadLetterPolicy(com.google.pubsub.v1.DeadLetterPolicy) UpdateSubscriptionRequest(com.google.pubsub.v1.UpdateSubscriptionRequest) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) Subscription(com.google.pubsub.v1.Subscription) FieldMask(com.google.protobuf.FieldMask) TopicName(com.google.pubsub.v1.TopicName) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName)

Aggregations

Subscription (com.google.pubsub.v1.Subscription)7 DeadLetterPolicy (com.google.pubsub.v1.DeadLetterPolicy)6 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)4 FieldMask (com.google.protobuf.FieldMask)3 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)3 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)3 TopicName (com.google.pubsub.v1.TopicName)3 UpdateSubscriptionRequest (com.google.pubsub.v1.UpdateSubscriptionRequest)3 PubSubConsumerProperties (com.google.cloud.spring.stream.binder.pubsub.properties.PubSubConsumerProperties)2 Topic (com.google.pubsub.v1.Topic)2 SubscriptionName (com.google.pubsub.v1.SubscriptionName)1 Test (org.junit.Test)1 ProvisioningException (org.springframework.cloud.stream.provisioning.ProvisioningException)1