Reputation: 3632
I have a rest api controller that take a huge collection of elements, search each element in DB ,update fields then save updated objectin DB Response is really slow so i want to use some kind of async calls. My code:
@RequestMapping(value = "qwerty/", method = RequestMethod.POST,
consumes = {"application/xml"},
produces = "application/xml")
@ResponseBody
public PriceResponceDTOCollection performTask(@RequestBody PriceDataDTOCollection priceList){
final List<PriceError> priceErrorList = new ArrayList<>();
final List<PriceError> priceErrorList = new ArrayList<>();
final List<CompletableFuture> futures = new ArrayList();
for (final PriceData price : priceList.getPriceList()) {
futures.add(CompletableFuture.supplyAsync(()->priceService.setPrice(price)).thenAccept(priceErrorList::add));
}
futures.stream().map(CompletableFuture::join).count();
}
This is priceservice's setPrice
method
@Autowired
private FlexibleSearchService flexibleSearchService;
public PriceError setPrice(Price data){
//building query
//initialize map with params
return flexibleSearchService.search(query.toString(),params).getResult().get(0);
}
Then i get NPE in de.hybris.platform.servicelayer.session.impl.DefaultSessionService.executeInLocalView(DefaultSessionService.java:94)
BTW , if my controller look's like this(without Fork join pool):
@RequestMapping(value = "qwerty/", method = RequestMethod.POST,
consumes = {"application/xml"},
produces = "application/xml")
@ResponseBody
public PriceResponceDTOCollection performTask(@RequestBody PriceDataDTOCollection priceList){
final List<PriceError> priceErrorList = new ArrayList<>();
for (final PriceData price : priceList.getPriceList()) {
PriceError err = priceService.setPrice(price);
priceErrorList.add(err);
}
}
everything work perfect. What is the reason (I also tried priceList.getPriceList().parallelstream()
and also get NPE)
Upvotes: 0
Views: 401
Reputation: 5758
My opinion updating price via web service is critical operation. Maybe you can convert updates as impex and call impex import service. Impex engine has parallel processing and other protections.
Upvotes: 1