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

教程:配置 Amazon SQS 队列的长轮询

长轮询 通过消除空响应的数量 (ReceiveMessage 请求时没有消息可用时) 并消除假的空响应 (消息可用但未包含在响应中) 来帮助降低您使用 Amazon SQS 的成本。 在本教程中,您将学习如何为 Amazon SQS 队列配置长轮询。有关更多信息,请参阅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. 对于接收消息等待时间,键入一个介于 120 之间的数字。

    注意

    将值设置为 0 可配置短轮询。有关更多信息,请参阅长轮询和短轮询之间的区别

  7. 选择 Create Queue

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

    注意

    创建队列时,队列可能需要一小段时间在整个 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>

SQSLongPollingExample.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.CreateQueueRequest; import com.amazonaws.services.sqs.model.QueueAttributeName; import java.util.Scanner; public class SQSLongPollingExample { 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 ReceiveMessage wait time (1-20 seconds): "); final String receiveMessageWaitTime = 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 with long polling. final CreateQueueRequest createQueueRequest = new CreateQueueRequest() .withQueueName(queueName) .addAttributesEntry(QueueAttributeName.ReceiveMessageWaitTimeSeconds .toString(), receiveMessageWaitTime); sqs.createQueue(createQueueRequest); System.out.println("Created queue " + queueName + " with " + "ReceiveMessage wait time set to " + receiveMessageWaitTime + " 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. 检索队列的 URL:

    final String queueUrl = sqs.getQueueUrl(queueName).getQueueUrl();
  3. 使用 SetQueueAttributesRequest 操作配置现有队列的长轮询:

    final SetQueueAttributesRequest setQueueAttributesRequest = new SetQueueAttributesRequest() .withQueueUrl(queueUrl) .addAttributesEntry("ReceiveMessageWaitTimeSeconds", "20"); sqs.setQueueAttributes(setQueueAttributesRequest);
  4. 使用 ReceiveMessageRequest 操作配置消息接收的长轮询:

    final ReceiveMessageRequest receive_request = new ReceiveMessageRequest() .withQueueUrl(queueUrl) .withWaitTimeSeconds(20); sqs.receiveMessage(receive_request);
  5. 编译并运行程序。

    为您的队列配置了长轮询,并且发送、接收并删除了消息。