v1 Publish with ordering keys (DEPRECATED)

(DEPRECATED) Publish 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" 
  
 "sync" 
  
 "sync/atomic" 
  
 "cloud.google.com/go/pubsub" 
  
 "google.golang.org/api/option" 
 ) 
 func 
  
 publishWithOrderingKey 
 ( 
 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 
 () 
  
 var 
  
 wg 
  
 sync 
 . 
 WaitGroup 
  
 var 
  
 totalErrors 
  
 uint64 
  
 t 
  
 := 
  
 client 
 . 
 Topic 
 ( 
 topicID 
 ) 
  
 t 
 . 
 EnableMessageOrdering 
  
 = 
  
 true 
  
 messages 
  
 := 
  
 [] 
 struct 
  
 { 
  
 message 
  
 string 
  
 orderingKey 
  
 string 
  
 }{ 
  
 { 
  
 message 
 : 
  
 "message1" 
 , 
  
 orderingKey 
 : 
  
 "key1" 
 , 
  
 }, 
  
 { 
  
 message 
 : 
  
 "message2" 
 , 
  
 orderingKey 
 : 
  
 "key2" 
 , 
  
 }, 
  
 { 
  
 message 
 : 
  
 "message3" 
 , 
  
 orderingKey 
 : 
  
 "key1" 
 , 
  
 }, 
  
 { 
  
 message 
 : 
  
 "message4" 
 , 
  
 orderingKey 
 : 
  
 "key2" 
 , 
  
 }, 
  
 } 
  
 for 
  
 _ 
 , 
  
 m 
  
 := 
  
 range 
  
 messages 
  
 { 
  
 res 
  
 := 
  
 t 
 . 
 Publish 
 ( 
 ctx 
 , 
  
& pubsub 
 . 
 Message 
 { 
  
 Data 
 : 
  
 [] 
 byte 
 ( 
 m 
 . 
 message 
 ), 
  
 OrderingKey 
 : 
  
 m 
 . 
 orderingKey 
 , 
  
 }) 
  
 wg 
 . 
 Add 
 ( 
 1 
 ) 
  
 go 
  
 func 
 ( 
 res 
  
 * 
 pubsub 
 . 
  PublishResult 
 
 ) 
  
 { 
  
 defer 
  
 wg 
 . 
 Done 
 () 
  
 // The Get method blocks until a server-generated ID or 
  
 // an error is returned for the published message. 
  
 _ 
 , 
  
 err 
  
 := 
  
 res 
 . 
 Get 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 // Error handling code can be added here. 
  
 fmt 
 . 
 Printf 
 ( 
 "Failed to publish: %s\n" 
 , 
  
 err 
 ) 
  
 atomic 
 . 
 AddUint64 
 ( 
& totalErrors 
 , 
  
 1 
 ) 
  
 return 
  
 } 
  
 }( 
 res 
 ) 
  
 } 
  
 wg 
 . 
 Wait 
 () 
  
 if 
  
 totalErrors 
 > 
 0 
  
 { 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "%d of 4 messages did not publish successfully" 
 , 
  
 totalErrors 
 ) 
  
 return 
  
 } 
  
 fmt 
 . 
 Fprint 
 ( 
 w 
 , 
  
 "Published 4 messages with ordering keys 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 
  
 endpoint 
 : 
  
 "us-east1-pubsub.googleapis.com:443" 
 # 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" 
 end 
 # Stop the async_publisher to send all queued messages immediately. 
 topic 
 . 
 async_publisher 
 . 
 stop! 
 puts 
  
 "Messages published with ordering key." 
 

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: