Amazon SQS でのキューの使用 - AWS SDK for Ruby

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Amazon SQS でのキューの使用

Amazon SQS は、アプリケーションまたはマイクロサービス間をメッセージが移動する間に、それらを保存するための、スケーラブルなホストされたキューを提供します。キューの詳細については、「Amazon SQS キューの操作」を参照してください。

この例では、AWS SDK for Ruby を 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 を使用してキューを削除する。

前提条件

コード例を実行する前に、以下で説明されているように、AWS SDK for Ruby をインストールし、設定する必要があります。

# 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)