Spring 4.3 和 Java 8 都配置了 spring
@Repository
public class EmployeeRepostiry {
@Async
public CompleteableFuture<StatusVO> getEmployee(String id) {
StatusVo status = new StatusVO();
return CompletableFuture.completedFuture(status);
}
}
调用服务 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 ;
}
}
异步正在成功执行,并且也在异步线程中运行,并且所有 id 都已完成。但是该语句join();
在加入后挂起并且所有异步任务都被创建后没有出现,我试图捕获一个通用异常并且没有返回任何内容