Created
May 2, 2016 09:51
-
-
Save Valken/1bd01afa1d94f2b49dd0a22ff1693e92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Consumer.h" | |
| #include <aws/sqs/model/GetQueueUrlRequest.h> | |
| #include <aws/sqs/model/ReceiveMessageRequest.h> | |
| #include <aws/sqs/model/DeleteMessageBatchRequest.h> | |
| using namespace Aws; | |
| using namespace Aws::Client; | |
| using namespace Aws::SQS; | |
| using namespace Aws::SQS::Model; | |
| Consumer::Consumer(SQSClient& client, String const& queueName) : client(client) | |
| { | |
| GetQueueUrlRequest queueUrlRequest; | |
| queueUrlRequest.SetQueueName(queueName); | |
| auto queueUrlResponse = client.GetQueueUrl(queueUrlRequest); | |
| url = queueUrlResponse.GetResult().GetQueueUrl(); | |
| } | |
| Consumer::~Consumer() {} | |
| void Consumer::GetMessages(std::function<bool(String const& message)> handler) | |
| { | |
| ReceiveMessageRequest request; | |
| request.SetQueueUrl(url); | |
| request.SetWaitTimeSeconds(5); | |
| request.SetMaxNumberOfMessages(10); | |
| auto deleteRequest = DeleteMessageBatchRequest().WithQueueUrl(url); | |
| auto response = client.ReceiveMessage(request); | |
| for (auto const & message : response.GetResult().GetMessages()) | |
| { | |
| if (handler(message.GetBody())) | |
| { | |
| DeleteMessageBatchRequestEntry entry; | |
| entry.SetId(message.GetMessageId()); | |
| entry.SetReceiptHandle(message.GetReceiptHandle()); | |
| deleteRequest.AddEntries(entry); | |
| } | |
| } | |
| if (response.GetResult().GetMessages().size() > 0) | |
| client.DeleteMessageBatch(deleteRequest); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <aws/sqs/SQSCLient.h> | |
| class Consumer | |
| { | |
| public: | |
| Consumer(Aws::SQS::SQSClient& client, Aws::String const& queueName); | |
| ~Consumer(); | |
| void GetMessages(std::function<bool(Aws::String const& message)> handler); | |
| private: | |
| Aws::SQS::SQSClient& client; | |
| Aws::String url; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment