I have an spring boot application with @async annotated methods, running on AWS lambda. This is the configuration I uses.
@Configuration
@EnableAsync
@Slf4j
public class AsyncConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(5);
executor.setQueueCapacity(50);
executor.setThreadNamePrefix("Async Tasks-");
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return (ex, method, params) -> {
log.error("Exception message in async task - {}", ex.getMessage());
log.error("Method name - {}", method.getName());
for (Object param : params) {
log.error("Parameter value - {}" , param);
}
};
}
}
I execute async tasks when listening from external call backs so that they can run asynchronously.
I can see the tasks are getting started but sometimes, they are not getting completed.
Is it because AWS lambda closes the instance, when a main thread completes? Can anyone explains what is happening?
Please explain is there anyway to fix this.
That's indeed what I expect.
The container is kept up and running when possible for future calls.
You have however no warranty it will remain up if the main thread is not active.
You can make the Lambda asynchronous and use a message oriented middleware like SQS to spawn multiple tasks.