Working with Amazon SQS Message Queues - Amazon SDK for C++
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

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 Amazon 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 Amazon 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 Amazon (for the service and the action). For more information, see Providing Amazon 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 lq_req; const Aws::SQS::Model::ListQueuesOutcome outcome = sqsClient.ListQueues(lq_req); if (outcome.IsSuccess()) { std::cout << "Queue Urls:" << std::endl << std::endl; const auto &queue_urls = outcome.GetResult().GetQueueUrls(); for (const auto &iter: queue_urls) { std::cout << " " << iter << std::endl; } } else { std::cerr << "Error listing queues: " << outcome.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 <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