【发布时间】:2017-02-23 20:19:43
【问题描述】:
独立 Spring Boot 应用程序中@Service 注释类中的@Async 方法不会异步运行。我做错了什么?
当我直接从主类(@SpringBootApplication 注释)运行相同的方法时,它可以工作。示例:
主类
@SpringBootApplication
@EnableAsync
public class Application implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// here when I call downloadAnSave() it runs asynchronously...
// but when I call downloadAnSave() via downloadAllImages() it does not run asynchronously...
}
}
和我的服务类(这里异步行为不起作用):
@EnableAsync
@Service
public class ImageProcessorService implements IIMageProcessorService {
public void downloadAllImages(Run lastRun) {
// this method calls downloadAnSave() in loop and should run asynchronously....
}
@Async
@Override
public boolean downloadAnSave(String productId, String imageUrl) {
//
}
}
【问题讨论】:
-
问题本身似乎并不完全重复,但相同的答案(和 cmets)适用于此。
标签: java asynchronous spring-boot