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

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

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

以下示例使用QueuePoller实用程序类来显示 Amazon SQS 队列中所有消息的正文,URL 为URL中的us-west-2区域,并删除该消息。不活动时间达到大约 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 为的 Amazon SQS 队列URL,并等待持续时间秒。

您可以通过执行 Amazon SQS 示例来获取正确的 URL获取有关 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' 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 为的 Amazon SQS 队列URL,并为您提供可见性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 为的 Amazon SQS 队列URL,并更改可见性timeout秒,对于需要通过方法进行额外处理的任何消息do_something2

# 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