Menu
Amazon Simple Queue Service
Developer Guide

Tutorial: Configuring an Amazon SQS Dead-Letter Queue

A dead-letter queue is a queue that other (source) queues can target for messages that can't be processed (consumed) successfully. The following example demonstrates how to create a queue and to configure a dead-letter queue for it. For more information, see Using Amazon SQS Dead-Letter Queues.

Important

The dead-letter queue of a FIFO queue must also be a FIFO queue. Similarly, the dead-letter queue of a standard queue must also be a standard queue.

AWS Management Console

  1. Sign in to the Amazon SQS console.

  2. Choose Create New Queue.

  3. On the Create New Queue page, ensure that you're in the correct region and then type the Queue Name.

    Note

    The name of a FIFO queue must end with the .fifo suffix. FIFO queues are available in the US East (N. Virginia), US East (Ohio), US West (Oregon), and EU (Ireland) regions.

  4. Standard is selected by default. Choose FIFO.

  5. Choose Configure Queue.

  6. In this example, you enable the redrive policy for your new queue, set the MyDeadLetterQueue.fifo queue as the dead-letter queue, and set the number of maximum receives to 50.

    1. To configure the dead-letter queue, choose Use Redrive Policy.

    2. Enter the name of the existing Dead Letter Queue to which you want sources queues to send messages.

    3. To configure the number of times that a message can be received before being sent to a dead-letter queue, set Maximum Receives to a value between 1 and 1,000.

      Note

      The Maximum Receives setting applies only to individual messages.

    4. Choose Create Queue.

    Your new dead-letter queue is created and selected in the queue list.

    Note

    When you create a queue, it can take a short time for the queue to propagate throughout Amazon SQS.

    Your queue's Maximum Receives and Dead Letter Queue ARN are displayed on the Redrive Policy tab.

Java

Before you begin working with the example code, specify your AWS credentials. For more information, see Set up AWS Credentials and Region for Development in the AWS SDK for Java Developer Guide.

To configure a dead-letter queue

  1. Copy the example program for a standard queue or a FIFO queue.

  2. Set a string that contains JSON-formatted parameters and values for the RedrivePolicy queue attribute:

    Copy
    String redrivePolicy = "{\"maxReceiveCount\":\"5\", \"deadLetterTargetArn\":\"arn:aws:sqs:us-east-2:123456789012:MyDeadLetterQueue\"}";
  3. Use the CreateQueue or SetQueueAttributesRequest API action to set the RedrivePolicy queue attribute:

    Copy
    SetQueueAttributesRequest queueAttributes = new SetQueueAttributesRequest(); Map<String,String> attributes = new HashMap<String,String>(); attributes.put("RedrivePolicy", redrivePolicy); queueAttributes.setAttributes(attributes); queueAttributes.setQueueUrl(myQueueUrl); sqs.setQueueAttributes(queueAttributes);
  4. Compile and run your program.

    The dead-letter queue is configured.

Sample Request

Copy
http://sqs.us-east-2.amazonaws.com/123456789012/MySourceQueue ?Action=SetQueueAttributes &Attribute.1.Value=%7B%22maxReceiveCount%22%3A%225%22%2C+%22deadLetterTargetArn%22%3A%22arn%3Aaws%3Asqs%3Aus-east-2%3A123456789012%3AMyDeadLetterQueue%22%7D &Version=2012-11-05 &Attribute.1.Name=RedrivePolicy

Note

Queue names and queue URLs are case-sensitive.

Sample Response

Copy
<SetQueueAttributesResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/"> <ResponseMetadata> <RequestId>40945605-b328-53b5-aed4-1cc24a7240e8</RequestId> </ResponseMetadata> </SetQueueAttributesResponse>