Receiving Messages in Amazon SQS - Amazon SDK for Ruby
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Receiving Messages in Amazon SQS

The following example displays the body of up to 10 messages in the Amazon SQS queue with the URL URL in the us-west-2 region.

Note

receive_message does not guarantee to get all messages (see Properties of Distributed Queues), and by default does not delete the message.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 require 'aws-sdk-sqs' require 'aws-sdk-sts' # 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-east-1'), # 'https://sqs.us-east-1.amazonaws.com/111111111111/my-queue', # 10 # ) 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 # Full example call: def run_me region = 'us-east-1' queue_name = 'my-queue' max_number_of_messages = 10 sts_client = Aws::STS::Client.new(region: region) # For example: # 'https://sqs.us-east-1.amazonaws.com/111111111111/my-queue' queue_url = 'https://sqs.' + region + '.amazonaws.com/' + sts_client.get_caller_identity.account + '/' + queue_name sqs_client = Aws::SQS::Client.new(region: region) puts "Receiving messages from queue '#{queue_name}'..." receive_messages(sqs_client, queue_url, max_number_of_messages) end run_me if $PROGRAM_NAME == __FILE__