class AWS::SQS::ReceivedMessage

Represents a message received from an Amazon SQS Queue.

Constants

ATTRIBUTE_ALIASES

@private

Attributes

attributes[R]

@private

body[R]

@return [String] The message’s contents.

handle[R]

@return [String] A string associated with this specific

instance of receiving the message.
id[R]

@return [String] The ID of the message.

md5[R]

@return [String] An MD5 digest of the message body.

queue[R]

@return [Queue] The queue from which this message was

received.

Public Class Methods

new(queue, id, handle, opts = {}) click to toggle source

@private

# File lib/aws/sqs/received_message.rb, line 50
def initialize(queue, id, handle, opts = {})
  @queue = queue
  @id = id
  @handle = handle
  @body = opts[:body]
  @md5 = opts[:md5]
  @attributes = opts[:attributes] || {}
  super
end

Public Instance Methods

approximate_first_receive_timestamp() click to toggle source

@return [Time] The time when the message was first received.

# File lib/aws/sqs/received_message.rb, line 171
def approximate_first_receive_timestamp
  @received_at ||=
    (timestamp = attributes["ApproximateFirstReceiveTimestamp"] and
     Time.at(timestamp.to_i / 1000.0)) || nil
end
Also aliased as: first_received_at
approximate_receive_count() click to toggle source

@return [Integer] The number of times a message has been

received but not deleted.
# File lib/aws/sqs/received_message.rb, line 164
def approximate_receive_count
  (count = attributes["ApproximateReceiveCount"] and
   count.to_i) or nil
end
Also aliased as: receive_count
as_sns_message() click to toggle source

When SNS publishes messages to SQS queues the message body is formatted as a json message and then base 64 encoded. An easy way to work with SNS messages is to call this method:

sns_msg = message.as_sns_message

sns_msg.topic
#=> <AWS::SNS::Topic ...>

sns_msg.to_h.inspect
#=> { :body => '...', :topic_arn => ... }

@return [ReceivedSNSMessage]

# File lib/aws/sqs/received_message.rb, line 73
def as_sns_message
  ReceivedSNSMessage.new(body, :config => config)
end
delete() click to toggle source

Deletes the message from the queue. Even if the message is locked by another reader due to the visibility timeout setting, it is still deleted from the queue. If you leave a message in the queue for more than 4 days, SQS automatically deletes it.

If you use {AWS::SQS::Queue#poll} or {AWS::SQS::Queue#receive_message} in block form, the messages you receive will be deleted automatically unless an exception occurs while you are processing them. You can still use this method if you want to delete a message early and then continue processing it.

@note It is possible you will receive a message even after

you have deleted it. This might happen on rare occasions
if one of the servers storing a copy of the message is
unavailable when you request to delete the message. The
copy remains on the server and might be returned to you
again on a subsequent receive request. You should create
your system to be idempotent so that receiving a
particular message more than once is not a problem.

@return [nil]

# File lib/aws/sqs/received_message.rb, line 99
def delete
  client.delete_message(
    :queue_url => queue.url, 
    :receipt_handle => handle)
  nil
end
first_received_at() click to toggle source
receive_count() click to toggle source
sender_id() click to toggle source

@return [String] The AWS account number (or the IP address,

if anonymous access is allowed) of the sender.
# File lib/aws/sqs/received_message.rb, line 148
def sender_id
  attributes["SenderId"]
end
sent_at() click to toggle source
Alias for: sent_timestamp
sent_timestamp() click to toggle source

@return [Time] The time when the message was sent.

# File lib/aws/sqs/received_message.rb, line 153
def sent_timestamp
  @sent_at ||=
    (timestamp = attributes["SentTimestamp"] and
     Time.at(timestamp.to_i / 1000.0)) || nil
rescue RangeError => e
  p [timestamp, timestamp.to_i]
end
Also aliased as: sent_at
visibility_timeout=(timeout) click to toggle source

Changes the visibility timeout of a specified message in a queue to a new value. The maximum allowed timeout value you can set the value to is 12 hours. This means you can’t extend the timeout of a message in an existing queue to more than a total visibility timeout of 12 hours. (For more information visibility timeout, see Visibility Timeout in the Amazon SQS Developer Guide.)

For example, let’s say the timeout for the queue is 30 seconds, and you receive a message. Once you’re 20 seconds into the timeout for that message (i.e., you have 10 seconds left), you extend it by 60 seconds by calling this method with timeout set to 60 seconds. You have then changed the remaining visibility timeout from 10 seconds to 60 seconds.

@note If you attempt to set the timeout to an amount more

than the maximum time left, Amazon SQS returns an
error. It will not automatically recalculate and increase
the timeout to the maximum time remaining.

@note Unlike with a queue, when you change the visibility

timeout for a specific message, that timeout value is
applied immediately but is not saved in memory for that
message. If you don't delete a message after it is
received, the visibility timeout for the message the next
time it is received reverts to the original timeout value,
not the value you set with this method.

@return Returns the timeout argument as passed.

# File lib/aws/sqs/received_message.rb, line 137
def visibility_timeout=(timeout)
  client.change_message_visibility(
    :queue_url => queue.url,
    :receipt_handle => handle,
    :visibility_timeout => timeout
  )
  timeout
end