Example usage for com.amazonaws.services.lambda AWSLambdaAsync invokeAsync

List of usage examples for com.amazonaws.services.lambda AWSLambdaAsync invokeAsync

Introduction

In this page you can find the example usage for com.amazonaws.services.lambda AWSLambdaAsync invokeAsync.

Prototype

java.util.concurrent.Future<InvokeResult> invokeAsync(InvokeRequest invokeRequest);

Source Link

Document

Invokes a Lambda function.

Usage

From source file:example.lambda.InvokeLambdaFunctionAsync.java

License:Apache License

public static void main(String[] args) {
    String function_name = "HelloFunction";
    String function_input = "{\"who\":\"AWS SDK for Java\"}";

    AWSLambdaAsync lambda = AWSLambdaAsyncClientBuilder.defaultClient();
    InvokeRequest req = new InvokeRequest().withFunctionName(function_name)
            .withPayload(ByteBuffer.wrap(function_input.getBytes()));

    Future<InvokeResult> future_res = lambda.invokeAsync(req);

    System.out.print("Waiting for future");
    while (future_res.isDone() == false) {
        System.out.print(".");
        try {/*from   w w  w .  j a v  a  2s  . c  o m*/
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.err.println("\nThread.sleep() was interrupted!");
            System.exit(1);
        }
    }

    try {
        InvokeResult res = future_res.get();
        if (res.getStatusCode() == 200) {
            System.out.println("\nLambda function returned:");
            ByteBuffer response_payload = res.getPayload();
            System.out.println(new String(response_payload.array()));
        } else {
            System.out.format("Received a non-OK response from AWS: %d\n", res.getStatusCode());
        }
    } catch (InterruptedException | ExecutionException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }

    System.exit(0);
}