I am testing out @Async in a spring boot 2, and i followed some online tutorial
My Config Class:
@Configuration
@EnableAsync
public class AsyncConfig {
@Bean
public Executor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(5);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("Async Process-");
executor.initialize();
return executor;
}
}
Snippet of My Controller:
@GetMapping("/test/async")
public void testAsync() {
System.err.println("Thread in controller: " + Thread.currentThread().getName());
TestAsyncClazz clazz = new TestAsyncClazz();
clazz.testAsyncMethod();
}
My TestAsyncClass:
public class TestAsyncClazz {
@Async
public void testAsyncMethod(){
System.err.println("Running async: "+ Thread.currentThread().getName());
}
}
When i check the print line, it shows that both of my method running on the same thread, and it didn't use the threadNamePrefix Async Process-:
Thread in controller: http-nio-8080-exec-2
Running async: http-nio-8080-exec-2
What i did wrong? Did i misunderstand something?