//snippet-sourcedescription:[DeadLetterQueues.java demonstrates how to set a queue as a dead letter queue.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[sqs]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[2020-02-20]
//snippet-sourceauthor:[scmacdon-aws]
// snippet-start:[sqs.java2.delete_letter_queues.complete]
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License 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.
 */
// snippet-start:[sqs.java2.delete_letter_queues.import]
package com.example.sqs;
import software.amazon.awssdk.services.sqs.model.CreateQueueRequest;
import software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest;
import software.amazon.awssdk.services.sqs.model.GetQueueAttributesResponse;
import software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest;
import software.amazon.awssdk.services.sqs.model.QueueAttributeName;
import software.amazon.awssdk.services.sqs.model.QueueNameExistsException;
import software.amazon.awssdk.services.sqs.model.SetQueueAttributesRequest;
import software.amazon.awssdk.services.sqs.model.SetQueueAttributesResponse;

import java.util.Date;
import java.util.HashMap;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsClient;

// snippet-end:[sqs.java2.delete_letter_queues.import]
// snippet-start:[sqs.java2.delete_letter_queues.main]
public class DeadLetterQueues {
    private static final String QueueName = "testQueue" + new Date().getTime();
    private static final String DLQueueName = "DLQueue" + new Date().getTime();

    public static void main(String[] args) {
        SqsClient sqs = SqsClient.builder()
                .region(Region.US_WEST_2)
                .build();

        try {
            CreateQueueRequest request = CreateQueueRequest.builder()
                .queueName(QueueName).build();

             CreateQueueRequest dlrequest = CreateQueueRequest.builder()
                .queueName(DLQueueName).build();

            sqs.createQueue(dlrequest);

            GetQueueUrlRequest getRequest = GetQueueUrlRequest.builder()
                .queueName(DLQueueName)
                .build();

            // Get dead-letter queue ARN
            String dlQueueUrl = sqs.getQueueUrl(getRequest)
                .queueUrl();

            GetQueueAttributesResponse queueAttrs = sqs.getQueueAttributes(
                GetQueueAttributesRequest.builder()
                        .queueUrl(dlQueueUrl)
                        .attributeNames(QueueAttributeName.QUEUE_ARN).build());

            String dlQueueArn = queueAttrs.attributes().get(QueueAttributeName.QUEUE_ARN);

            // Set dead letter queue with redrive policy on source queue.
            GetQueueUrlRequest getRequestSource = GetQueueUrlRequest.builder()
                .queueName(DLQueueName)
                .build();

            String srcQueueUrl = sqs.getQueueUrl(getRequestSource)
                .queueUrl();

            HashMap<QueueAttributeName, String> attributes = new HashMap<QueueAttributeName, String>();
            attributes.put(QueueAttributeName.REDRIVE_POLICY, "{\"maxReceiveCount\":\"5\", \"deadLetterTargetArn\":\""
                + dlQueueArn + "\"}");

            SetQueueAttributesRequest setAttrRequest = SetQueueAttributesRequest.builder()
                .queueUrl(srcQueueUrl)
                .attributes(attributes)
                .build();

            SetQueueAttributesResponse setAttrResponse = sqs.setQueueAttributes(setAttrRequest);

        } catch (QueueNameExistsException e) {
            throw e;
        }
    }
}
// snippet-end:[sqs.java2.delete_letter_queues.main]
// snippet-end:[sqs.java2.delete_letter_queues.complete]

@@ -192,19 +192,43 @@
 ervice:%5B
-sqs
+Amazon Simple Queue Service
 %5D%0D%0A//sni
@@ -286,15 +286,14 @@
 e:%5B2
-020-02-
+/20/20
 20%5D%0D
@@ -336,66 +336,8 @@
 s%5D%0D%0A
-// snippet-start:%5Bsqs.java2.delete_letter_queues.complete%5D
 %0D%0A/*
@@ -881,16 +881,46 @@
 .%0D%0A */%0D%0A
+%0D%0Apackage com.example.sqs;%0D%0A%0D%0A
 // snipp
@@ -973,34 +973,8 @@
 t%5D%0D%0A
-package com.example.sqs;%0D%0A
 impo
@@ -1565,18 +1565,16 @@
 ponse;%0D%0A
-%0D%0A
 import j
@@ -1718,18 +1718,16 @@
 lient;%0D%0A
-%0D%0A
 // snipp
@@ -1778,62 +1778,8 @@
 t%5D%0D%0A
-// snippet-start:%5Bsqs.java2.delete_letter_queues.main%5D
 %0D%0Apu
@@ -2015,24 +2015,26 @@
 %5B%5D args) %7B%0D%0A
+%0D%0A
         SqsC
@@ -2130,32 +2130,198 @@
    .build();%0D%0A%0D%0A
+        setDeadLetterQueue(sqs);%0D%0A    %7D%0D%0A%0D%0A    // snippet-start:%5Bsqs.java2.delete_letter_queues.main%5D%0D%0A    public static void setDeadLetterQueue( SqsClient sqs) %7B%0D%0A%0D%0A
         try %7B%0D%0A 
@@ -4285,62 +4285,4 @@
 n%5D%0D%0A
-// snippet-end:%5Bsqs.java2.delete_letter_queues.complete%5D%0D%0A

Update DeadLetterQueues.java