@Async
method in @Service
annotated class in standalone Spring Boot application doesn\'t run asynchronously. What am I doing wrong?
When I
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.
Calling async method from within the same class would trigger the original method and not the intercepted one. You need to create another service with the async method, and call it from your service.
Spring creates a proxy for each service and component you create using the common annotations. Only those proxies contain the wanted behavior defined by the method annotations such as the Async. So, calling those method not via the proxy but by the original naked class would not trigger those behaviors.