I need a POJO method to execute asynchronously, so I've annotated it with @Async
. I've added @EnableAsync
to my @Configuration
class with the proper @ComponentScan
. Here's a small test case for you to run.
public class Test {
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(MyConfig.class);
context.refresh();
Object o = context.getBean(AsyncBean.class);
//((AsyncBean)o).doStuff();
System.out.println(o);
}
@ComponentScan(basePackages = "my.package")
@EnableAsync
@Configuration
// @EnableScheduling
public static class MyConfig {
@Bean
public AsyncBean bean() throws InterruptedException {
AsyncBean b = new AsyncBean();
return b;
}
}
public static class AsyncBean {
//@Scheduled(fixedRate = 10000L, initialDelay = 1000L)
@Async
public void doStuff() throws InterruptedException {
for (int i = 0; i < 5; i++) {
System.out.println("async loop" + i + " -> " + Thread.currentThread().getId());
Thread.sleep(1000L);
}
}
}
}
The code above will load the AnnotationConfigApplicationContext
and quit. If, however, I un-comment //((AsyncBean)o).doStuff();
, then that will run in a separate thread. Why is it that the @Async
method doesn't get started when the configuration is completely read? That's what I would expect.
I've left some @Scheduled
stuff above so you can try it yourself. In the case of @Scheduled
, the annotated method gets triggered right away (after initial delay that is).
Is there something else I need to implement for Spring to know it has to start my @Async
methods?