简体   繁体   English

将消息放回 AWS SQS,用于由 Lambda 处理但在内部失败的消息

[英]Putting back messages on AWS SQS, for messages processed by Lambda but failed internally

I am fairly new to the SQS Lambda integration and trying to understand the best approach for my use-case.我对 SQS Lambda 集成相当陌生,并试图了解我的用例的最佳方法。

I have an SQS Lambda integration.我有一个 SQS Lambda 集成。 My Lambda function is receiving messages from SQS and calls an external API to process the messages.我的 Lambda 函数正在接收来自 SQS 的消息并调用外部 API 来处理消息。 The messages will be received one at a time by Lambda function, with batch size set to 1. Lambda 函数将一次接收一条消息,批量大小设置为 1。

    @Override
    public Void handleRequest(SQSEvent event, Context context) {
        Response response = null;
        if (event != null) {
            for (SQSMessage msg : event.getRecords()) {
                response = callXXXAPI(msg.getBody(), reqId, logger);
            }
        }
            return null;
    }

I'm looking for a way to capture any error responses from the external API call and put back the messages which had failed response (non-200 status) on to the queue.我正在寻找一种方法来捕获来自外部 API 调用的任何错误响应,并将响应失败(非 200 状态)的消息放回到队列中。 These API-failed messages are not currently going back to the queue as they are considered "PROCESSED".这些 API 失败的消息当前不会返回队列,因为它们被视为“已处理”。 Is there a way to do it with my current setup?有没有办法用我当前的设置来做到这一点? Is it recommended?是否推荐?

The SQS also has a Dead Letter Queue. SQS 还有一个死信队列。

If you modify your function code to throw an Exception when the API call fails, the Lambda/SQS integration will treat that as a failure and place the message back in the queue.如果您修改函数代码以在 API 调用失败时抛出Exception ,Lambda/SQS 集成会​​将其视为失败并将消息放回队列中。

You will also need to configure Maximum receives to some number greater than 1 if you want the messages to be retried multiple times before being sent to the DLQ.如果您希望在将消息发送到 DLQ 之前多次重试消息,您还需要将Maximum receives数配置为大于1某个数字。

如果外部 API 调用失败,则抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM