ruby/example_code/sqs/sqs-ruby-example-get-messages.rb
def receive_messages(sqs_client, queue_url, max_number_of_messages = 10)

  if max_number_of_messages > 10
    puts 'Maximum number of messages to receive must be 10 or less. ' \
      'Stopping program.'
    return
  end

  response = sqs_client.receive_message(
    queue_url: queue_url,
    max_number_of_messages: max_number_of_messages
  )

  if response.messages.count.zero?
    puts 'No messages to receive, or all messages have already ' \
      'been previously received.'
    return
  end

  response.messages.each do |message|
    puts '-' * 20
    puts "Message body: #{message.body}"
    puts "Message ID:   #{message.message_id}"
  end

rescue StandardError => e
  puts "Error receiving messages: #{e.message}"
end
# Receives messages in a queue in Amazon Simple Queue Service (Amazon SQS).
#
# @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client.
# @param queue_url [String] The URL of the queue.
# @param max_number_of_messages [Integer] The maximum number of messages
#   to receive. This number must be 10 or less. The default is 10.
# @example
#   receive_messages(
#     Aws::SQS::Client.new(region: 'us-west-2'),
#     'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue',
#     10
#   )

Receives messages in a queue in Amazon Simple Queue Service (Amazon SQS).
Receives messages in a queue in Amazon Simple Queue Service ( Amazon SQS ) .
def receive_messages(sqs_client, queue_url, max_number_of_messages = 10)
  if max_number_of_messages > 10
    puts 'Maximum number of messages to receive must be 10 or less. ' \
      'Stopping program.'
    return
  end
  response = sqs_client.receive_message(
    queue_url: queue_url,
    max_number_of_messages: max_number_of_messages
  )
  if response.messages.count.zero?
    puts 'No messages to receive, or all messages have already ' \
      'been previously received.'
    return
  end
  response.messages.each do |message|
    puts '-' * 20
    puts "Message body: #{message.body}"
    puts "Message ID:   #{message.message_id}"
  end
rescue StandardError => e
  puts "Error receiving messages: #{e.message}"
end
def receive_messages ( sqs_client , queue_url , max_number_of_messages = 10 ) if max_number_of_messages > 10 puts 'Maximum number of messages to receive must be 10 or less. ' 'Stopping program.' return end response = sqs_client . receive_message ( queue_url : queue_url , max_number_of_messages : max_number_of_messages ) if response . messages . count . zero? puts 'No messages to receive, or all messages have already ' 'been previously received.' return end response . messages . each do | message | puts '-' * 20 puts "Message body: #{message.body}" puts "Message ID:   #{message.message_id}" end rescue StandardError => e puts "Error receiving messages: #{e.message}" end
Receives messages in a queue in Amazon Simple Queue Service (Amazon SQS).
Receives messages in a queue in Amazon Simple Queue Service ( Amazon SQS ) .
sqs_client
An initialized Amazon SQS client.
An initialized Amazon SQS client .
queue_url
The URL of the queue.
The URL of the queue .
max_number_of_messages
The maximum number of messages to receive. This number must be 10 or less. The default is 10.
The maximum number of messages to receive . This number must be 10 or less . The default is 10 .
example
None