Java异步方法-未来无法按预期工作
我正在尝试创建一个异步执行DB操作的Api App.java:Java异步方法-未来无法按预期工作,java,spring,asynchronous,Java,Spring,Asynchronous,我正在尝试创建一个异步执行DB操作的Api App.java: @SpringBootApplication @EnableAsync public class App { public static void main(String[] args) { SpringApplication.run(PhonenumberApplication.class, args); } } @PostMapping(/insert) public Integer hel
@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;
}
控制器:
@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;
}
服务:
@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;
}
@Async
公共整数calcAndinsertDbrecords(字符串输入){
List finalList=性能操作(输入);
//对输入字符串执行一些java操作
//结果,我得到了不同字符串的列表
插入记录(finalList);
返回finalList.size();
}
公开未来插入记录(列表finalList){
//DB调用以在DB中插入字符串列表
//此方法需要时间来插入
repository.saveAll(finalList);
返回null;
}
据我所知,大小已经存在,我想异步执行db操作并返回大小,但是
/insert
api始终返回null,即使值不是null。final
是一个关键字,所以我会首先尝试重命名该参数/变量很抱歉,如果它是同步的,那么它是一个输入错误,您如何或为什么期望值可用?