在 Amazon SQS 中启用长轮询 - Amazon 适用于 Ruby 的 SDK
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在 Amazon SQS 中启用长轮询

长轮询有助于减少空响应的数量并消除假的空响应,从而有助于降低 Amazon SQS 的使用成本。有关长轮询的更多信息,请参阅 Amazon SQS 长轮询

在此示例中,您结合使用适用于 Ruby 的 Amazon SDK 和 Amazon SQS 以实现如下用途:

  1. 通过使用 Aws::SQS::Client#create_queue 来创建队列并将其设置为长轮询。

  2. 通过使用 Aws::SQS::Client#set_queue_attributes 来为现有队列设置长轮询。

  3. 通过使用 Aws::SQS::Client#receive_message 来设置接收队列的消息时的长轮询。

先决条件

在运行示例代码之前,您需要安装并配置适用于 Ruby 的 Amazon SDK,如以下文档所述:

您还需要创建队列 existing-queuereceive-queue,您可以在 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. # Demonstrates how to: # 1. Create a queue and set it for long polling. # 2. Set long polling for an existing queue. # 3. Set long polling when receiving messages for a queue. require 'aws-sdk-sqs' # v2: require 'aws-sdk' sqs = Aws::SQS::Client.new(region: 'us-east-1') # Create a queue and set it for long polling. new_queue_name = "new-queue" create_queue_result = sqs.create_queue({ queue_name: new_queue_name, attributes: { "ReceiveMessageWaitTimeSeconds" => "20" # Wait 20 seconds to receive messages. }, }) puts create_queue_result.queue_url # Set long polling for an existing queue. begin existing_queue_name = "existing-queue" existing_queue_url = sqs.get_queue_url(queue_name: existing_queue_name).queue_url sqs.set_queue_attributes({ queue_url: existing_queue_url, attributes: { "ReceiveMessageWaitTimeSeconds" => "20" # Wait 20 seconds to receive messages. }, }) rescue Aws::SQS::Errors::NonExistentQueue puts "Cannot set long polling for a queue named '#{existing_queue_name}', as it does not exist." end # Set long polling when receiving messages for a queue. # 1. Using receive_message. begin receive_queue_name = "receive-queue" receive_queue_url = sqs.get_queue_url(queue_name: receive_queue_name).queue_url 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. max_number_of_messages: 10 # Receive up to 10 messages, if there are that many. }) puts "Received #{receive_message_result.messages.count} message(s)." rescue Aws::SQS::Errors::NonExistentQueue 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. 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). }) do |messages| messages.each do |message| puts "Message body: #{message.body}" end end # Note: If poller.poll is successful, all received messages are automatically deleted from the queue. puts "Poller stats:" puts " Polling started at: #{poller_stats.polling_started_at}" puts " Polling stopped at: #{poller_stats.polling_stopped_at}" puts " Last message received at: #{poller_stats.last_message_received_at}" puts " Number of polling requests: #{poller_stats.request_count}" puts " Number of received messages: #{poller_stats.received_message_count}" rescue Aws::SQS::Errors::NonExistentQueue puts "Cannot receive messages using Aws::SQS::QueuePoller for a queue named '#{receive_queue_name}', as it does not exist." end