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.
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # This file is licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. A copy of the # License is located at # # http://aws.amazon.com/apache2.0/ # # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. 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.
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # This file is licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. A copy of the # License is located at # # http://aws.amazon.com/apache2.0/ # # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. 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
.
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # This file is licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. A copy of the # License is located at # # http://aws.amazon.com/apache2.0/ # # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. 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
.
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # This file is licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. A copy of the # License is located at # # http://aws.amazon.com/apache2.0/ # # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. require 'aws-sdk-sqs' # v2: require 'aws-sdk' # Process the message def do_something(_) 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