I developed a simple asynchronous API in Scala using Spring MVC. In the Controller I am using DeferredResult<Map<String,Integer>> response = new DeferredResult<>(360000L,new String("PROCESSING_TIMEOUT"));
in order to return the message PROCESSING_TIMEOUT
in case of excessivly long processing time.
Then, in order to test this API, I send 100 simultaneous requests. The problem is that after a successful processing of around 50 first requests, the program gets blocked and waits 360000 milliseconds, then returns the message PROCESSING_TIMEOUT
, and continues processing the rest of requests. Then it gets blocked again and unblocks in 360000 milliseconds. I don't understand such behaviour. Why does it happen? And how to resolve it? I tried to play with the core pool size and maximum pool size parameters, but it didn't solve the problem.
@Configuration
@SpringBootApplication
@EnableAsync
public class AppConfig implements AsyncConfigurer {
@Bean
public javax.validation.Validator localValidatorFactoryBean() {
return new LocalValidatorFactoryBean();
}
@Override
public Executor getAsyncExecutor() {
//return new SimpleAsyncTaskExecutor();
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(1000);
executor.setThreadNamePrefix("MyExecutor-");
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new MyUncaughtExceptionHandler();
}
public static void main(String[] args) {
SpringApplication.run(AppConfig.class, args);
}
}