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

教程:配置 Amazon SQS 延迟队列

延迟队列可让您将针对队列的新消息传递操作推迟特定的秒数。如果您创建延迟队列,则发送到该队列的任何消息在延迟期间对用户都保持不可见。The minimum delay for a queue is 0 seconds. The maximum is 15 minutes.在本教程中,您将学习如何使用 AWS 管理控制台或使用 AWS SDK for Java 配置延迟队列。有关更多信息,请参阅 Amazon SQS 延迟队列

AWS 管理控制台

  1. 登录 Amazon SQS 控制台

  2. 选择 Create New Queue。

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

    注意

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

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

  5. 选择 Configure Queue

  6. 在此示例中,将交付延迟设置为 1 分钟。

  7. 选择 Create Queue

    新队列配置为使用 1 分钟延迟,将创建此队列并在队列列表中选择它。

    注意

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

    您的队列的交付延迟将显示在详细信息选项卡上。

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>

SQSDelayQueueExample.java

以下示例 Java 代码创建标准队列并将其延迟设置为 1 分钟。

/* * 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.CreateQueueRequest; import com.amazonaws.services.sqs.model.QueueAttributeName; import com.amazonaws.services.sqs.model.SetQueueAttributesRequest; import java.util.Scanner; public class SQSDelayQueueExample { public static void main(String[] args) { final Scanner input = new Scanner(System.in); System.out.print("Enter the queue name: "); final String queueName = input.nextLine(); System.out.print("Enter the delay in seconds (0 seconds to 15 minutes): "); final String queueDelay = 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 queue. final CreateQueueRequest createQueueRequest = new CreateQueueRequest() .withQueueName(queueName); sqs.createQueue(createQueueRequest); // Set the delay for the queue. final String queueUrl = sqs.getQueueUrl(queueName) .getQueueUrl(); final SetQueueAttributesRequest request = new SetQueueAttributesRequest() .withQueueUrl(queueUrl) .addAttributesEntry(QueueAttributeName.DelaySeconds .toString(), queueDelay); sqs.setQueueAttributes(request); System.out.println("Created queue " + queueName + " with " + "delay set to " + queueDelay + " seconds."); } 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. 要配置延迟队列,请交付延迟值 (以秒为单位)。

    // Set the delay for the queue. final String queueUrl = sqs.getQueueUrl(queueName).getQueueUrl(); final SetQueueAttributesRequest request = new SetQueueAttributesRequest() .withQueueUrl(queueUrl) .addAttributesEntry(QueueAttributeName.DelaySeconds.toString(), queueDelay); sqs.setQueueAttributes(request);
  3. 编译并运行程序。

    配置一条消息或多条消息的可见性超时。