AWS SDK for Ruby
Developer Guide

Receiving Messages Using the QueuePoller Class in Amazon SQS

The following example uses the QueuePoller utility class to display the body of all messages in the Amazon SQS queue with the URL URL in the us-west-2 region, and deletes the message. After approximately 15 seconds of inactivity, the script times out.

require 'aws-sdk-sqs' # v2: require 'aws-sdk' Aws.config.update({region: 'us-west-2'}) poller = Aws::SQS::QueuePoller.new(URL) poller.poll(idle_timeout: 15) do |msg| puts msg.body end

The following example loops through the Amazon SQS queue with the URL URL, and waits up to duration seconds.

You can get the correct URL by executing the Amazon SQS example in Getting Information about All Queues in Amazon SQS.

require 'aws-sdk-sqs' # v2: require 'aws-sdk' Aws.config.update({region: 'us-west-2'}) poller = Aws::SQS::QueuePoller.new(URL) poller.poll(wait_time_seconds: duration, idle_timeout: duration + 1) do |msg| puts msg.body end

The following example loops through the Amazon SQS queue with the URL URL, and gives you up to the visibility timeout seconds to process the message, represented by the method do_something.

require 'aws-sdk-sqs' # v2: require 'aws-sdk' # Process the message def do_something(msg) puts msg.body end Aws.config.update({region: 'us-west-2'}) poller = Aws::SQS::QueuePoller.new(URL) poller.poll(visibility_timeout: timeout, idle_timeout: timeout + 1) do |msg| do_something(msg) end

The following example loops through the Amazon SQS queue with the URL URL, and changes the visibility timeout seconds, for any message that needs additional processing by the method do_something2.

require 'aws-sdk-sqs' # v2: require 'aws-sdk' # Process the message def do_something(msg) true end # Do additional processing def do_something2(msg) puts msg.body end Aws.config.update({region: 'us-west-2'}) poller = Aws::SQS::QueuePoller.new(URL) poller.poll(idle_timeout: timeout + 1) do |msg| if do_something(msg) # need more time for processing poller.change_message_visibility_timeout(msg, timeout) do_something2(msg) end end