英文:
Putting back messages on AWS SQS, for messages processed by Lambda but failed internally
问题
我对SQS Lambda集成还比较新,并且正在尝试理解适用于我的用例的最佳方法。
我有一个SQS Lambda集成。我的Lambda函数正在从SQS接收消息,并调用外部API来处理这些消息。这些消息将逐条被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;
}
我正在寻找一种方法来捕获外部API调用中的任何错误响应,并将响应失败(非200状态)的消息重新放回队列。由于这些API调用失败的消息被视为“已处理”,它们目前并未返回到队列中。在我的当前设置中是否有实现这一点的方法?这样做是否被推荐?
该SQS还有一个死信队列。
英文:
I am fairly new to the SQS Lambda integration and trying to understand the best approach for my use-case.
I have an SQS Lambda integration. My Lambda function is receiving messages from SQS and calls an external API to process the messages. The messages will be received one at a time by Lambda function, with batch size set to 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. These API-failed messages are not currently going back to the queue as they are considered "PROCESSED". Is there a way to do it with my current setup? Is it recommended?
The SQS also has a Dead Letter Queue.
答案1
得分: 3
如果您修改函数代码,在API调用失败时抛出“异常”,Lambda/SQS集成将将其视为失败,并将消息放回队列。
如果要在将消息发送到DLQ之前多次重试消息,还需要将“最大接收次数”配置为大于1的某个数字。
英文:
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.
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.
答案2
得分: 0
如果外部 API 调用失败,则抛出异常。
英文:
Throw Exception if the external API call is failed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论