AWS Lambda RequestHandler用于无效输出

时间:2019-03-19 10:30:45

标签: java amazon-web-services aws-lambda aws-sdk-java-2.0

考虑一个用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;
    }
}

这很烦人。

RequestHandler处理程序是否可以替代void?:

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

1 个答案:

答案 0 :(得分:2)

您不需要为Lambda入口点实现接口。您的处理程序类可以只是一个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作为 handler 配置。

另请参阅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);
    }
}