I'm using Spring Boot 2.3.5.RELEASE and I noticed that @Async methods in my application are working without adding @EnableAsync in any configuration class.
After some digging, I saw a comment from Marten Deinum suggesting that adding spring-boot-starter-actuator can enable @Async and scheduling by default.
Does spring-boot-starter-actuator implicitly enable async support?
When I add @EnableAsync and run, I get a BeanCreationException.
@Configuration
public class AsyncConfig implements WebMvcConfigurer {
@Bean(name = "taskExecutor")
public AsyncTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(50);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("Async-");
executor.initialize();
return executor;
}
@Override
public void configureAsyncSupport(AsyncSupportConfigurer
configurer) {
configurer.setTaskExecutor(taskExecutor());
}
}
@Service
public class TaskExecutorService {
@Async("taskExecutor")
public void sendEmailAndNotification(Candidate candidate) {
// Your logic to send email and notification
}
}
this code work without error,
but if I add @EnableAsync I get this,
WebMvcConfigurer, that is async support for the controllers and has nothing to do with@Async. You should use theAsyncConfigurerinstead.@EnableAsync? That error comes when I run with this annotation.