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();
}