用于无效输出的 AWS Lambda RequestHandler

AWS Lambda RequestHandler for a void output

考虑用 Java:

编写的简单 Lambda
import com.amazonaws.services.lambda.runtime.Context; 
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class Hello implements RequestHandler<Integer, String>{
    public String handleRequest(int myCount, Context context) {
        return String.valueOf(myCount);
    }
}

处理程序接口定义为 RequestHandler<InputType, OutputType>,但是当我的 Lambda 对事件做出反应并且只是产生一些副作用时,输出类型是否不必要,我必须这样写:

public class Hello implements RequestHandler<SNSEvent, Void>{
    public Void handleRequest(SNSEvent snsEvent, Context context) {
        ...
        return null;
    }
}

这很烦人。

void 处理程序是否有替代 RequestHandler 的方法?:

public class Hello implements EventHandler<SNSEvent>{
    public void handleEvent(SNSEvent snsEvent, Context context) {
        ...
    }
}

您不需要为 Lambda 入口点实现接口。您的处理程序 class 可以只是一个 POJO,其签名满足 explained in the documentation.

的要求

例如:

package example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.SNSEvent;

public class Hello {
  public void handleEvent(SNSEvent event, Context context) {
    // Process the event
  }
}

在这种情况下,您应该使用 example.Hello::handleEvent 作为 处理程序 配置。

另见 this example from the official docs:

package example;

import com.amazonaws.services.lambda.runtime.Context; 
import com.amazonaws.services.lambda.runtime.LambdaLogger;

public class Hello {
    public String myHandler(int myCount, Context context) {
        LambdaLogger logger = context.getLogger();
        logger.log("received : " + myCount);
        return String.valueOf(myCount);
    }
}