|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package pubsub; |
| 18 | + |
| 19 | +// [START pubsub_dead_letter_create_subscription] |
| 20 | + |
| 21 | +import com.google.cloud.pubsub.v1.SubscriptionAdminClient; |
| 22 | +import com.google.pubsub.v1.DeadLetterPolicy; |
| 23 | +import com.google.pubsub.v1.ProjectSubscriptionName; |
| 24 | +import com.google.pubsub.v1.ProjectTopicName; |
| 25 | +import com.google.pubsub.v1.Subscription; |
| 26 | + |
| 27 | +public class CreateSubscriptionWithDeadLetterPolicyExample { |
| 28 | + |
| 29 | + public static void main(String... args) throws Exception { |
| 30 | + // TODO(developer): Replace these variables before running the sample. |
| 31 | + String projectId = "Your Project ID"; |
| 32 | + // This is the subscription you want to create with a dead letter policy. |
| 33 | + String subscriptionId = "Your Subscription ID"; |
| 34 | + // This is an existing topic that you want to attach the subscription with dead letter policy |
| 35 | + // to. |
| 36 | + String topicId = "Your Topic ID"; |
| 37 | + // This is an existing topic that the subscription with dead letter policy forwards dead letter |
| 38 | + // messages to. |
| 39 | + String deadLetterTopicId = "Your Dead Letter Topic ID"; |
| 40 | + |
| 41 | + CreateSubscriptionWithDeadLetterPolicyExample.createSubscriptionWithDeadLetterPolicyExample( |
| 42 | + projectId, subscriptionId, topicId, deadLetterTopicId); |
| 43 | + } |
| 44 | + |
| 45 | + public static void createSubscriptionWithDeadLetterPolicyExample( |
| 46 | + String projectId, String subscriptionId, String topicId, String deadLetterTopicId) |
| 47 | + throws Exception { |
| 48 | + try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { |
| 49 | + |
| 50 | + ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); |
| 51 | + ProjectSubscriptionName subscriptionName = |
| 52 | + ProjectSubscriptionName.of(projectId, subscriptionId); |
| 53 | + ProjectTopicName deadLetterTopicName = ProjectTopicName.of(projectId, deadLetterTopicId); |
| 54 | + |
| 55 | + DeadLetterPolicy deadLetterPolicy = |
| 56 | + DeadLetterPolicy.newBuilder() |
| 57 | + .setDeadLetterTopic(deadLetterTopicName.toString()) |
| 58 | + // The maximum number of times that the service attempts to deliver a |
| 59 | + // message before forwarding it to the dead letter topic. Must be [5-100]. |
| 60 | + .setMaxDeliveryAttempts(10) |
| 61 | + .build(); |
| 62 | + |
| 63 | + Subscription request = |
| 64 | + Subscription.newBuilder() |
| 65 | + .setName(subscriptionName.toString()) |
| 66 | + .setTopic(topicName.toString()) |
| 67 | + .setDeadLetterPolicy(deadLetterPolicy) |
| 68 | + .build(); |
| 69 | + |
| 70 | + Subscription subscription = subscriptionAdminClient.createSubscription(request); |
| 71 | + |
| 72 | + System.out.println("Created subscription: " + subscription.getName()); |
| 73 | + System.out.println( |
| 74 | + "It will forward dead letter messages to: " |
| 75 | + + subscription.getDeadLetterPolicy().getDeadLetterTopic()); |
| 76 | + System.out.println( |
| 77 | + "After " |
| 78 | + + subscription.getDeadLetterPolicy().getMaxDeliveryAttempts() |
| 79 | + + " delivery attempts."); |
| 80 | + // Remember to attach a subscription to the dead letter topic because |
| 81 | + // messages published to a topic with no subscriptions are lost. |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +// [END pubsub_dead_letter_create_subscription] |
0 commit comments