Tutorial: Configuring long polling for an Amazon SQS queue - Amazon Simple Queue Service

Tutorial: Configuring long polling for an Amazon SQS queue

When the wait time for the ReceiveMessage API action is greater than 0, long polling is in effect. The maximum long polling wait time is 20 seconds. 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 learn how to configure long polling for an Amazon SQS queue. For more information, see Amazon SQS short and 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.

  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

The following example uses the AWS Java SDK. To install and set up the SDK, see Set up the AWS SDK for Java in the AWS SDK for Java Developer Guide.

Before you run the example code, configure 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

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

/* * Copyright 2010-2019 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 * * https://aws.amazon.com/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()); } } }

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 and the message is sent, received, and deleted.