本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在 Amazon SQS 中使用长轮询接收消息
以下示例将等待最多 10 秒来显示 us-west-2
区域中 URL 为 URL
的 Amazon SQS 队列中的最多 10 条消息的正文。
如果您没有指定等待时间,则默认值为 0(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' sqs = Aws::SQS::Client.new(region: 'us-west-2') resp = sqs.receive_message(queue_url: URL, max_number_of_messages: 10, wait_time_seconds: 10) resp.messages.each do |m| puts m.body end