Skip to content

Commit

Permalink
fixes for long polling
Browse files Browse the repository at this point in the history
  • Loading branch information
ford-at-aws committed Aug 21, 2023
1 parent 7853de4 commit a2e27c9
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions ruby/example_code/sqs/scenario_polling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
})

Expand All @@ -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}"
Expand Down

0 comments on commit a2e27c9

Please sign in to comment.