Using Dead Letter Queues in Amazon SQS - 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).

Using Dead Letter Queues in Amazon SQS

Amazon SQS provides support for dead letter queues. A dead letter queue is a queue that other queues can target for messages that can’t be processed successfully. You can set aside and isolate these messages in the dead letter queue to determine why their processing did not succeed.

To create a dead letter queue, you must first create a redrive policy, and then set the policy in the queue’s attributes.

Important

A dead letter queue must be the same type of queue (FIFO or standard) that the source queue is. It must also be created using the same Amazon Web Services account and Amazon Web Services Region as the source 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 Redrive Policy

A redrive policy is specified in JSON. To create it, you can use the JSON utility class provided with the Amazon SDK for C++.

Here is an example function that creates a redrive policy by providing it with the ARN of your dead letter queue and the maximum number of times the message can be received and not processed before it’s sent to the dead letter queue.

Includes

#include <aws/core/Aws.h> #include <aws/core/utils/json/JsonSerializer.h>

Code

Aws::String MakeRedrivePolicy(const Aws::String &queueArn, int maxReceiveCount) { Aws::Utils::Json::JsonValue redrive_arn_entry; redrive_arn_entry.AsString(queueArn); Aws::Utils::Json::JsonValue max_msg_entry; max_msg_entry.AsInteger(maxReceiveCount); Aws::Utils::Json::JsonValue policy_map; policy_map.WithObject("deadLetterTargetArn", redrive_arn_entry); policy_map.WithObject("maxReceiveCount", max_msg_entry); return policy_map.View().WriteReadable(); }

See the complete example.

Set the Redrive Policy on your Source Queue

To finish setting up your dead letter queue, call the SQSClient class’ SetQueueAttributes member function with a SetQueueAttributesRequest object for which you’ve set the RedrivePolicy attribute with your JSON redrive policy.

Includes

#include <aws/sqs/SQSClient.h> #include <aws/sqs/model/SetQueueAttributesRequest.h> #include <iostream>

Code

Aws::SQS::Model::SetQueueAttributesRequest request; request.SetQueueUrl(srcQueueUrl); request.AddAttributes( Aws::SQS::Model::QueueAttributeName::RedrivePolicy, redrivePolicy); const Aws::SQS::Model::SetQueueAttributesOutcome outcome = sqsClient.SetQueueAttributes(request); if (outcome.IsSuccess()) { std::cout << "Successfully set dead letter queue for queue " << srcQueueUrl << " to " << deadLetterQueueARN << std::endl; } else { std::cerr << "Error setting dead letter queue for queue " << srcQueueUrl << ": " << outcome.GetError().GetMessage() << std::endl; }

See the complete example.

More Info