

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 对 Amazon SQS ReceiveMessage API 调用未返回的消息进行故障排除
<a name="troubleshooting-messages-not-returned-ReceiveMessage"></a>

以下主题介绍了 Amazon SQS 消息可能无法返回给使用者的最常见原因以及如何进行问题排查。有关更多信息，请参阅《Amazon Knowledge Center 指南》**中的[“为什么我无法从我的 Amazon SQS 队列接收消息？”](https://repost.aws/knowledge-center/sqs-queue-message)。

## 空队列
<a name="troubleshooting-empty-queue"></a>

要确定队列是否为空，请使用长轮询来调用 [https://docs.amazonaws.cn/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html](https://docs.amazonaws.cn/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html) API。您也可以使用`ApproximateNumberOfMessagesVisible``ApproximateNumberOfMessagesNotVisible`、和`ApproximateNumberOfMessagesDelayed` CloudWatch 指标。如果所有指标的值在几分钟内均为 0，则判断队列为空。

## 已达到传输中消息数量限制
<a name="troubleshooting-in-flight-limit-reached"></a>

如果您使用[长轮询](sqs-short-and-long-polling.md#sqs-long-polling)，并且消息数量已超出队列的传输中消息数量上限（默认值为 120000），Amazon SQS 不会返回提示[超出配额限制](sqs-quotas.md)的错误消息。

## 消息延迟
<a name="troubleshooting-message-delayed"></a>

如果 Amazon SQS 队列配置为[延迟队列](sqs-delay-queues.md)，或者消息通过[消息计时器](sqs-message-timers.md)发送，则在延迟时间结束之前消息不可见。要验证队列是否配置为延迟队列，请使用 [https://docs.amazonaws.cn/AWSSimpleQueueService/latest/APIReference/API_GetQueueAttributes.html](https://docs.amazonaws.cn/AWSSimpleQueueService/latest/APIReference/API_GetQueueAttributes.html) API `DelaySeconds` 属性，或者从队列控制台的**交付延迟**下方查看相关信息。检查[`ApproximateNumberOfMessagesDelayed`](sqs-available-cloudwatch-metrics.md) CloudWatch 指标以了解是否有任何消息延迟。

## 消息正在传输中
<a name="troubleshooting-message-in-flight"></a>

如果其他使用者对消息进行了轮询，则该消息将在[可见性超时](sqs-visibility-timeout.md)期间内处于传输中或不可见状态。额外的轮询可能返回空结果（即没有可接收的消息）。检查[ApproximateNumberOfMessagesVisible](sqs-available-cloudwatch-metrics.md) CloudWatch指标以了解可接收的消息数量。对于 FIFO 队列，如果具有消息组 ID 的消息正在传输中，除非您删除该消息或该消息变为可见，否则系统不会再返回消息。这是因为[消息顺序](sqs-fifo-queues.md)是在 FIFO 队列中的消息组级别进行维护的。

## 轮询方法
<a name="troubleshooting-polling-method"></a>

如果您使用的是[短轮询](sqs-short-and-long-polling.md#sqs-short-polling)，则（[WaitTimeSeconds](https://docs.amazonaws.cn/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html#API_ReceiveMessage_RequestSyntax)为 0）Amazon SQS 会对其服务器的子集进行采样，并仅返回来自这些服务器的消息。因此，即使有可接收的消息，您也可能无法接收到它们。后续的轮询请求将会返回消息。

如果您使用[长轮询](sqs-short-and-long-polling.md#sqs-long-polling)，Amazon SQS 会轮询所有服务器，并在收集至少一条可用消息后，最多返回您指定的最大数量的消息。如果的值 ReceiveMessage [WaitTimeSeconds](https://docs.amazonaws.cn/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html#API_ReceiveMessage_RequestSyntax)太低，则可能无法收到所有可用消息。