在 Amazon SQS 中啟用長輪詢 - AWS SDK for Ruby

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

在 Amazon SQS 中啟用長輪詢

長輪詢有助於降低使用 Amazon SQS 的成本,方法是減少空白回應的數量並消除錯誤的空白回應。如需長輪詢的詳細資訊,請參閱 Amazon SQS 長輪詢

在此範例中,您可以使用適用於 Ruby 的AWS開發套件搭配 Amazon SQS 來執行下列作業:

  1. 使用 Aws:: SQS:: 客戶端 #create_queue 創建一個隊列並將其設置為長輪詢。

  2. 使用 Aws:: SQS:: 用戶端 #set_queue_attributes,為現有佇列設定長輪詢。

  3. 使用 AWS:: SQS:: 用戶端 #receive_message,在接收佇列訊息時設定長輪詢。

先決條件

在執行範例程式碼之前,您必須先安裝並設定 AWS SDK for Ruby,如下所述:

您還需要建立現有佇列和接收佇列的佇列,您可以在 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