Class: Google::Cloud::PubSub::BatchPublisher

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/pubsub/batch_publisher.rb

Overview

Topic Batch Publisher object used to publish multiple messages at once.

Examples:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

topic = pubsub.topic "my-topic"
msgs = topic.publish do |batch_publisher|
  batch_publisher.publish "task 1 completed", foo: :bar
  batch_publisher.publish "task 2 completed", foo: :baz
  batch_publisher.publish "task 3 completed", foo: :bif
end

Instance Method Summary collapse

Instance Method Details

#publish(data, attributes = nil, ordering_key: nil, **extra_attrs) ⇒ Object

Add a message to the batch to be published to the topic. All messages added to the batch will be published at once. See Topic#publish

Examples:

Multiple messages can be sent at the same time using a block:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

topic = pubsub.topic "my-topic"
msgs = topic.publish do |batch_publisher|
  batch_publisher.publish "task 1 completed", foo: :bar
  batch_publisher.publish "task 2 completed", foo: :baz
  batch_publisher.publish "task 3 completed", foo: :bif
end

Parameters:

  • data (String, File)

    The message payload. This will be converted to bytes encoded as ASCII-8BIT.

  • attributes (Hash) (defaults to: nil)

    Optional attributes for the message.

  • ordering_key (String) (defaults to: nil)

    Identifies related messages for which publish order should be respected.



96
97
98
99
100
# File 'lib/google/cloud/pubsub/batch_publisher.rb', line 96

def publish data, attributes = nil, ordering_key: nil, **extra_attrs
  msg = Convert.pubsub_message data, attributes, ordering_key, extra_attrs
  @total_message_bytes += msg.data.bytesize + 2
  @messages << msg
end