Amazon Simple Queue Service
开发人员指南
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

教程:配置 Amazon SQS 死信队列

死信队列是其他 (源) 队列可将其作为无法成功处理 (使用) 的消息的目标的队列。在本教程中,您将学习如何创建 Amazon SQS 源队列以及如何将另一个队列配置为此源队列的死信队列。有关更多信息,请参阅 Amazon SQS 死信队列

重要

FIFO 队列的死信队列也必须为 FIFO 队列。同样,标准 队列的死信队列也必须为 标准 队列。

AWS 管理控制台

  1. 登录 Amazon SQS 控制台

  2. 选择 Create New Queue。

  3. Create New Queue 页上,确保您位于正确的区域,然后键入 Queue Name

    注意

    FIFO 队列的名称必须以 .fifo 后缀结尾。 FIFO 队列在美国东部(弗吉尼亚北部)、美国东部(俄亥俄州)、美国西部(俄勒冈) 和欧洲(爱尔兰) 区域中提供。

  4. 默认情况下已选择 标准。选择 FIFO

  5. 选择 Configure Queue

  6. 在此示例中,您将为新队列启用重新驱动策略、将 MyDeadLetterQueue.fifo 队列设置为死信队列,并将最大接收数设置为 50

    要配置死信队列,请选择 Use Redrive Policy

    输入您希望源队列向其发送消息的现有死信队列的名称。

    要配置消息在发送到死信队列之前可接收的次数,请将 Maximum Receives 设置为一个介于 1 和 1000 之间的值。

    注意

    Maximum Receives 设置仅应用于单个消息。

    选择 Create Queue

    您的新队列将配置为使用死信队列、创建并在队列列表中将其选定。

    注意

    创建队列时,队列可能需要一小段时间在整个 Amazon SQS 中传播。

    您的队列的 Maximum ReceivesDead Letter Queue ARN 将显示在 Redrive Policy 选项卡上。

AWS SDK for Java

在您开始处理示例代码之前,请指定 AWS 凭证。有关更多信息,请参阅 AWS SDK for Java Developer Guide 中的设置用于开发的 AWS 凭证和区域

配置死信队列

先决条件

aws-java-sdk-sqs.jar 程序包添加到 Java 类路径中。以下示例说明了 Maven 项目的 pom.xml 文件中的这一依赖关系。

<dependencies> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-sqs</artifactId> <version><replaceable>LATEST</replaceable></version> </dependency> </dependencies>

SQSDeadLetterQueueExample.java

以下示例 Java 代码创建两个标准队列,并将一个队列配置为另一个队列的源队列 (死信队列)。

/* * Copyright 2010-2018 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. * A copy of the License is located at * * http://amazonaws.cn/apache2.0 * * or in the "license" file accompanying this file. 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. * */ import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClientBuilder; import com.amazonaws.services.sqs.model.GetQueueAttributesRequest; import com.amazonaws.services.sqs.model.GetQueueAttributesResult; import com.amazonaws.services.sqs.model.QueueAttributeName; import com.amazonaws.services.sqs.model.SetQueueAttributesRequest; import java.util.Scanner; public class SQSDeadLetterQueueExample { public static void main(String[] args) { final Scanner input = new Scanner(System.in); System.out.print("Enter the source queue name: "); final String sourceQueueName = input.nextLine(); System.out.print("Enter the dead-letter queue name: "); final String deadLetterQueueName = input.nextLine(); /* * Create a new instance of the builder with all defaults (credentials * and region) set automatically. For more information, see * Creating Service Clients in the AWS SDK for Java Developer Guide. */ final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); try { // Create a source queue. sqs.createQueue(sourceQueueName); // Create a dead-letter queue. sqs.createQueue(deadLetterQueueName); // Get the dead-letter queue ARN. final String deadLetterQueueUrl = sqs.getQueueUrl(deadLetterQueueName) .getQueueUrl(); final GetQueueAttributesResult deadLetterQueueAttributes = sqs.getQueueAttributes( new GetQueueAttributesRequest(deadLetterQueueUrl) .withAttributeNames("QueueArn")); final String deadLetterQueueArn = deadLetterQueueAttributes.getAttributes().get("QueueArn"); // Set the dead letter queue for the source queue using the redrive policy. final String sourceQueueUrl = sqs.getQueueUrl(sourceQueueName) .getQueueUrl(); final SetQueueAttributesRequest request = new SetQueueAttributesRequest() .withQueueUrl(sourceQueueUrl) .addAttributesEntry(QueueAttributeName.RedrivePolicy.toString(), "{\"maxReceiveCount\":\"5\", \"deadLetterTargetArn\":\"" + deadLetterQueueArn + "\"}"); sqs.setQueueAttributes(request); System.out.println("Set queue " + sourceQueueName + " as source queue " + "for dead-letter queue " + deadLetterQueueName + "."); } catch (final AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means " + "your request made it to Amazon SQS, but was " + "rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (final AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means " + "the client encountered a serious internal problem while " + "trying to communicate with Amazon SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } } }

配置死信队列并发送、接收和删除消息

  1. 复制 标准 队列FIFO 队列的示例程序。

  2. 设置一个字符串,其中包含 JSON 格式的参数和 RedrivePolicy 队列属性的值:

    final String redrivePolicy = "{\"maxReceiveCount\":\"5\", \"deadLetterTargetArn\":\"arn:aws-cn:sqs:us-east-2:123456789012:MyDeadLetterQueue\"}";
  3. 使用 CreateQueueSetQueueAttributesRequest 操作配置 RedrivePolicy 队列属性:

    final SetQueueAttributesRequest queueAttributes = new SetQueueAttributesRequest(); final Map<String,String> attributes = new HashMap<String,String>(); attributes.put("RedrivePolicy", redrivePolicy); queueAttributes.setAttributes(attributes); queueAttributes.setQueueUrl(myQueueUrl); sqs.setQueueAttributes(queueAttributes);
  4. 编译并运行程序。

    已配置的死信队列。