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

huangapple go评论72阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月20日 00:00:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63970718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定