From a4abdffc3657f4b7f704bd7218e7d5c97de1fd76 Mon Sep 17 00:00:00 2001 From: ford prior <108086978+ford-at-aws@users.noreply.github.com> Date: Wed, 23 Aug 2023 09:28:59 -0400 Subject: [PATCH] Ruby - SQS - Fixes for long polling (#5292) fixes for long polling --- ruby/example_code/sqs/scenario_polling.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ruby/example_code/sqs/scenario_polling.rb b/ruby/example_code/sqs/scenario_polling.rb index d741b0054d7..29686a0d653 100644 --- a/ruby/example_code/sqs/scenario_polling.rb +++ b/ruby/example_code/sqs/scenario_polling.rb @@ -51,8 +51,7 @@ puts "Begin receipt of any messages using receive_message..." receive_message_result = sqs.receive_message({ queue_url: receive_queue_url, - attribute_names: ["All"], # Receive all available built-in message attributes. - message_attribute_names: ["All"], # Receive any custom message attributes. + wait_time_seconds: 10, # Poll messages for 10 seconds at a time. Default: 0. max_number_of_messages: 10 # Receive up to 10 messages, if there are that many. }) @@ -61,15 +60,18 @@ puts "Cannot receive messages using receive_message for a queue named '#{receive_queue_name}', as it does not exist." end -# 2. Using Aws::SQS::QueuePoller. +# 2. Using Aws::SQS::QueuePoller +# NOTE: Using this class, messages are received using a "long poll" of 20 seconds. +# If you prefer to use settings configured in the queue, then pass a nil value for :wait_time_seconds. begin puts "Begin receipt of any messages using Aws::SQS::QueuePoller..." puts "(Will keep polling until no more messages available for at least 60 seconds.)" poller = Aws::SQS::QueuePoller.new(receive_queue_url) poller_stats = poller.poll({ - max_number_of_messages: 10, - idle_timeout: 60 # Stop polling after 60 seconds of no more messages available (polls indefinitely by default). + wait_time_seconds: 30, # Poll messages for 30 seconds at a time. Default: 20. + max_number_of_messages: 10, # Return up to 10 messages at a time. + idle_timeout: 60 # Terminate polling loop after 60 seconds. }) do |messages| messages.each do |message| puts "Message body: #{message.body}"