v1 Resume publishing with ordering keys (DEPRECATED)

(DEPRECATED) Resume publishing with ordering keys

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" 
  
 "cloud.google.com/go/pubsub" 
  
 "google.golang.org/api/option" 
 ) 
 func 
  
 resumePublishWithOrderingKey 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 topicID 
  
 string 
 ) 
  
 { 
  
 // projectID := "my-project-id" 
  
 // topicID := "my-topic" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 // Pub/Sub's ordered delivery guarantee only applies when publishes for an ordering key are in the same region 
  
 // For list of locational endpoints for Pub/Sub, see https://cloud.google.com/pubsub/docs/reference/service_apis_overview#list_of_locational_endpoints 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
  NewClient 
 
 ( 
 ctx 
 , 
  
 projectID 
 , 
  
 option 
 . 
 WithEndpoint 
 ( 
 "us-east1-pubsub.googleapis.com:443" 
 )) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "pubsub.NewClient: %v" 
 , 
  
 err 
 ) 
  
 return 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 t 
  
 := 
  
 client 
 . 
 Topic 
 ( 
 topicID 
 ) 
  
 t 
 . 
 EnableMessageOrdering 
  
 = 
  
 true 
  
 key 
  
 := 
  
 "some-ordering-key" 
  
 res 
  
 := 
  
 t 
 . 
 Publish 
 ( 
 ctx 
 , 
  
& pubsub 
 . 
 Message 
 { 
  
 Data 
 : 
  
 [] 
 byte 
 ( 
 "some-message" 
 ), 
  
 OrderingKey 
 : 
  
 key 
 , 
  
 }) 
  
 _ 
 , 
  
 err 
  
 = 
  
 res 
 . 
 Get 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 // Error handling code can be added here. 
  
 fmt 
 . 
 Printf 
 ( 
 "Failed to publish: %s\n" 
 , 
  
 err 
 ) 
  
 // Resume publish on an ordering key that has had unrecoverable errors. 
  
 // After such an error publishes with this ordering key will fail 
  
 // until this method is called. 
  
 t 
 . 
  ResumePublish 
 
 ( 
 key 
 ) 
  
 } 
  
 fmt 
 . 
 Fprint 
 ( 
 w 
 , 
  
 "Published a message with ordering key successfully\n" 
 ) 
 } 
 

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" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  Pubsub 
 
 . 
 new 
 # Start sending messages in one request once the size of all queued messages 
 # reaches 1 MB or the number of queued messages reaches 20 
 topic 
  
 = 
  
 pubsub 
 . 
 topic 
  
 topic_id 
 , 
  
 async 
 : 
  
 { 
  
 max_bytes 
 : 
  
 1_000_000 
 , 
  
 max_messages 
 : 
  
 20 
 } 
 topic 
 . 
 enable_message_ordering! 
 10 
 . 
 times 
  
 do 
  
 | 
 i 
 | 
  
 topic 
 . 
 publish_async 
  
 "This is message # 
 #{ 
 i 
 } 
 ." 
 , 
  
 ordering_key 
 : 
  
 "ordering-key" 
  
 do 
  
 | 
 result 
 | 
  
 if 
  
 result 
 . 
 succeeded? 
  
 puts 
  
 "Message # 
 #{ 
 i 
 } 
 successfully published." 
  
 else 
  
 puts 
  
 "Message # 
 #{ 
 i 
 } 
 failed to publish" 
  
 # Allow publishing to continue on "ordering-key" after processing the 
  
 # failure. 
  
 topic 
 . 
 resume_publish 
  
 "ordering-key" 
  
 end 
  
 end 
 end 
 # Stop the async_publisher to send all queued messages immediately. 
 topic 
 . 
 async_publisher 
 . 
 stop! 
 

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: