AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Enabling Long Polling for Amazon SQS Message Queues

Amazon SQS uses short polling by default, querying only a subset of the servers—based on a weighted random distribution—to determine whether any messages are available for inclusion in the response.

Long polling helps reduce your cost of using Amazon SQS by reducing the number of empty responses when there are no messages available to return in reply to a ReceiveMessage request sent to an Amazon SQS queue and eliminating false empty responses.

Note

You can set a long polling frequency from 1–20 seconds.

Enabling Long Polling when Creating a Queue#

To enable long polling when creating an Amazon SQS queue, set the ReceiveMessageWaitTimeSeconds attribute on the CreateQueueRequest object before calling the SQSClient class' CreateQueue member function.

Includes

#include <aws/core/Aws.h>
#include <aws/sqs/SQSClient.h>
#include <aws/sqs/model/CreateQueueRequest.h>
#include <aws/sqs/model/CreateQueueResult.h>

Code

Aws::SQS::SQSClient sqs;

Aws::SQS::Model::CreateQueueRequest request;
request.SetQueueName(queue_name);
request.AddAttributes(
    Aws::SQS::Model::QueueAttributeName::ReceiveMessageWaitTimeSeconds,
    poll_time);

auto outcome = sqs.CreateQueue(request);
if (outcome.IsSuccess()) {
    std::cout << "Successfully created queue " << queue_name <<
        std::endl;
} else {
    std::cout << "Error creating queue " << queue_name << ": " <<
        outcome.GetError().GetMessage() << std::endl;
}

See the complete example.

Enabling Long Polling on an Existing Queue#

In addition to enabling long polling when creating a queue, you can also enable it on an existing queue by setting ReceiveMessageWaitTimeSeconds on the SetQueueAttributesRequest before calling the SQSClient class' SetQueueAttributes member function.

Includes

#include <aws/core/Aws.h>
#include <aws/sqs/SQSClient.h>
#include <aws/sqs/model/SetQueueAttributesRequest.h>

Code

Aws::SQS::SQSClient sqs;

Aws::SQS::Model::SetQueueAttributesRequest request;
request.SetQueueUrl(queue_url);
request.AddAttributes(
    Aws::SQS::Model::QueueAttributeName::ReceiveMessageWaitTimeSeconds,
    poll_time);

auto outcome = sqs.SetQueueAttributes(request);
if (outcome.IsSuccess()) {
    std::cout << "Successfully updated long polling time for queue " <<
        queue_url << " to " << poll_time << std::endl;
} else {
    std::cout << "Error updating long polling time for queue " <<
        queue_url << ": " << outcome.GetError().GetMessage() <<
        std::endl;
}

See the complete example.

Enabling Long Polling on Message Receipt#

You can enable long polling when receiving a message by setting the wait time in seconds on the ReceiveMessageRequest that you supply to the SQSClient class' ReceiveMessage member function.

Note

You should make sure that the AWS client's request timeout is larger than the maximum long poll time (20s) so that your ReceiveMessage requests don't time out while waiting for the next poll event!

Includes

#include <aws/core/Aws.h>
#include <aws/sqs/SQSClient.h>
#include <aws/sqs/model/ReceiveMessageRequest.h>
#include <aws/sqs/model/ReceiveMessageResult.h>

Code

Aws::Client::ClientConfiguration client_cfg;
client_cfg.requestTimeoutMs = 30000;

Aws::SQS::SQSClient sqs(client_cfg);

Aws::SQS::Model::ReceiveMessageRequest request;
request.SetQueueUrl(queue_url);
request.SetMaxNumberOfMessages(1);
request.SetWaitTimeSeconds(wait_time);

auto outcome = sqs.ReceiveMessage(request);

See the complete example.

More Info#