Amazon SQS でのデッドレターキューの操作 - AWS SDK for Ruby

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

Amazon SQS でのデッドレターキューの操作

Amazon SQS では、デッドレターキューがサポートされます。デッドレターキューは、正常に処理できないメッセージの送信先として他の (送信元) キューが使用できるキューです。これらのメッセージは、処理が成功しなかった理由を判断するためにデッドレターキューに分離できます。デッドレターキューの詳細については、「Amazon SQS デッドレターキューの使用」を参照してください。

この例では、AWS SDK for Ruby を Amazon SQS で使用して、以下のことを行います。

  1. Aws::SQS::Client#create_queue を使用して、デッドレターキューを表すキューを作成する。

  2. Aws::SQS::Client#set_queue_attributes を使用して、デッドレターキューを既存のキューに関連付ける。

  3. Aws::SQS::Client#send_message を使用して、既存のキューにメッセージを送信する。

  4. Aws::SQS::QueuePoller を使用してキューをポーリングする。

  5. Aws::SQS::Client#receive_message を使用してデッドレターキューでメッセージを受信する。

前提条件

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

AWS Management Console を使用して、既存のキュー my-queue を作成する必要もあります。

注記

わかりやすくするために、このコード例では Aws::SQS::Client#add_permission を省略しています。実際のシナリオでは、SendMessage、ReceiveMessage、DeleteMessage、DeleteQueue などのアクションへのアクセスは常に制限する必要があります。そうしないと、情報漏洩、サービス拒否、またはキューへのメッセージのインジェクションが発生する可能性があります。

# 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. Create a queue representing a dead letter queue. # 2. Associate the dead letter queue with an existing queue. require 'aws-sdk-sqs' # v2: require 'aws-sdk' # Uncomment for Windows. # Aws.use_bundled_cert! sqs = Aws::SQS::Client.new(region: 'us-east-1') # Create a queue representing a dead letter queue. dead_letter_queue_name = "dead-letter-queue" sqs.create_queue({ queue_name: dead_letter_queue_name }) # Get the dead letter queue's URL and ARN, so that you can associate it with an existing queue. dead_letter_queue_url = sqs.get_queue_url(queue_name: dead_letter_queue_name).queue_url dead_letter_queue_arn = sqs.get_queue_attributes({ queue_url: dead_letter_queue_url, attribute_names: ["QueueArn"] }).attributes["QueueArn"] # Associate the dead letter queue with an existing queue. begin queue_name = "my-queue" queue_url = sqs.get_queue_url(queue_name: queue_name).queue_url # Use a redrive policy to specify the dead letter queue and its behavior. redrive_policy = { "maxReceiveCount" => "5", # After the queue receives the same message 5 times, send that message to the dead letter queue. "deadLetterTargetArn" => dead_letter_queue_arn }.to_json sqs.set_queue_attributes({ queue_url: queue_url, attributes: { "RedrivePolicy" => redrive_policy } }) rescue Aws::SQS::Errors::NonExistentQueue puts "A queue named '#{queue_name}' does not exist." exit(false) end # Send a message to the queue. puts "Sending a message..." sqs.send_message({ queue_url: queue_url, message_body: "I hope I get moved to the dead letter queue." }) 30.downto(0) do |i| print "\rWaiting #{i} second(s) for sent message to be receivable..." sleep(1) end puts "\n" poller = Aws::SQS::QueuePoller.new(queue_url) # Receive 5 messages max and stop polling after 20 seconds of no received messages. poller.poll(max_number_of_messages:5, idle_timeout: 20) do |messages| messages.each do |msg| puts "Received message ID: #{msg.message_id}" end end # Check to see if Amazon SQS moved the message to the dead letter queue. receive_message_result = sqs.receive_message({ queue_url: dead_letter_queue_url, max_number_of_messages: 1 }) if receive_message_result.messages.count > 0 puts "\n#{receive_message_result.messages[0].body}" else puts "\nNo messages received." end