Asynchronous method not running in parallel

ASd Dasd :

I have Service.class with start() and asychronous() method :

    public ResponseEntity<Object> start() throws APICommandConstructionException, APICommunicationException, APIReplyParseException,
            APIErrorResponse, IOException {

        List<Company> companiesList = dbHandler.retrieveCompaniesList();

        Company company = null;
        for (int i = 0; i < companiesList.size(); i++) {
            asychronousMethod(companiesList, i, company);
        }
        return new ResponseEntity<Object>("Start method has Finished", HttpStatus.OK);
    }

    @Async("threadPoolTaskExecutor")
    public void asychronousMethod(List<Company> companiesList, int i, Company company) throws APICommandConstructionException, APIReplyParseException, APICommunicationException, APIErrorResponse, IOException {
        company = companiesList.get(i);
        company = utils.websiteScrap(company);
        companiesRepository.save(company);
    }

Everything that is inside a loop doesn't run in parallel, but it starts second loop after first finished. Why is that? How to do it parallel?

Dewfy :

In brief: you shouldn't call explicitly methods with Spring annotations.

More detailed: Spring creates special proxies that on back-stage do 'magic' for you. So if you have async annotation, that means that (depending on compile- and runtime-configuration of Spring) there was some hidden part of code that is not executed when you invoke this.asynchronousMethod.

How to fix:

First of all method should match public CompletableFuture<Void> - such way you know when thread is complete.

Second instead of this you need resolve self-instance as Spring proxy. The simplest way over @Autowired:

@Autowired
MyClass zhis;

.... //in for loop:
future = zhis.asychronousMethod(companiesList, i, company);

P.s please see good example at https://spring.io/guides/gs/async-method/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400324&siteId=1