v1 Create subscription with dead letter policy (DEPRECATED)

(DEPRECATED) Create subscription with dead letter policy

Code sample

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" 
  
 "time" 
  
 "cloud.google.com/go/pubsub" 
 ) 
 // createSubWithDeadLetter creates a subscription with a dead letter policy. 
 func 
  
 createSubWithDeadLetter 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 subID 
  
 string 
 , 
  
 topicID 
  
 string 
 , 
  
 fullyQualifiedDeadLetterTopic 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // subID := "my-sub" 
  
 // topicID := "my-topic" 
  
 // fullyQualifiedDeadLetterTopic := "projects/my-project/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 
 () 
  
 topic 
  
 := 
  
 client 
 . 
 Topic 
 ( 
 topicID 
 ) 
  
 subConfig 
  
 := 
  
 pubsub 
 . 
 SubscriptionConfig 
 { 
  
 Topic 
 : 
  
 topic 
 , 
  
 AckDeadline 
 : 
  
 20 
  
 * 
  
 time 
 . 
 Second 
 , 
  
 DeadLetterPolicy 
 : 
  
& pubsub 
 . 
 DeadLetterPolicy 
 { 
  
 DeadLetterTopic 
 : 
  
 fullyQualifiedDeadLetterTopic 
 , 
  
 MaxDeliveryAttempts 
 : 
  
 10 
 , 
  
 }, 
  
 } 
  
 sub 
 , 
  
 err 
  
 := 
  
 client 
 . 
 CreateSubscription 
 ( 
 ctx 
 , 
  
 subID 
 , 
  
 subConfig 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "CreateSubscription: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Created subscription (%s) with dead letter topic (%s)\n" 
 , 
  
 sub 
 . 
 String 
 (), 
  
 fullyQualifiedDeadLetterTopic 
 ) 
  
 fmt 
 . 
 Fprintln 
 ( 
 w 
 , 
  
 "To process dead letter messages, remember to add a subscription to your dead letter topic." 
 ) 
  
 return 
  
 nil 
 } 
 

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 
 topic 
  
 = 
  
 pubsub 
 . 
 topic 
  
 topic_id 
 dead_letter_topic 
  
 = 
  
 pubsub 
 . 
 topic 
  
 dead_letter_topic_id 
 subscription 
  
 = 
  
 topic 
 . 
 subscribe 
  
 subscription_id 
 , 
  
 dead_letter_topic 
 : 
  
 dead_letter_topic 
 , 
  
 dead_letter_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: