class Google::Cloud::PubSub::BatchPublisher
Topic
Batch Publisher object used to publish multiple messages at once.
@example
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
Attributes
messages[R]
@private The messages to publish
Public Class Methods
new(data, attributes, ordering_key, extra_attrs)
click to toggle source
@private Create a new instance of the object.
# File lib/google/cloud/pubsub/batch_publisher.rb, line 44 def initialize data, attributes, ordering_key, extra_attrs @messages = [] @mode = :batch return if data.nil? @mode = :single publish data, attributes, ordering_key: ordering_key, **extra_attrs end
Public Instance Methods
publish(data, attributes = nil, ordering_key: nil, **extra_attrs)
click to toggle source
Add a message to the batch to be published to the topic. All messages added to the batch will be published at once. See {Google::Cloud::PubSub::Topic#publish}
@param [String, File] data The message payload. This will be converted
to bytes encoded as ASCII-8BIT.
@param [Hash] attributes Optional attributes for the message. @param [String] ordering_key Identifies related messages for which
publish order should be respected.
@example 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
# File lib/google/cloud/pubsub/batch_publisher.rb, line 75 def publish data, attributes = nil, ordering_key: nil, **extra_attrs msg = Convert.pubsub_message data, attributes, ordering_key, extra_attrs @messages << msg end
to_gcloud_messages(message_ids)
click to toggle source
@private Create Message
objects with message ids.
# File lib/google/cloud/pubsub/batch_publisher.rb, line 82 def to_gcloud_messages message_ids msgs = @messages.zip(Array(message_ids)).map do |msg, id| msg.message_id = id Message.from_grpc msg end # Return just one Message if a single publish, # otherwise return the array of Messages. if @mode == :single && msgs.count <= 1 msgs.first else msgs end end