4

I am currently using amazon's SQS, and am having issues when trying to delete queue messages that are currently "In Flight".

Below is some example code:

ReceiveMessageRequest queueRequest = new ReceiveMessageRequest();
        queueRequest.WithMaxNumberOfMessages(1);
        queueRequest.WithQueueUrl(config.QueueURL);
        queueRequest.WithAttributeName("All");

        ReceiveMessageResponse response = sqs.ReceiveMessage(queueRequest);

        if (response.IsSetReceiveMessageResult())
        {
            ReceiveMessageResult result = response.ReceiveMessageResult;

            if (result.IsSetMessage())
            {
                if (result.Message.First() != null)
                {
                    return new Tuple<string, string, bool>(result.Message.First().ReceiptHandle, result.Message.First().Body ?? null, false);
                }
            }
        }

Now after receiving both the Handle, and the message body, I store the Receipt handle string into cloud storage (DynamoDB for example). I then later on load that handle out of the storage service, and call a delete with something similar to the following:

sqs.DeleteMessage(new DeleteMessageRequest() { QueueUrl = "urL", ReceiptHandle = handle });

However, when running that line, I receive a "The input receipt handle is invalid" error message.

Note, I know that this message has not gotten re-received, so the receipt handle being logged, should be the most recent one. Also note that I can currently delete messages within that same application, simply by sleeping after the message is received, and then trying to delete it as above!

Any ideas?

Samuel Raghunath
  • 155
  • 1
  • 13

3 Answers3

3

Make sure you are using the exact same URL that references the queue when you are executing both operations (ReceiveMessage and DeleteMessage).

b-s-d
  • 4,953
  • 2
  • 25
  • 31
2

You have to wait until a message's VisibilityTimeout has expired before you can act on it again. This allows the machine handling that message the time to do whatever it needs to do.

Ryan Parman
  • 6,855
  • 1
  • 29
  • 43
  • 1
    So let's say a Machine A is handing a message X. Machine A sets the visibility timeout to 2 days from now. And then it starts to operate on this message (delete it). Would that work? – Ravindranath Akila Jun 08 '15 at 10:58
0

The ReceiptHandle contains some special characters, which are sensitive to URL. So You need to first URL encode it. I did the same and it worked.

Hari Das
  • 10,145
  • 7
  • 62
  • 59