Java Asyn c метод - Будущее не работает, как ожидалось - PullRequest
0 голосов
/ 28 апреля 2020

Я пытаюсь создать API, который выполняет операции с БД асинхронно.

Приложение. java:

@SpringBootApplication
@EnableAsync
public class App {

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

}

Контроллер:

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

Служба:

@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;
}

Поскольку я уже знаю размер, я хочу выполнить операцию БД асинхронно и вернуть размер, но /insert api всегда возвращает ноль , даже если значение не равно нулю.

1 Ответ

0 голосов
/ 28 апреля 2020

final - это ключевое слово, поэтому я сначала попробую переименовать этот параметр / переменную

...