I have a spring boot application where a users hits an endpoint and I have to acknowledge that I got their request immediately. I need to do some computation on different thread and send them response on a different endpoint after my computation is over. For executing the task on different thread my thread pool configuration looks something like this:
@Configuration
@EnableAsync
public class SpringAsyncMatchingConfig {
@Bean(name = "threadTaskExecutor")
public TaskExecutor getMatchingTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(10);
threadPoolTaskExecutor.setQueueCapacity(0);
threadPoolTaskExecutor.setMaxPoolSize(15);
return threadPoolTaskExecutor;
}
}
While I do the computation I need to hit one of the endpoint which returns me a token id and does some computation. It usually takes 3 to 4 minutes to do the computation. So what I have done is I have mentioned Thread.sleep(30000)
and after 30 seconds is completed I again hit the same api with the token id it provided expecting it to give me a result.
while(result == false) {
Thread.sleep(30000)
result = callendpoint(tokenid)
}
Suppose my thread pool is exhausted, it reached its maximum size of 15 threads, and some more tasks are provided to the pool, some of my threads will be in 30 seconds sleep state, will those threads be terminated and assigned a new task because I am in sleep (idle) state? Should I add the set keep alive to prevent this from happening?
threadPoolTaskExecutor.setKeepAliveSeconds(120);
Is this the right thing to do?