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?

4

3 回答 3

3

确保在执行这两个操作(ReceiveMessage 和 DeleteMessage)时使用引用队列的完全相同的 URL。

于 2015-04-01T17:26:54.370 回答
2

您必须等到消息的 VisibilityTimeout 过期才能再次对其执行操作。这允许处理该消息的机器有时间做它需要做的任何事情。

于 2012-10-05T08:52:54.857 回答
0

ReceiptHandle 包含一些对 URL 敏感的特殊字符。所以你需要先对它进行 URL 编码。我也做了同样的事情,它奏效了。

于 2015-05-10T14:21:51.907 回答