AWS SDK for C++
Developer Guide

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.

Note

These code snippets assume that you understand the material in Getting Started Using the AWS SDK for C++ and have configured default AWS credentials using the information in 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

CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/sqs/SQSClient.h>

Code

Aws::String queue_name = argv[1]; 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 {

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

CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/sqs/SQSClient.h>

Code

:SDKOptions options; :InitAPI(options); 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 {

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

CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/sqs/SQSClient.h>

Code

Aws::String queue_name = argv[1]; 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 {

See the complete example.

Delete a Queue

Provide the URL to the SQSClient class DeleteQueue member function.

Includes

CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/core/client/DefaultRetryStrategy.h>

Code

Aws::String queue_url = argv[1]; // disable retries so that bad urls don't hang the exe via retry loop 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 {

See the complete example.

More Info