Working with Amazon SQS Message Queues - AWS SDK for C++

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 Simple Queue Service 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.

Prerequisites

Before you begin, we recommend you read Getting started using the AWS SDK for C++.

Download the example code and build the solution as described in Get started on code examples.

To run the examples, the user profile your code uses to make the requests must have proper permissions in AWS (for the service and the action). For more information, see Providing AWS credentials.

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 <iostream>

Code

Aws::SQS::SQSClient sqsClient(clientConfiguration); Aws::SQS::Model::CreateQueueRequest request; request.SetQueueName(queueName); const Aws::SQS::Model::CreateQueueOutcome outcome = sqsClient.CreateQueue(request); if (outcome.IsSuccess()) { std::cout << "Successfully created queue " << queueName << " with a queue URL " << outcome.GetResult().GetQueueUrl() << "." << std::endl; } else { std::cerr << "Error creating queue " << queueName << ": " << outcome.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 <iostream>

Code

Aws::SQS::SQSClient sqsClient(clientConfiguration); Aws::SQS::Model::ListQueuesRequest listQueuesRequest; Aws::String nextToken; // Used for pagination. Aws::Vector<Aws::String> allQueueUrls; do { if (!nextToken.empty()) { listQueuesRequest.SetNextToken(nextToken); } const Aws::SQS::Model::ListQueuesOutcome outcome = sqsClient.ListQueues( listQueuesRequest); if (outcome.IsSuccess()) { const Aws::Vector<Aws::String> &queueUrls = outcome.GetResult().GetQueueUrls(); allQueueUrls.insert(allQueueUrls.end(), queueUrls.begin(), queueUrls.end()); nextToken = outcome.GetResult().GetNextToken(); } else { std::cerr << "Error listing queues: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken.empty()); std::cout << allQueueUrls.size() << " Amazon SQS queue(s) found." << std::endl; for (const auto &iter: allQueueUrls) { std::cout << " " << iter << 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 <iostream>

Code

Aws::SQS::SQSClient sqsClient(clientConfiguration); Aws::SQS::Model::GetQueueUrlRequest request; request.SetQueueName(queueName); const Aws::SQS::Model::GetQueueUrlOutcome outcome = sqsClient.GetQueueUrl(request); if (outcome.IsSuccess()) { std::cout << "Queue " << queueName << " has url " << outcome.GetResult().GetQueueUrl() << std::endl; } else { std::cerr << "Error getting url for queue " << queueName << ": " << outcome.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> #include <iostream>

Code

Aws::SQS::Model::DeleteQueueRequest request; request.SetQueueUrl(queueURL); const Aws::SQS::Model::DeleteQueueOutcome outcome = sqsClient.DeleteQueue(request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted queue with url " << queueURL << std::endl; } else { std::cerr << "Error deleting queue " << queueURL << ": " << outcome.GetError().GetMessage() << std::endl; }

See the complete example.

More Info