AWS Lambda不会从队列中删除消息 [英] AWS Lambda not removing messages from the queue

查看:91
本文介绍了AWS Lambda不会从队列中删除消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从SQS事件中触发Lambda函数:

I am triggering a Lambda function from an SQS event with the following code:

@Override
public Void handleRequest(SQSEvent sqsEvent, Context context) {
    for (SQSMessage sqsMessage : sqsEvent.getRecords()) {
        final String body = sqsMessage.getBody();
        try {
            //do stuff here
        } catch (Exception ex) {
            //send to DLQ
        }
    }
    return null;
}

做事"正在使用以下代码调用另一个Lambda函数:

The "do stuff" is calling another Lambda function with the following code:

private final AWSLambda client;
    private final String functionName;

    public LambdaService(AWSLambdaAsync client, String functionName) {
        this.client = client;
        this.functionName = functionName;
    }

    public void runWithPayload(String payload) {
        logger.info("Invoking lambda {} with payload {}", functionName, payload);
        final InvokeRequest request = new InvokeRequest();
        request.withFunctionName(functionName).withPayload(payload);
        final InvokeResult invokeResult = client.invoke(request);
        final Integer statusCode = invokeResult.getStatusCode();
        logger.info("Invoked lambda {} with payload {}. Got status code {} and response payload {}",
                functionName,
                payload,
                statusCode,
                StandardCharsets.UTF_8.decode(invokeResult.getPayload()).toString());
        if(statusCode.equals(200) == false) {
            throw new IllegalStateException(String.format("There was an error executing the lambda function %s with payload %s", functionName, payload));
        }
    }

我正在使用以下库:

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-lambda-java-core</artifactId>
  <version>1.2.0</version>
</dependency>
<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-lambda-java-events</artifactId>
  <version>2.2.6</version>
</dependency>
<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk-sqs</artifactId>
  <version>1.11.505</version>
</dependency>
<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk-lambda</artifactId>
  <version>1.11.505</version>
</dependency>

问题在于,似乎SQS消息并未从队列中删除,并且一遍又一遍地对其进行了重新处理.它每30秒发生一次,这恰好是 Default Visibility Timeout 的值.现在,据我所知,如果使用sqs消息的lambda正确终止,它应该自动从队列中删除该消息,但这没有发生.我不认为lambda中发生任何错误,因为我没有在DLQ中收到任何消息(并且我有一个包罗万象的块),并且在Cloudwatch的日志中看不到任何stacktrace.我对这里发生的事情感到困惑,有人知道吗?

The problem is that it looks like the SQS message is not removed from the queue and it gets reprocessed over and over. It happens every 30 seconds which is exactly the value of Default Visibility Timeout. Now, as far as I know, if the lambda consuming the sqs messages is terminating properly it should automatically delete the message from the queue, but this is not happening. I don't think there is any error happening in the lambda because I am not getting any message in the DLQ (and I have a catch-all block) and I cannot see any stacktrace in the logs in Cloudwatch. I am bit confused about what's happening here, anyone has some good idea?

推荐答案

除非最近有所更改,否则我认为适用于Java的AWS开发工具包不会自动从队列中删除消息.您需要编写代码来做到这一点.

Unless something changed recently, I don't think the AWS SDK for Java automatically deletes the message from the queue. You need to write the code to do that.

我很想被证明是错误的,请分享我错过的文档摘录.

I would love to be proven wrong on that one, please share the doc excerpt I missed.

代码示例:

这篇关于AWS Lambda不会从队列中删除消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆