简体   繁体   中英

Java Async method - Future not working as expected

I'm trying to create an Api which does the DB operation asynchronously.

App.java:

@SpringBootApplication
@EnableAsync
public class App {

    public static void main(String[] args) {
        SpringApplication.run(PhonenumberApplication.class, args);
    }

}

Controller:

@PostMapping(/insert)
public Integer hello() {
    return service.calcAndinsertDbrecords("Hello");
}

Service:

@Async
public Integer calcAndinsertDbrecords(String input) {

    List<String> finalList = performOperation(input);

    // Perform some java operation on input string
    // As a result i get list of different string

    insertRecords(finalList);

    return finalList.size();
}

public Future<String> insertRecords(List<String> finalList){
    // DB call to insert the list of strings in DB
    // this method takes time to insert
    repository.saveAll(finalList);
    return null;
}

As i know the size already I want to perform the db operation asynchronously and return size but the /insert api always returns null even though the value is not null.

final is a keyword so I would first try renaming that parameter/variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM