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);
}
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);
}
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);
}
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());
}
}
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());
}
}
Aggregations