嗨,我该如何执行3个可调用函数,这样我就不必等待何时执行另一个进程,在某种程度上,我将不得不等待它们完成,然后才能正确地移动到我的下一行代码?我是这样做的:
我有一个返回值为可调用的函数:
public <T> Callable<List<T>> getDataFromOtherAPI(List<Long> ids, String url, Class<T> tClass){
WebClient webClient = WebClient.create();
//NOTE init mapper
ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//NOTE end of init mapper
CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, tClass);
List<T> object = webClient.post()
.uri(url).body(Mono.just(ids), Object.class)
.retrieve()
.onStatus(HttpStatus::isError, res -> res.bodyToMono(ExceptionResponse.class)
.onErrorResume(e -> Mono.error(new ResourceNotFound("Something Happened")))
.flatMap(errorBody -> Mono.error(new ResourceNotFound(errorBody.getMessage())))
)
.bodyToMono(new ParameterizedTypeReference<List<T>>() {})
.block();
List<T> mappedClass = mapper.convertValue(object, listType);
if(mappedClass.size() == 0) {
throw new ResourceNotFound("Data in Url " + url + " not found");
}
return () -> mappedClass;
}
我是这样宣布我的执行官的:
@Autowired
@Qualifier("executor")
private AsyncTaskExecutor execs;
下面是我如何尝试从函数中的另一个api调用:
Future<List<PartnerDto>> initPartners = execs.submit(getDataFromOtherAPI(
transDeliveryPlanningDtSoDtoPartnerIds, b2b.getPartnerSpecificId(), PartnerDto.class));
Future<List<PartnerShipmentDto>> initPartnerShipments = execs.submit(getDataFromOtherAPI(
transDeliveryPlanningDtSoDtoPartnerShipmentIds, b2b.getPartnerShipmentSpecificId(), PartnerShipmentDto.class));
Future<List<ProductResponseDto>> initProduct = execs.submit(getDataFromOtherAPI(
transDeliveryPlanningDtSoDetailDto, b2b.getProductSpecificId(), ProductResponseDto.class));
//another much process
//end of another much process
//i have to hold over here before i continue into my next process to do some for loop :
for(TransDeliveryPlanningDtSoDto e : d.getTransDeliveryPlanningDtSoDto()) {
PartnerDto latestPartner = new PartnerDto();
PartnerShipmentDto latestPartnerShipment = new PartnerShipmentDto();
TransSalesOrder latestSalesOrder = new TransSalesOrder();
latestProduct = initProduct.stream().filter(so -> productId.equals(so.getId())).findAny().orElseThrow(() -> new ResourceNotFound("Product " + so.getIs() + " Not Found"));
latestPartner = initPartners.stream().filter(partner -> partnerId.equals(partner.getId())).findAny().orElseThrow(() -> new ResourceNotFound("Partner " + partnerName + " Not Found"));
latestPartnerShipment = initPartnerShipments.stream().filter(partnerShipment -> partnerShipmentId.equals(partnerShipment.getId())).findAny().orElseThrow(() -> new ResourceNotFound("PartnerShipment Not Found"));
}
我必须添加一些东西来保持/等待异步进程正确完成?我该怎么做?
暂无答案!
目前还没有任何答案,快来回答吧!