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.

Working with Amazon SQS Message Queues

A message queue is the logical container you use to send messages reliably in Amazon SQS. There are two types of queues: standard and first-in, first-out (FIFO). To learn more about queues and the differences between these types, see the Amazon SQS Developer Guide.

These C++ examples show you how to use the AWS SDK for C++ to create, list, delete, and get the URL of an Amazon SQS queue.

Create a Queue#

Use the SQSClient class CreateQueue member function, and provide it with a CreateQueueRequest object that describes the queue parameters.

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 cq_req;
cq_req.SetQueueName(queue_name);

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

See the complete example.

List Queues#

To list Amazon SQS queues for your account, call the SQSClient class ListQueues member function, and pass it a ListQueuesRequest object.

Includes

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

Code

Aws::SQS::SQSClient sqs;

Aws::SQS::Model::ListQueuesRequest lq_req;

auto lq_out = sqs.ListQueues(lq_req);
if (lq_out.IsSuccess()) {
    std::cout << "Queue Urls:" << std::endl << std::endl;
    const auto &queue_urls = lq_out.GetResult().GetQueueUrls();
    for (const auto &iter : queue_urls) {
        std::cout << " " << iter << std::endl;
    }
} else {
    std::cout << "Error listing queues: " <<
        lq_out.GetError().GetMessage() << std::endl;
}

See the complete example.

Get the Queue's URL#

To get the URL for an existing Amazon SQS queue, call the SQSClient class GetQueueUrl member function.

Includes

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

Code

Aws::SQS::SQSClient sqs;

Aws::SQS::Model::GetQueueUrlRequest gqu_req;
gqu_req.SetQueueName(queue_name);

auto gqu_out = sqs.GetQueueUrl(gqu_req);
if (gqu_out.IsSuccess()) {
    std::cout << "Queue " << queue_name << " has url " <<
    gqu_out.GetResult().GetQueueUrl() << std::endl;
} else {
    std::cout << "Error getting url for queue " << queue_name << ": " <<
        gqu_out.GetError().GetMessage() << std::endl;
}

See the complete example.

Delete a Queue#

Provide the URL to the SQSClient class DeleteQueue member function.

Includes

#include <aws/core/Aws.h>
#include <aws/core/client/DefaultRetryStrategy.h>
#include <aws/sqs/SQSClient.h>
#include <aws/sqs/model/DeleteQueueRequest.h>

Code

Aws::Client::ClientConfiguration client_cfg;
client_cfg.retryStrategy =
    Aws::MakeShared<Aws::Client::DefaultRetryStrategy>(
        "sqs_delete_queue", 0);
Aws::SQS::SQSClient sqs(client_cfg);

Aws::SQS::Model::DeleteQueueRequest dq_req;
dq_req.SetQueueUrl(queue_url);

auto dq_out = sqs.DeleteQueue(dq_req);
if (dq_out.IsSuccess()) {
    std::cout << "Successfully deleted queue with url " << queue_url <<
        std::endl;
} else {
    std::cout << "Error deleting queue " << queue_url << ": " <<
        dq_out.GetError().GetMessage() << std::endl;
}

See the complete example.

More Info#