Class: Google::Cloud::Pubsub::ReceivedMessage

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

Overview

ReceivedMessage

Represents a Pub/Sub Message that can be acknowledged or delayed.

Examples:

require "google/cloud/pubsub"

pubsub = Google::Cloud::Pubsub.new

sub = pubsub.subscription "my-topic-sub"
subscriber = sub.listen do |received_message|
  puts received_message.message.data
  received_message.acknowledge!
end

# Start background threads that will call the block passed to listen.
subscriber.start

# Shut down the subscriber when ready to stop receiving messages.
subscriber.stop.wait!

Instance Method Summary collapse

Instance Method Details

#ack_idObject

The acknowledgment ID for the message.



62
63
64
# File 'lib/google/cloud/pubsub/received_message.rb', line 62

def ack_id
  @grpc.ack_id
end

#acknowledge!Object Also known as: ack!

Acknowledges receipt of the message.

Examples:

require "google/cloud/pubsub"

pubsub = Google::Cloud::Pubsub.new

sub = pubsub.subscription "my-topic-sub"
subscriber = sub.listen do |received_message|
  puts received_message.message.data

  received_message.acknowledge!
end

# Start background threads that will call block passed to listen.
subscriber.start

# Shut down the subscriber when ready to stop receiving messages.
subscriber.stop.wait!


122
123
124
125
# File 'lib/google/cloud/pubsub/received_message.rb', line 122

def acknowledge!
  ensure_subscription!
  subscription.acknowledge ack_id
end

#attributesObject

Optional attributes for the received message.



82
83
84
# File 'lib/google/cloud/pubsub/received_message.rb', line 82

def attributes
  message.attributes
end

#dataObject

The received message payload. This data is a list of bytes encoded as ASCII-8BIT.



76
77
78
# File 'lib/google/cloud/pubsub/received_message.rb', line 76

def data
  message.data
end

#delay!(new_deadline) ⇒ Object Also known as: modify_ack_deadline!

Modifies the acknowledge deadline for the message.

This indicates that more time is needed to process the message, or to make the message available for redelivery.

Examples:

require "google/cloud/pubsub"

pubsub = Google::Cloud::Pubsub.new

sub = pubsub.subscription "my-topic-sub"
subscriber = sub.listen do |received_message|
  puts received_message.message.data

  # Delay for 2 minutes
  received_message.delay! 120
end

# Start background threads that will call block passed to listen.
subscriber.start

# Shut down the subscriber when ready to stop receiving messages.
subscriber.stop.wait!

Parameters:

  • new_deadline (Integer)

    The new ack deadline in seconds from the time this request is sent to the Pub/Sub system. Must be >= 0. For example, if the value is 10, the new ack deadline will expire 10 seconds after the call is made. Specifying 0 may immediately make the message available for another pull request.



159
160
161
162
# File 'lib/google/cloud/pubsub/received_message.rb', line 159

def delay! new_deadline
  ensure_subscription!
  subscription.delay new_deadline, ack_id
end

#messageObject Also known as: msg

The received message.



68
69
70
# File 'lib/google/cloud/pubsub/received_message.rb', line 68

def message
  Message.from_grpc @grpc.message
end

#message_idObject Also known as: msg_id

The ID of the received message, assigned by the server at publication time. Guaranteed to be unique within the topic.



89
90
91
# File 'lib/google/cloud/pubsub/received_message.rb', line 89

def message_id
  message.message_id
end

#published_atObject Also known as: publish_time

The time at which the message was published.



96
97
98
# File 'lib/google/cloud/pubsub/received_message.rb', line 96

def published_at
  message.published_at
end

#reject!Object Also known as: nack!, ignore!

Resets the acknowledge deadline for the message without acknowledging it.

This will make the message available for redelivery.

Examples:

require "google/cloud/pubsub"

pubsub = Google::Cloud::Pubsub.new

sub = pubsub.subscription "my-topic-sub"
subscriber = sub.listen do |received_message|
  puts received_message.message.data

  # Release message back to the API.
  received_message.reject!
end

# Start background threads that will call block passed to listen.
subscriber.start

# Shut down the subscriber when ready to stop receiving messages.
subscriber.stop.wait!


190
191
192
# File 'lib/google/cloud/pubsub/received_message.rb', line 190

def reject!
  delay! 0
end