@Async in Spring doesn't work in Service class?

前端 未结 2 382
礼貌的吻别
礼貌的吻别 2021-01-20 07:36

@Async method in @Service annotated class in standalone Spring Boot application doesn\'t run asynchronously. What am I doing wrong?

When I

2条回答
  •  离开以前
    2021-01-20 08:01

    Workaround is:

    @EnableAsync
    @Service("ip-service")
    public class ImageProcessorService implements IIMageProcessorService {
        
        @Autowired
        @Qualifier("ip-service")
        ImageProcessorService ipService;
    
        public void downloadAllImages(Run lastRun) {
            // this method calls downloadAnSave() in loop and should run asynchronously....
            ipService.downloadAnSave(productId, imageUrl);
        }
    
        @Async
        @Override
        public boolean downloadAnSave(String productId, String imageUrl) {
            //
        }
    }
    

    With such approach you call method of proxy, not the class instance. The same approach can be used with other tools working with proxies, e.g. @Transactional etc.

提交回复
热议问题