Menu
AWS SDK for C++
Developer Guide

Sending, Receiving, and Deleting Amazon SQS Messages

Messages are always delivered using an SQS queue. These C++ examples show you how to use the AWS SDK for C++ to send, receive, and delete Amazon SQS messages from SQS queues.

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.

Send a Message

You can add a single message to an Amazon SQS queue by calling the SQSClient class SendMessage member function. You provide SendMessage with a SendMessageRequest object containing the queue's URL, the message body, and an optional delay value (in seconds).

Includes

Copy
#include <aws/core/Aws.h> #include <aws/sqs/SQSClient.h> #include <aws/sqs/model/SendMessageRequest.h> #include <aws/sqs/model/SendMessageResult.h> #include <iostream>

Code

Copy
Aws::SQS::SQSClient sqs; Aws::SQS::Model::SendMessageRequest sm_req; sm_req.SetQueueUrl(queue_url); sm_req.SetMessageBody(msg_body); auto sm_out = sqs.SendMessage(sm_req); if (sm_out.IsSuccess()) { std::cout << "Successfully sent message to " << queue_url << std::endl; } else { std::cout << "Error sending message to " << queue_url << ": " << sm_out.GetError().GetMessage() << std::endl; }

See the complete example.

Receive Messages

Retrieve any messages that are currently in the queue by calling the SQSClient class ReceiveMessage member function, passing it the queue's URL. Messages are returned as a list of Message objects.

Includes

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

Code

Copy
Aws::Client::ClientConfiguration client_cfg; client_cfg.requestTimeoutMs = 30000; Aws::SQS::SQSClient sqs(client_cfg); Aws::SQS::Model::ReceiveMessageRequest rm_req; rm_req.SetQueueUrl(queue_url); rm_req.SetMaxNumberOfMessages(1); auto rm_out = sqs.ReceiveMessage(rm_req); if (!rm_out.IsSuccess()) { std::cout << "Error receiving message from queue " << queue_url << ": " << rm_out.GetError().GetMessage() << std::endl; return; } const auto& messages = rm_out.GetResult().GetMessages(); if (messages.size() == 0) { std::cout << "No messages received from queue " << queue_url << std::endl; return; } const auto& message = messages[0]; std::cout << "Received message:" << std::endl; std::cout << " MessageId: " << message.GetMessageId() << std::endl; std::cout << " ReceiptHandle: " << message.GetReceiptHandle() << std::endl; std::cout << " Body: " << message.GetBody() << std::endl << std::endl;

See the complete example.

Delete Messages after Receipt

After receiving a message and processing its contents, delete the message from the queue by sending the message's receipt handle and the queue URL to the SQSClient class DeleteMessage member function.

Includes

Copy
#include <aws/core/Aws.h> #include <aws/sqs/SQSClient.h> #include <aws/sqs/model/DeleteMessageRequest.h> #include <iostream>

Code

Copy
Aws::SQS::Model::DeleteMessageRequest dm_req; dm_req.SetQueueUrl(queue_url); dm_req.SetReceiptHandle(message.GetReceiptHandle()); auto dm_out = sqs.DeleteMessage(dm_req); if (dm_out.IsSuccess()) { std::cout << "Successfully deleted message " << message.GetMessageId() << " from queue " << queue_url << std::endl; } else { std::cout << "Error deleting message " << message.GetMessageId() << " from queue " << queue_url << ": " << dm_out.GetError().GetMessage() << std::endl; }

See the complete example.

More Info