Exemple #1
0
bool deleteAllMessages (SQSConnectionPtr aSQS, std::string aQueueName) {
    try {
        bool lMessagesReceived = true;
        ReceiveMessageResponse::Message lMessage;
        ReceiveMessageResponsePtr lReceiveMessages;

        while (lMessagesReceived) {
            lMessagesReceived = false;
            lReceiveMessages = aSQS->receiveMessage (aQueueName);
            lReceiveMessages->open();
            std::cout << "start deleting messages:" << std::endl;
            while (lReceiveMessages->next (lMessage)) {
                lMessagesReceived = true;
                std::cout << "   message-id: [" << lMessage.message_id  << "]" << std::endl;
                DeleteMessageResponsePtr lDelRes = aSQS->deleteMessage (aQueueName, lMessage.receipt_handle);
            }
            lReceiveMessages->close();
        }

        std::cout << "deleted messages successfully" << std::endl;
    } catch (CreateBucketException &e) {
        std::cerr << e.what() << std::endl;
        return false;
    }
    return true;
}
int
testMessages(SQSConnection* lSQSCon)
{
  {
    try {
    	// create queue
      CreateQueueResponsePtr lCreateQueue = lSQSCon->createQueue("aQueue");
      std::string lAQueueURL = lCreateQueue->getQueueUrl();
      std::cout << "Bucket created successfully. QueueUrl: " << lAQueueURL << std::endl;

      // send message
      SendMessageResponsePtr lSendResponse = lSQSCon->sendMessage(lAQueueURL, "my cool body");
      std::string lMessageId = lSendResponse->getMessageId();
      if (lMessageId == "") {
      	std::cout << "Emtpy message ID" << std::cout;
      	return 1;
      }
      std::cout << "Message sent. ID: " << lMessageId << std::endl;

      // receive message
      ReceiveMessageResponsePtr lReceiveResponse = lSQSCon->receiveMessage(lAQueueURL,
      		1, -1);
      ReceiveMessageResponse::Message lMessage;
      int lCount = 0;
      lReceiveResponse->open();
      while (lReceiveResponse->next(lMessage)) {
      	std::cout << "Message received. ID: " << lMessage.message_id << std::endl;
      	std::cout << "Message body: ";
      	std::cout.write(lMessage.message_body, lMessage.message_size);
      	std::cout << std::endl;
      	lCount++;
      }
      lReceiveResponse->close();
      if (lCount != 1) {
      	std::cout << "Wrong number of messages (exp. 1): " << lCount << std::endl;
      	return 1;
      }

      // delete message
      DeleteMessageResponsePtr lDeleteResponse = lSQSCon->deleteMessage(lAQueueURL,
      		lMessage.receipt_handle);
      std::cout << "Message with ID " << lMessage.message_id << " has been deleted" << std::endl;

      // delete queue
			DeleteQueueResponsePtr lDeleteQueue = lSQSCon->deleteQueue(lAQueueURL);
      std::cout << "Queue " << lAQueueURL << " has been deleted" << std::endl;

    } catch (CreateBucketException& e) {
      std::cerr << "Couldn't create bucket" << std::endl;
      std::cerr << e.what() << std::endl;
      return 1;
    }
  }
  return 0;
}
Exemple #3
0
bool receiveMessage (SQSConnectionPtr aSQS, std::string aQueueName,  int aMaxNbMessages, int aVisibilityTimeout, bool aDecode = true) {
    try {
        ReceiveMessageResponsePtr lReceiveMessages = aSQS->receiveMessage (aQueueName, aMaxNbMessages, aVisibilityTimeout, aDecode);
        lReceiveMessages->open();
        ReceiveMessageResponse::Message lMessage;
        std::cout << "received messages:" << std::endl;
        int lNb = 0;
        while (lReceiveMessages->next (lMessage)) {
            std::cout << "  Message Nb. " << lNb++ << std::endl;
            std::cout << "    message-id: [" << lMessage.message_id << "]" << std::endl;
            std::cout << "    message-handle: [" << lMessage.receipt_handle << "]"<< std::endl;
            std::cout << "    message-md5: [" << lMessage.message_md5 << "]" << std::endl;
            std::cout << "    message-size: [" << lMessage.message_size << "]" << std::endl;
            std::cout << "    message-content: [";
            std::cout.write (lMessage.message_body, lMessage.message_size);
            std::cout << "]" << std::endl;
        }
        lReceiveMessages->close();
    } catch (ListBucketException &e) {
        std::cerr << e.what() << std::endl;
        return false;
    }
    return true;
}