Update subscription

Updates a subscription.

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_admin 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 pubsub_admin 
 ; 
 namespace 
  
 pubsub 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 pubsub 
 ; 
 []( 
 pubsub_admin 
 :: 
 SubscriptionAdminClient 
  
 client 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 project_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 subscription_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 endpoint 
 ) 
  
 { 
  
 google 
 :: 
 pubsub 
 :: 
 v1 
 :: 
 ModifyPushConfigRequest 
  
 request 
 ; 
  
 request 
 . 
 set_subscription 
 ( 
  
 pubsub 
 :: 
 Subscription 
 ( 
 project_id 
 , 
  
 subscription_id 
 ). 
 FullName 
 ()); 
  
 request 
 . 
 mutable_push_config 
 () 
 - 
> set_push_endpoint 
 ( 
 endpoint 
 ); 
  
 auto 
  
 status 
  
 = 
  
 client 
 . 
 ModifyPushConfig 
 ( 
 request 
 ); 
  
 if 
  
 ( 
 ! 
 status 
 . 
 ok 
 ()) 
  
 throw 
  
 std 
 :: 
 runtime_error 
 ( 
 status 
 . 
 message 
 ()); 
  
 std 
 :: 
 cout 
 << 
 "The subscription push configuration was successfully" 
 << 
 " modified 
 \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 
 
 ; 
 public 
  
 class 
  
 UpdatePushConfigurationSample 
 { 
  
 public 
  
 void 
  
 UpdatePushConfiguration 
 ( 
 string 
  
 projectId 
 , 
  
 string 
  
 subscriptionId 
 , 
  
 string 
  
 pushEndpoint 
 ) 
  
 { 
  
  SubscriberServiceApiClient 
 
  
 subscriber 
  
 = 
  
  SubscriberServiceApiClient 
 
 . 
  Create 
 
 (); 
  
  SubscriptionName 
 
  
 subscriptionName 
  
 = 
  
  SubscriptionName 
 
 . 
  FromProjectSubscription 
 
 ( 
 projectId 
 , 
  
 subscriptionId 
 ); 
  
  PushConfig 
 
  
 pushConfig 
  
 = 
  
 new 
  
  PushConfig 
 
  
 { 
  
 PushEndpoint 
  
 = 
  
 pushEndpoint 
  
 }; 
  
 subscriber 
 . 
  ModifyPushConfig 
 
 ( 
 subscriptionName 
 , 
  
 pushConfig 
 ); 
  
 } 
 } 
 

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" 
  
 "google.golang.org/protobuf/types/known/fieldmaskpb" 
 ) 
 func 
  
 updateEndpoint 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 subscriptionName 
 , 
  
 endpoint 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // subscriptionName := "projects/my-project/subscriptions/my-sub" 
  
 // endpoint := "https://my-test-project.appspot.com/push" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 req 
  
 := 
  
& pubsubpb 
 . 
 UpdateSubscriptionRequest 
 { 
  
 Subscription 
 : 
  
& pubsubpb 
 . 
 Subscription 
 { 
  
 Name 
 : 
  
 subscriptionName 
 , 
  
 PushConfig 
 : 
  
& pubsubpb 
 . 
 PushConfig 
 { 
  
 PushEndpoint 
 : 
  
 endpoint 
 , 
  
 }, 
  
 }, 
  
 UpdateMask 
 : 
  
& fieldmaskpb 
 . 
 FieldMask 
 { 
  
 Paths 
 : 
  
 [] 
 string 
 { 
 "push_config" 
 }, 
  
 }, 
  
 } 
  
 subConfig 
 , 
  
 err 
  
 := 
  
 client 
 . 
 SubscriptionAdminClient 
 . 
 UpdateSubscription 
 ( 
 ctx 
 , 
  
 req 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "Update: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Updated subscription config: %v\n" 
 , 
  
 subConfig 
 ) 
  
 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. PushConfig 
 
 ; 
 import 
  
 com.google.pubsub.v1. Subscription 
 
 ; 
 import 
  
 com.google.pubsub.v1. SubscriptionName 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 UpdatePushConfigurationExample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 ... 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 String 
  
 projectId 
  
 = 
  
 "your-project-id" 
 ; 
  
 String 
  
 subscriptionId 
  
 = 
  
 "your-subscription-id" 
 ; 
  
 String 
  
 pushEndpoint 
  
 = 
  
 "https://my-test-project.appspot.com/push" 
 ; 
  
 updatePushConfigurationExample 
 ( 
 projectId 
 , 
  
 subscriptionId 
 , 
  
 pushEndpoint 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 updatePushConfigurationExample 
 ( 
  
 String 
  
 projectId 
 , 
  
 String 
  
 subscriptionId 
 , 
  
 String 
  
 pushEndpoint 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 try 
  
 ( 
  SubscriptionAdminClient 
 
  
 subscriptionAdminClient 
  
 = 
  
  SubscriptionAdminClient 
 
 . 
 create 
 ()) 
  
 { 
  
  SubscriptionName 
 
  
 subscriptionName 
  
 = 
  
  SubscriptionName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 subscriptionId 
 ); 
  
  PushConfig 
 
  
 pushConfig 
  
 = 
  
  PushConfig 
 
 . 
 newBuilder 
 (). 
  setPushEndpoint 
 
 ( 
 pushEndpoint 
 ). 
 build 
 (); 
  
 subscriptionAdminClient 
 . 
 modifyPushConfig 
 ( 
 subscriptionName 
 , 
  
 pushConfig 
 ); 
  
  Subscription 
 
  
 subscription 
  
 = 
  
 subscriptionAdminClient 
 . 
 getSubscription 
 ( 
 subscriptionName 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
  
 "Updated push endpoint to: " 
  
 + 
  
 subscription 
 . 
  getPushConfig 
 
 (). 
 getPushEndpoint 
 ()); 
  
 } 
  
 } 
 } 
 

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 .

  # subscription_id   = "your-subscription-id" 
 # new_endpoint      = "Endpoint where your app receives messages"" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 . 
  new 
 
 subscription_admin 
  
 = 
  
 pubsub 
 . 
  subscription_admin 
 
 subscription 
  
 = 
  
 subscription_admin 
 . 
 get_subscription 
  
 \ 
  
 subscription 
 : 
  
 pubsub 
 . 
 subscription_path 
 ( 
 subscription_id 
 ) 
 subscription 
 . 
 push_config 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 :: 
  V1 
 
 :: 
 PushConfig 
 . 
  new 
 
  
 \ 
  
 push_endpoint 
 : 
  
 new_endpoint 
 subscription_admin 
 . 
 update_subscription 
  
 subscription 
 : 
  
 subscription 
 , 
  
 update_mask 
 : 
  
 { 
  
 paths 
 : 
  
 [ 
 "push_config" 
 ] 
  
 } 
 puts 
  
 "Push endpoint updated." 
 

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: