I defined this scheduled task:
@EnableAsync
@Configuration
@EnableScheduling
public class DoWork {
@Async
@Scheduled(cron = "xxxxxxxx")
public void foo(){
}
}
I am getting warning in IDE on @Async
:
Remove this “@Async” annotation from this method.
I need to execute this method in background thread, every x minutes.
I don’t need pool of threads, only 1 additional thread for this task.
Expecting not to see the warning.
2
You don’t need @Async there. By default, methods annotated with @Scheduled in Spring are not executed in the main thread but in a thread managed by Spring’s task scheduler, typically using a single-threaded task executor – you can verify it by logging the thread’s name using ‘Thread.currentThread().getName()’. Also, I think it’s cleaner to use @Component instead of @Configuration since this is not a configuration class.
Also if you wanna configure a threadpool with a single thread for it, you can use ThreadPoolTaskScheduler.
2