调用Lambda函数处理程序Java

时间:2019-10-25 19:13:25

标签: aws-lambda payload

我有一个lambda函数,该函数具有一个处理程序,该处理程序又具有多个路由器。每个路由器都对应一个API。

我已经用Java创建了一个lambda客户端,需要调用这些API。要调用这些API,我需要调用处理程序,并将有效负载与其一起传递给客户端。你们能帮我调用句柄和传递有效载荷的语法吗?

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,那么我首先创建了一个Lambda,看起来像:

public class SampleHandler implements RequestStreamHandler {
    private static final Logger logger = LogManager.getLogger(SampleHandler.class);

    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        logger.info("handlingRequest");
        LambdaLogger lambdaLogger = context.getLogger();

        ObjectMapper objectMapper = new ObjectMapper();
        String inputString = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
        JsonNode jsonNode = objectMapper.readTree(inputString);

        String route = jsonNode.get("route").asText();

        RouterResult routerResult = new RouterResult();

        switch( route ) {
            case "requestTypeA":
                RequestTypeA requestTypeA = objectMapper.readValue(inputString, RequestTypeA.class);
                routerResult.setResult(handleRequestTypeA(requestTypeA));
                break;

            case "requestTypeB":
                RequestTypeB requestTypeB = objectMapper.readValue(inputString, RequestTypeB.class);
                routerResult.setResult(handleRequestTypeB(requestTypeB));
                break;

            default:
                logger.error( "don't know how to handle route of type \"" + route + "\n" );
                routerResult.setResult("error");
        }

        outputStream.write(objectMapper.writeValueAsString(routerResult).getBytes(StandardCharsets.UTF_8));

       logger.info("done with run, remaining time in ms is " + context.getRemainingTimeInMillis() );
    }


    private String handleRequestTypeA(RequestTypeA requestTypeA) {
        logger.info("handling requestTypeA, requestTypeA.requestA is " + requestTypeA.getRequestA() );

        return "handled requestTypeA";
    }

    private String handleRequestTypeB(RequestTypeB requestTypeB) {
        logger.info("handling requestTypeB, requestTypeB.requestB is " + requestTypeB.getRequestB() );

         return "handled requestTypeB";
    }
}

RouterRequest.java

public class RouterRequest {
    protected String route;

    public String getRoute() {
        return route;
    }
}

RequestTypeA.java

public class RequestTypeA extends RouterRequest {
    private String requestA;

    public RequestTypeA() {
        route = "requestTypeA";
    }

    public String getRequestA() {
        return requestA;
    }

    public void setRequestA(String requestA) {
        this.requestA = requestA;
    }
}

RequestTypeB.java

public class RequestTypeB extends RouterRequest {
    private String requestB;

    public RequestTypeB() {
        route = "requestTypeB";
    }

    public String getRequestB() {
        return requestB;
    }

    public void setRequestB(String requestB) {
        this.requestB = requestB;
    }
}

还有一个结果类RouterResult.java

public class RouterResult {
    private String result;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    @Override
    public String toString() {
        return "RouterResult{" +
                "result='" + result + '\'' +
                '}';
    }
}

然后,要调用此Lambda,您将需要具有lambda:InvokeFunction权限的角色。调用的代码如下:

public class RouterRunner {
    private static final String AWS_ACCESS_KEY_ID = "<access key>";
    private static final String AWS_SECRET_ACCESS_KEY = "<access secret>";

    public static void main( String[] argv ) throws IOException {
        AWSCredentials credentials = new BasicAWSCredentials( AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY );
        AWSLambda lambda = AWSLambdaClientBuilder.standard()
                .withRegion(Regions.US_WEST_2)
                .withCredentials(new AWSStaticCredentialsProvider(credentials)).build();

        RequestTypeA requestTypeA = new RequestTypeA();
        requestTypeA.setRequestA("set from the runner, request type A");

        ObjectMapper objectMapper = new ObjectMapper();

        InvokeRequest invokeRequest = new InvokeRequest()
                .withFunctionName("lambda-router")
                .withPayload(objectMapper.writeValueAsString(requestTypeA));

        invokeRequest.setInvocationType(InvocationType.RequestResponse);

        InvokeResult invokeResult = lambda.invoke(invokeRequest);

        String resultJSON = new String(invokeResult.getPayload().array(), StandardCharsets.UTF_8);

        System.out.println( "result from lambda is " + resultJSON );

        RouterResult routerResult = objectMapper.readValue(resultJSON, RouterResult.class);

        System.out.println( "result.toString is " + routerResult.toString() );


        RequestTypeB requestTypeB = new RequestTypeB();
        requestTypeB.setRequestB("set from the runner, request type B");

        invokeRequest = new InvokeRequest()
                .withFunctionName("lambda-router")
                .withPayload(objectMapper.writeValueAsString(requestTypeB));

        invokeRequest.setInvocationType(InvocationType.RequestResponse);

        invokeResult = lambda.invoke(invokeRequest);

        resultJSON = new String(invokeResult.getPayload().array(), StandardCharsets.UTF_8);

        System.out.println( "result from lambda is " + resultJSON );

        routerResult = objectMapper.readValue(resultJSON, RouterResult.class);

        System.out.println( "result.toString is " + routerResult.toString() );
    }
}

可能需要对错误处理进行一些改进,并且我相信您可以使它更有效。但这就是整体想法。最终,在Lambda端,我将InputStream转换为String并根据请求类型中的公共字段将该String转换为某种对象。在客户端,我将对象转换为JSON,将其发送出去,然后将结果从JSON字符串转换回结果对象。