v1 Publish with batching settings (DEPRECATED)

(DEPRECATED) Publish with batching settings

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" 
  
 "strconv" 
  
 "time" 
  
 "cloud.google.com/go/pubsub" 
 ) 
 func 
  
 publishWithSettings 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 topicID 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // topicID := "my-topic" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
  NewClient 
 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 var 
  
 results 
  
 [] 
 * 
 pubsub 
 . 
  PublishResult 
 
  
 var 
  
 resultErrors 
  
 [] 
 error 
  
 t 
  
 := 
  
 client 
 . 
 Topic 
 ( 
 topicID 
 ) 
  
 t 
 . 
 PublishSettings 
 . 
 ByteThreshold 
  
 = 
  
 5000 
  
 t 
 . 
 PublishSettings 
 . 
 CountThreshold 
  
 = 
  
 10 
  
 t 
 . 
 PublishSettings 
 . 
 DelayThreshold 
  
 = 
  
 100 
  
 * 
  
 time 
 . 
 Millisecond 
  
 for 
  
 i 
  
 := 
  
 0 
 ; 
  
 i 
 < 
 10 
 ; 
  
 i 
 ++ 
  
 { 
  
 result 
  
 := 
  
 t 
 . 
 Publish 
 ( 
 ctx 
 , 
  
& pubsub 
 . 
 Message 
 { 
  
 Data 
 : 
  
 [] 
 byte 
 ( 
 "Message " 
  
 + 
  
 strconv 
 . 
 Itoa 
 ( 
 i 
 )), 
  
 }) 
  
 results 
  
 = 
  
 append 
 ( 
 results 
 , 
  
 result 
 ) 
  
 } 
  
 // The Get method blocks until a server-generated ID or 
  
 // an error is returned for the published message. 
  
 for 
  
 i 
 , 
  
 res 
  
 := 
  
 range 
  
 results 
  
 { 
  
 id 
 , 
  
 err 
  
 := 
  
 res 
 . 
 Get 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 resultErrors 
  
 = 
  
 append 
 ( 
 resultErrors 
 , 
  
 err 
 ) 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Failed to publish: %v" 
 , 
  
 err 
 ) 
  
 continue 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Published message %d; msg ID: %v\n" 
 , 
  
 i 
 , 
  
 id 
 ) 
  
 } 
  
 if 
  
 len 
 ( 
 resultErrors 
 ) 
  
 != 
  
 0 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "Get: %v" 
 , 
  
 resultErrors 
 [ 
 len 
 ( 
 resultErrors 
 ) 
 - 
 1 
 ]) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Published messages with batch settings." 
 ) 
  
 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" 
 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 
 } 
 10 
 . 
 times 
  
 do 
  
 | 
 i 
 | 
  
 topic 
 . 
 publish_async 
  
 "This is message # 
 #{ 
 i 
 } 
 ." 
 end 
 # Stop the async_publisher to send all queued messages immediately. 
 topic 
 . 
 async_publisher 
 . 
 stop 
 . 
 wait! 
 puts 
  
 "Messages published asynchronously in batch." 
 

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: