2023-05-05 14:44:00 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/
2021-11-15 13:15:43 +00:00
// snippet-start:[sqs.cpp.make_redrive_policy.inc]
# include <aws/core/Aws.h>
# include <aws/core/utils/json/JsonSerializer.h>
// snippet-end:[sqs.cpp.make_redrive_policy.inc]
// snippet-start:[sqs.cpp.set_redrive_policy.inc]
# include <aws/sqs/SQSClient.h>
# include <aws/sqs/model/SetQueueAttributesRequest.h>
# include <iostream>
// snippet-end:[sqs.cpp.set_redrive_policy.inc]
2023-05-05 14:44:00 -04:00
# include "sqs_samples.h"
static Aws : : String MakeRedrivePolicy ( const Aws : : String & queueArn , int maxReceiveCount ) ;
// snippet-start:[cpp.example_code.sqs.SetDeadLetterQueue]
//! Connect an Amazon Simple Queue Service (Amazon SQS) queue to an associated
//! dead-letter queue.
/*!
\param srcQueueUrl: An Amazon SQS queue URL.
\param deadLetterQueueARN: The Amazon Resource Name (ARN) of an Amazon SQS dead-letter queue.
\param maxReceiveCount: The max receive count of a message before it is sent to the dead-letter queue.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
bool AwsDoc : : SQS : : setDeadLetterQueue ( const Aws : : String & srcQueueUrl ,
const Aws : : String & deadLetterQueueARN ,
int maxReceiveCount ,
const Aws : : Client : : ClientConfiguration & clientConfiguration ) {
Aws : : String redrivePolicy = MakeRedrivePolicy ( deadLetterQueueARN , maxReceiveCount ) ;
Aws : : SQS : : SQSClient sqsClient ( clientConfiguration ) ;
// snippet-start:[sqs.cpp.set_redrive_policy.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 ;
}
// snippet-end:[sqs.cpp.set_redrive_policy.code]
2021-11-15 13:15:43 +00:00
2023-05-05 14:44:00 -04:00
return outcome . IsSuccess ( ) ;
}
//! Make a redrive policy for a dead-letter queue.
/*!
\param queueArn: An Amazon SQS ARN for the dead-letter queue.
\param maxReceiveCount: The max receive count of a message before it is sent to the dead-letter queue.
\return Aws::String: Policy as JSON string.
*/
2021-11-15 13:15:43 +00:00
// snippet-start:[sqs.cpp.make_redrive_policy.code]
2023-05-05 14:44:00 -04:00
Aws : : String MakeRedrivePolicy ( const Aws : : String & queueArn , int maxReceiveCount ) {
2021-11-15 13:15:43 +00:00
Aws : : Utils : : Json : : JsonValue redrive_arn_entry ;
2023-05-05 14:44:00 -04:00
redrive_arn_entry . AsString ( queueArn ) ;
2021-11-15 13:15:43 +00:00
Aws : : Utils : : Json : : JsonValue max_msg_entry ;
2023-05-05 14:44:00 -04:00
max_msg_entry . AsInteger ( maxReceiveCount ) ;
2021-11-15 13:15:43 +00:00
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 ( ) ;
}
// snippet-end:[sqs.cpp.make_redrive_policy.code]
2023-05-05 14:44:00 -04:00
// snippet-end:[cpp.example_code.sqs.SetDeadLetterQueue]
2021-11-15 13:15:43 +00:00
2023-05-05 14:44:00 -04:00
/*
*
* main function
*
* Usage: 'run_dead_letter_queue <source_queue_url> <dead_letter_queue_arn> <max_messages>'
*
* Prerequisites: An existing Amazon SQS queue and an existing dead-letter queue.
*
2021-11-15 13:15:43 +00:00
*/
2023-05-05 14:44:00 -04:00
# ifndef TESTING_BUILD
int main ( int argc , char * * argv ) {
if ( argc ! = 4 ) {
std : : cout < < " Usage: run_dead_letter_queue <source_queue_url> " < <
" <dead_letter_queue_arn> <max_messages> " < < std : : endl ;
2021-11-15 13:15:43 +00:00
return 1 ;
}
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
{
2023-05-05 14:44:00 -04:00
Aws : : String srcQueueUrl = argv [ 1 ] ;
Aws : : String deadLetterQueueArn = argv [ 2 ] ;
2021-11-15 13:15:43 +00:00
Aws : : StringStream ss ( argv [ 3 ] ) ;
2023-05-05 14:44:00 -04:00
int maxReceiveCount = 1 ;
ss > > maxReceiveCount ;
2021-11-15 13:15:43 +00:00
2023-05-05 14:44:00 -04:00
// snippet-start:[cpp.example_code.sqs.SetDeadLetterQueue.config]
Aws : : Client : : ClientConfiguration clientConfig ;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
// snippet-end:[cpp.example_code.sqs.SetDeadLetterQueue.config]
2021-11-15 13:15:43 +00:00
2023-05-05 14:44:00 -04:00
AwsDoc : : SQS : : setDeadLetterQueue ( srcQueueUrl , deadLetterQueueArn ,
maxReceiveCount , clientConfig ) ;
2021-11-15 13:15:43 +00:00
}
Aws : : ShutdownAPI ( options ) ;
return 0 ;
}
2023-05-05 14:44:00 -04:00
# endif // TESTING_BUILD