Create subscription with `DeadLetterPolicy`

Creates a subscription that forwards undeliverable messages to a dead-letter topic.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C++

Before trying this sample, follow the C++ setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub C++ API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  namespace 
  
 pubsub 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 pubsub 
 ; 
 namespace 
  
 pubsub_admin 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 pubsub_admin 
 ; 
 []( 
 pubsub_admin 
 :: 
 SubscriptionAdminClient 
  
 client 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 project_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 topic_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 subscription_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 dead_letter_topic_id 
 , 
  
 int 
  
 dead_letter_delivery_attempts 
 ) 
  
 { 
  
 google 
 :: 
 pubsub 
 :: 
 v1 
 :: 
 Subscription 
  
 request 
 ; 
  
 request 
 . 
 set_name 
 ( 
  
 pubsub 
 :: 
 Subscription 
 ( 
 project_id 
 , 
  
 subscription_id 
 ). 
 FullName 
 ()); 
  
 request 
 . 
 set_topic 
 ( 
 pubsub 
 :: 
 Topic 
 ( 
 project_id 
 , 
  
 topic_id 
 ). 
 FullName 
 ()); 
  
 request 
 . 
 mutable_dead_letter_policy 
 () 
 - 
> set_dead_letter_topic 
 ( 
  
 pubsub 
 :: 
 Topic 
 ( 
 project_id 
 , 
  
 dead_letter_topic_id 
 ). 
 FullName 
 ()); 
  
 request 
 . 
 mutable_dead_letter_policy 
 () 
 - 
> set_max_delivery_attempts 
 ( 
  
 dead_letter_delivery_attempts 
 ); 
  
 auto 
  
 sub 
  
 = 
  
 client 
 . 
 CreateSubscription 
 ( 
 request 
 ); 
  
 if 
  
 ( 
 sub 
 . 
 status 
 (). 
 code 
 () 
  
 == 
  
 google 
 :: 
 cloud 
 :: 
 StatusCode 
 :: 
 kAlreadyExists 
 ) 
  
 { 
  
 std 
 :: 
 cout 
 << 
 "The subscription already exists 
 \n 
 " 
 ; 
  
 return 
 ; 
  
 } 
  
 if 
  
 ( 
 ! 
 sub 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 sub 
 ). 
 status 
 (); 
  
 std 
 :: 
 cout 
 << 
 "The subscription was successfully created: " 
 << 
 sub 
 - 
> DebugString 
 () 
 << 
 " 
 \n 
 " 
 ; 
  
 std 
 :: 
 cout 
 << 
 "It will forward dead letter messages to: " 
 << 
 sub 
 - 
> dead_letter_policy 
 (). 
 dead_letter_topic 
 () 
 << 
 " 
 \n 
 " 
 ; 
  
 std 
 :: 
 cout 
 << 
 "After " 
 << 
 sub 
 - 
> dead_letter_policy 
 (). 
 max_delivery_attempts 
 () 
 << 
 " delivery attempts. 
 \n 
 " 
 ; 
 } 
 

C#

Before trying this sample, follow the C# setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub C# API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  using 
  
  Google.Cloud.PubSub.V1 
 
 ; 
 using 
  
 System 
 ; 
 public 
  
 class 
  
 CreateSubscriptionWithDeadLetterPolicySample 
 { 
  
 public 
  
 Subscription 
  
 CreateSubscriptionWithDeadLetterPolicy 
 ( 
 string 
  
 projectId 
 , 
  
 string 
  
 topicId 
 , 
  
 string 
  
 subscriptionId 
 , 
  
 string 
  
 deadLetterTopicId 
 ) 
  
 { 
  
  SubscriberServiceApiClient 
 
  
 subscriber 
  
 = 
  
  SubscriberServiceApiClient 
 
 . 
  Create 
 
 (); 
  
 // This is the subscription you want to create with a dead letter policy. 
  
 var 
  
 subscriptionName 
  
 = 
  
  SubscriptionName 
 
 . 
  FromProjectSubscription 
 
 ( 
 projectId 
 , 
  
 subscriptionId 
 ); 
  
 // This is an existing topic that you want to attach the subscription with dead letter policy to. 
  
 var 
  
 topicName 
  
 = 
  
  TopicName 
 
 . 
  FromProjectTopic 
 
 ( 
 projectId 
 , 
  
 topicId 
 ); 
  
 // This is an existing topic that the subscription with dead letter policy forwards dead letter messages to. 
  
 var 
  
 deadLetterTopic 
  
 = 
  
  TopicName 
 
 . 
  FromProjectTopic 
 
 ( 
 projectId 
 , 
  
 deadLetterTopicId 
 ). 
 ToString 
 (); 
  
 var 
  
 subscriptionRequest 
  
 = 
  
 new 
  
  Subscription 
 
  
 { 
  
 SubscriptionName 
  
 = 
  
 subscriptionName 
 , 
  
 TopicAsTopicName 
  
 = 
  
 topicName 
 , 
  
 DeadLetterPolicy 
  
 = 
  
 new 
  
  DeadLetterPolicy 
 
  
 { 
  
 DeadLetterTopic 
  
 = 
  
 deadLetterTopic 
 , 
  
 // The maximum number of times that the service attempts to deliver a 
  
 // message before forwarding it to the dead letter topic. Must be [5-100]. 
  
 MaxDeliveryAttempts 
  
 = 
  
 10 
  
 }, 
  
 AckDeadlineSeconds 
  
 = 
  
 30 
  
 }; 
  
 var 
  
 subscription 
  
 = 
  
 subscriber 
 . 
  CreateSubscription 
 
 ( 
 subscriptionRequest 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 "Created subscription: " 
  
 + 
  
 subscription 
 . 
  SubscriptionName 
 
 . 
 SubscriptionId 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"It will forward dead letter messages to: {subscription. DeadLetterPolicy 
. DeadLetterTopic 
}" 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"After {subscription. DeadLetterPolicy 
. MaxDeliveryAttempts 
} delivery attempts." 
 ); 
  
 // Remember to attach a subscription to the dead letter topic because 
  
 // messages published to a topic with no subscriptions are lost. 
  
 return 
  
 subscription 
 ; 
  
 } 
 } 
 

Go

Before trying this sample, follow the Go setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Go API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 "cloud.google.com/go/pubsub/v2" 
  
 "cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" 
 ) 
 // createSubWithDeadLetter creates a subscription with a dead letter policy. 
 func 
  
 createSubWithDeadLetter 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 topic 
 , 
  
 subscription 
 , 
  
 fullyQualifiedDeadLetterTopic 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // topic := "projects/my-project-id/topics/my-topic" 
  
 // subscription := "projects/my-project-id/subscriptions/my-sub" 
  
 // fullyQualifiedDeadLetterTopic := "projects/my-project-id/topics/my-dead-letter-topic" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 _ 
 , 
  
 err 
  
 = 
  
 client 
 . 
 SubscriptionAdminClient 
 . 
 CreateSubscription 
 ( 
 ctx 
 , 
  
& pubsubpb 
 . 
 Subscription 
 { 
  
 Name 
 : 
  
 subscription 
 , 
  
 Topic 
 : 
  
 topic 
 , 
  
 DeadLetterPolicy 
 : 
  
& pubsubpb 
 . 
 DeadLetterPolicy 
 { 
  
 DeadLetterTopic 
 : 
  
 fullyQualifiedDeadLetterTopic 
 , 
  
 MaxDeliveryAttempts 
 : 
  
 10 
 , 
  
 }, 
  
 }) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "CreateSubscription: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Created subscription with dead letter topic: (%s)\n" 
 , 
  
 fullyQualifiedDeadLetterTopic 
 ) 
  
 fmt 
 . 
 Fprintln 
 ( 
 w 
 , 
  
 "To process dead letter messages, remember to add a subscription to your dead letter topic." 
 ) 
  
 return 
  
 nil 
 } 
 

Java

Before trying this sample, follow the Java setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Java API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 com.google.cloud.pubsub.v1. SubscriptionAdminClient 
 
 ; 
 import 
  
 com.google.pubsub.v1. DeadLetterPolicy 
 
 ; 
 import 
  
 com.google.pubsub.v1. ProjectSubscriptionName 
 
 ; 
 import 
  
 com.google.pubsub.v1. ProjectTopicName 
 
 ; 
 import 
  
 com.google.pubsub.v1. Subscription 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 CreateSubscriptionWithDeadLetterPolicyExample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 ... 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 String 
  
 projectId 
  
 = 
  
 "your-project-id" 
 ; 
  
 // This is the subscription you want to create with a dead letter policy. 
  
 String 
  
 subscriptionId 
  
 = 
  
 "your-subscription-id" 
 ; 
  
 // This is an existing topic that you want to attach the subscription with dead letter policy 
  
 // to. 
  
 String 
  
 topicId 
  
 = 
  
 "your-topic-id" 
 ; 
  
 // This is an existing topic that the subscription with dead letter policy forwards dead letter 
  
 // messages to. 
  
 String 
  
 deadLetterTopicId 
  
 = 
  
 "your-dead-letter-topic-id" 
 ; 
  
 CreateSubscriptionWithDeadLetterPolicyExample 
 . 
 createSubscriptionWithDeadLetterPolicyExample 
 ( 
  
 projectId 
 , 
  
 subscriptionId 
 , 
  
 topicId 
 , 
  
 deadLetterTopicId 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 createSubscriptionWithDeadLetterPolicyExample 
 ( 
  
 String 
  
 projectId 
 , 
  
 String 
  
 subscriptionId 
 , 
  
 String 
  
 topicId 
 , 
  
 String 
  
 deadLetterTopicId 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 try 
  
 ( 
  SubscriptionAdminClient 
 
  
 subscriptionAdminClient 
  
 = 
  
  SubscriptionAdminClient 
 
 . 
 create 
 ()) 
  
 { 
  
  ProjectTopicName 
 
  
 topicName 
  
 = 
  
  ProjectTopicName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 topicId 
 ); 
  
  ProjectSubscriptionName 
 
  
 subscriptionName 
  
 = 
  
  ProjectSubscriptionName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 subscriptionId 
 ); 
  
  ProjectTopicName 
 
  
 deadLetterTopicName 
  
 = 
  
  ProjectTopicName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 deadLetterTopicId 
 ); 
  
  DeadLetterPolicy 
 
  
 deadLetterPolicy 
  
 = 
  
  DeadLetterPolicy 
 
 . 
 newBuilder 
 () 
  
 . 
  setDeadLetterTopic 
 
 ( 
 deadLetterTopicName 
 . 
  toString 
 
 ()) 
  
 // The maximum number of times that the service attempts to deliver a 
  
 // message before forwarding it to the dead letter topic. Must be [5-100]. 
  
 . 
  setMaxDeliveryAttempts 
 
 ( 
 10 
 ) 
  
 . 
 build 
 (); 
  
  Subscription 
 
  
 request 
  
 = 
  
  Subscription 
 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
 subscriptionName 
 . 
  toString 
 
 ()) 
  
 . 
 setTopic 
 ( 
 topicName 
 . 
  toString 
 
 ()) 
  
 . 
  setDeadLetterPolicy 
 
 ( 
 deadLetterPolicy 
 ) 
  
 . 
 build 
 (); 
  
  Subscription 
 
  
 subscription 
  
 = 
  
 subscriptionAdminClient 
 . 
 createSubscription 
 ( 
 request 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Created subscription: " 
  
 + 
  
 subscription 
 . 
  getName 
 
 ()); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
  
 "It will forward dead letter messages to: " 
  
 + 
  
 subscription 
 . 
  getDeadLetterPolicy 
 
 (). 
 getDeadLetterTopic 
 ()); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
  
 "After " 
  
 + 
  
 subscription 
 . 
  getDeadLetterPolicy 
 
 (). 
 getMaxDeliveryAttempts 
 () 
  
 + 
  
 " delivery attempts." 
 ); 
  
 // Remember to attach a subscription to the dead letter topic because 
  
 // messages published to a topic with no subscriptions are lost. 
  
 } 
  
 } 
 } 
 

PHP

Before trying this sample, follow the PHP setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub PHP API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  use Google\Cloud\PubSub\PubSubClient; 
 /** 
 * Creates a Pub/Sub subscription with dead letter policy enabled. 
 * 
 * @param string $projectId  The Google project ID. 
 * @param string $topicName  The Pub/Sub topic name. 
 * @param string $subscriptionName  The Pub/Sub subscription name. 
 * @param string $deadLetterTopicName The Pub/Sub topic to use for dead letter policy. 
 */ 
 function dead_letter_create_subscription($projectId, $topicName, $subscriptionName, $deadLetterTopicName) 
 { 
 $pubsub = new PubSubClient([ 
 'projectId' => $projectId, 
 ]); 
 $topic = $pubsub->topic($topicName); 
 $deadLetterTopic = $pubsub->topic($deadLetterTopicName); 
 $subscription = $topic->subscribe($subscriptionName, [ 
 'deadLetterPolicy' => [ 
 'deadLetterTopic' => $deadLetterTopic 
 ] 
 ]); 
 printf( 
 'Subscription %s created with dead letter topic %s' . PHP_EOL, 
 $subscription->name(), 
 $deadLetterTopic->name() 
 ); 
 } 
 

Ruby

Before trying this sample, follow the Ruby setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Ruby API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  # topic_id             = "your-topic-id" 
 # subscription_id      = "your-subscription-id" 
 # dead_letter_topic_id = "your-dead-letter-topic-id" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 . 
  new 
 
 subscription_admin 
  
 = 
  
 pubsub 
 . 
  subscription_admin 
 
 subscription 
  
 = 
  
 subscription_admin 
 . 
 create_subscription 
  
 \ 
  
 name 
 : 
  
 pubsub 
 . 
 subscription_path 
 ( 
 subscription_id 
 ), 
  
 topic 
 : 
  
 pubsub 
 . 
 topic_path 
 ( 
 topic_id 
 ), 
  
 dead_letter_policy 
 : 
  
 { 
  
 dead_letter_topic 
 : 
  
 pubsub 
 . 
 topic_path 
 ( 
 dead_letter_topic_id 
 ), 
  
 max_delivery_attempts 
 : 
  
 10 
  
 } 
 puts 
  
 "Created subscription 
 #{ 
 subscription_id 
 } 
 with dead letter topic " 
  
 \ 
  
 " 
 #{ 
 dead_letter_topic_id 
 } 
 ." 
 puts 
  
 "To process dead letter messages, remember to add a subscription to " 
  
 \ 
  
 "your dead letter topic." 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: