在 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#list_queues 获取队列列表。

  2. 使用 Aws::SQS::Client#create_queue 创建队列

  3. 使用 Aws::SQS::Client#get_queue_url 获取队列的 URL。

  4. 使用 Aws::SQS::Client#delete_queue 删除队列。

先决条件

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

示例

# 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. Get a list of your queues. # 2. Create a queue. # 3. Get the queue's URL. # 4. Delete the queue. require 'aws-sdk-sqs' # v2: require 'aws-sdk' sqs = Aws::SQS::Client.new(region: 'us-east-1') # Get a list of your queues. sqs.list_queues.queue_urls.each do |queue_url| puts queue_url end # Create a queue. queue_name = "my-queue" begin sqs.create_queue({ queue_name: queue_name, attributes: { "DelaySeconds" => "60", # Delay message delivery for 1 minute (60 seconds). "MessageRetentionPeriod" => "86400" # Delete message after 1 day (24 hours * 60 minutes * 60 seconds). } }) rescue Aws::SQS::Errors::QueueDeletedRecently puts "A queue with the name '#{queue_name}' was recently deleted. Wait at least 60 seconds and try again." exit(false) end # Get the queue's URL. queue_url = sqs.get_queue_url(queue_name: queue_name).queue_url puts queue_url # Delete the queue. sqs.delete_queue(queue_url: queue_url)