1

I am using software.amazon.awssdk version 2.18.21 to invoke Lambda function from spring boot application. While invoking Lambda function which takes approx 2-3 minutes to finish throws Http status 504: Gateway Time-out Exception. I have been suggested to use Asynchronous Call to invoke Lambda function and then read the response. How can i convert this existing code to asynchronous call and get response to verify if it was success or error?

//imports from 
import software.amazon.awssdk.http.ApacheHttpClient;
import software.amazon.awssdk.services.lambda.LambdaClient;
import software.amazon.awssdk.services.lambda.model.InvokeRequest;
import software.amazon.awssdk.services.lambda.model.InvokeResponse;

//Calling Lambda Function
try{
  LambdaClient client = LambdaClient.builder().httpClientBuilder(ApacheHttpClient.builder()
                      .maxConnections(100)
                      .socketTimeout(Duration.ofSeconds(60))
                      .connectionTimeout(Duration.ofSeconds(60))
                    ).build();
  InvokeRequest req = InvokeRequest.builder().functionName("abc").build();
  InvokeResponse res = client.invoke(req);
  String respnonse = res.payload().asUtf8String();
  System.out.println(response);
}catch(Exception e){
  e.printStackTrace();
}

Edited: Tried below but unable to implement CompletableFuture. Can you suggest how to implement it for Lambda function and get response

try{
SdkAsyncHttpClient client = NettyNioAsyncHttpClient.builder().readTimeout(Duration.ofSeconds(60)).connectionTimeout(Duration.ofSeconds(60)).build();

  LambdaAsyncClient lambdaClient = LambdaAsyncClient .builder().httpClient(client).build();
  InvokeRequest req = InvokeRequest.builder().functionName("abc").invocationType("EVENT").build();
CompletableFuture<InvokeResponse> request = lambdaClient.invoke(req);
  //InvokeResponse res = client.invoke(req);
  //String respnonse = res.payload().asUtf8String();
  //System.out.println(response);
}catch(Exception e){
  e.printStackTrace();
}

1 Answer 1

0

To perform Lambda operations using Asynchronous calls with the Java V2 API, use this:

LambdaAsyncClient

The call the invoke method:

https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/lambda/LambdaAsyncClient.html#invoke(software.amazon.awssdk.services.lambda.model.InvokeRequest)

The patten to use Async methods are similar no matter what the service use. For example, most calls involve using CompletableFuture. Then you code the logic you want within the whenComplete code block.

To learn about the patterns, read this part of the Java V2 DEV Guide:

Asynchronous programming

For example, this code example is from that topic.

public class DynamoDBAsyncListTables {

    public static void main(String[] args) throws InterruptedException {

        // Create the DynamoDbAsyncClient object
        Region region = Region.US_EAST_1;
        DynamoDbAsyncClient client = DynamoDbAsyncClient.builder()
                .region(region)
                .build();

        listTables(client);
    }

    public static void listTables(DynamoDbAsyncClient client) {

        CompletableFuture<ListTablesResponse> response = client.listTables(ListTablesRequest.builder()
                .build());

        // Map the response to another CompletableFuture containing just the table names
        CompletableFuture<List<String>> tableNames = response.thenApply(ListTablesResponse::tableNames);

        // When future is complete (either successfully or in error) handle the response
        tableNames.whenComplete((tables, err) -> {
            try {
                if (tables != null) {
                    tables.forEach(System.out::println);
                } else {
                    // Handle error
                    err.printStackTrace();
                }
            } finally {
                // Lets the application shut down. Only close the client when you are completely done with it.
                client.close();
            }
        });
        tableNames.join();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.