Spring 4.3和Java 8 配置了弹簧
@Repository
public class EmployeeRepostiry {
@Async
public CompleteableFuture<StatusVO> getEmployee(String id) {
StatusVo status = new StatusVO();
return CompletableFuture.completedFuture(status);
}
}
CallingService cclass:
@Service
public class EmployeeService {
@Autowired public EmployeeRepostiry employeeReporsitory;
public List<StatusVO> getEmployee(List<String> ids) {
List<StatusVo> returnStatus = new ArrayList<StatusVO>();
List<CompleteableFuture<StatusVO>> statsuss = new ArrayList<CompleteableFuture<StatusVO>>();
for(String id: ids) {
statsuss.add(employeeReporsitory.getEmployee(id));
}
try {
CompletableFuture.allOf(statsuss.toArray(new CompletableFuture[statsuss.size()])).join();
} catch(Exception e){
e.printstacktracce();
}
Logger.debug("not coming to this line"); //Not coming here
returnStatus = statsuss.stream().map(completableStatus -> {
ServiceActionStatusVO statusVO = null;
try {
statusVO = completableStatus.get();
} catch (InterruptedException | ExecutionException e) {
statusVO = new StatusVO();
statusVO.setStatus(Constants.ERROR);
statusVO.setStatusText(e.getMessage());
LOGGER.error("Failed on the state of execution ",e);
}
return statusVO;
}).collect(Collectors.toList());
return returnStatus ;
}
}
async正在成功执行,并且还在异步线程中运行,并且所有ID都已完成。但声明不是在join();
之后发表的
它在连接后挂起并且所有异步任务都被创建,我试图捕获一个通用异常并且没有返回