在 Amazon SQS 中使用 QueuePoller 类接收消息 - Amazon 适用于 Ruby 的 SDK
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

在 Amazon SQS 中使用 QueuePoller 类接收消息

以下示例使用 QueuePoller 实用程序类来显示 us-west-2 区域中 URL 为 URL 的 Amazon SQS 队列中所有消息的正文,然后删除消息。不活动时间达到大约 15 秒后,脚本超时。

# 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' Aws.config.update({region: 'us-west-2'}) poller = Aws::SQS::QueuePoller.new(URL) poller.poll(idle_timeout: 15) do |msg| puts msg.body end

以下示例循环访问 URL 为 URL 的 Amazon SQS 队列,并最长等待 duration 中指定的秒数。

您可以通过执行获取有关 Amazon SQS 中所有队列的信息中的 Amazon SQS 示例来获取正确的 URL。

# 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' Aws.config.update({region: 'us-west-2'}) poller = Aws::SQS::QueuePoller.new(URL) poller.poll(wait_time_seconds: duration, idle_timeout: duration + 1) do |msg| puts msg.body end

以下示例循环访问 URL 为 URL 的 Amazon SQS 队列,并为您提供长达可见性 timeout 中指定的秒数来处理消息,具体操作由方法 do_something 表示。

# 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' # Process the message def do_something(msg) puts msg.body end Aws.config.update({region: 'us-west-2'}) poller = Aws::SQS::QueuePoller.new(URL) poller.poll(visibility_timeout: timeout, idle_timeout: timeout + 1) do |msg| do_something(msg) end

以下示例循环访问 URL 为 URL 的 Amazon SQS 队列,并为需要通过方法 do_something2 进行其他处理的任何消息更改可见性 timeout 指定的秒数。

# 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' # Process the message def do_something(_) true end # Do additional processing def do_something2(msg) puts msg.body end Aws.config.update({region: 'us-west-2'}) poller = Aws::SQS::QueuePoller.new(URL) poller.poll(idle_timeout: timeout + 1) do |msg| if do_something(msg) # need more time for processing poller.change_message_visibility_timeout(msg, timeout) do_something2(msg) end end