Amazon Simple Queue Service
Developer Guide

Tutorial: Configuring Long Polling for an Amazon SQS Queue

Long polling helps reduce the cost of using Amazon SQS by eliminating the number of empty responses (when there are no messages available for a ReceiveMessage request) and false empty responses (when messages are available but aren't included in a response). In this tutorial you'll learn how to configure long polling for an Amazon SQS queue. For more information, see Amazon SQS Long Polling.

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.


    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. For Receive Message Wait Time, type a number between 1 and 20.


    Setting the value to 0 configures short polling. For more information, see Differences Between Long and Short Polling.

  7. Choose Create Queue.

    Your new queue is configured to use long polling, created, and selected in the queue list.


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

AWS SDK for 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 long polling for a queue


Add the aws-java-sdk-sqs.jar package to your Java class path. The following example shows this dependency in a Maven project pom.xml file.

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

The following example Java code creates a standard queue and configures long polling for it.

/* * Copyright 2010-2018, 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 * * * * 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; import; import; import; import java.util.Scanner; public class SQSLongPollingExample { public static void main(String[] args) { final Scanner input = new Scanner(; 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()); } } }

To configure long polling for a queue and send, receive, and delete a message

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

  2. Retrieve the queue's URL:

    final String queueUrl = sqs.getQueueUrl(queueName).getQueueUrl();
  3. Use the SetQueueAttributesRequest action to configure long polling for an existing queue:

    final SetQueueAttributesRequest setQueueAttributesRequest = new SetQueueAttributesRequest() .withQueueUrl(queueUrl) .addAttributesEntry("ReceiveMessageWaitTimeSeconds", "20"); sqs.setQueueAttributes(setQueueAttributesRequest);
  4. Use the ReceiveMessageRequest action to configure the long polling for a message receipt:

    final ReceiveMessageRequest receive_request = new ReceiveMessageRequest() .withQueueUrl(queueUrl) .withWaitTimeSeconds(20); sqs.receiveMessage(receive_request);
  5. Compile and run your program.

    The long-polling for your queue is configured.