I am using Spring boot with @EnableScheduling
and @EnableAsync
.
I have a method which is annotated with @Scheduled
.
I have a few more methods, which are annotated with @Async
.
Now I am calling these @Async
methods in the @Scheduled
method and printing out the name of the current thread in the async methods. What I see is they all have same thread name, which in fact is the thread that is running the @Scheduled
method.
I don't see asynchronous method execution. What is wrong here?
Here is my application boot class
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class ApplicationBoot {
public static void main(String[] args) {
SpringApplication.run(ApplicationBoot.class, args);
}
}
Here is my scheduler class
@Component
public class TaskScheduler {
private static final Logger logger = Logger.getLogger(TaskScheduler.class);
@Scheduled(fixedDelay = 10000)
public void ScheduledMethod() {
methodOne();
methodTwo();
methodThree();
}
@Async
private void methodOne() {
logger.info("Method one called by Thread : " + Thread.currentThread().getName() + " at " + new Date());
}
@Async
private void methodTwo() {
logger.info("Method two called by Thread : " + Thread.currentThread().getName() + " at " + new Date());
}
@Async
private void methodThree() {
logger.info("Method three called by Thread : " + Thread.currentThread().getName() + " at " + new Date());
}
}
Output
Method one called by Thread : pool-1-thread-1 at Tue Apr 04 16:32:27 IST 2017
Method two called by Thread : pool-1-thread-1 at Tue Apr 04 16:32:27 IST 2017
Method three called by Thread : pool-1-thread-1 at Tue Apr 04 16:32:27 IST 2017