I am starting to play with streams in java 8 and I want to do something that feels should be possible but somehow cant seem to get it down.
I have to make call from my service to external service that can process at most 500 unique id and given time. My service can receive any number of this at a given time.
I am looking for a short and precise way to split the list of incoming id's to size of 500 , make call to external service and merge them before returning me the result (Bonus if we can do this in parallel)
private List<ServiceReply> makeServiceCallByBatch(List<Long> ids,AuthToken authToken){
//make external call and get reply
}
public List<ServiceReply> makeServiceCall(List<Long> ids,AuthToken authToken){
private int BATCH_SIZE = 500;
if(ids.size() > BATCH_SIZE){
//split by batch and process each batch and merge it all
} else{
return makeServiceCallByBatch(ids,authToken)
}
}
subList
view and performing the operation? Not really sure by the first look, what do you mean by "merge it all"! You might want to have abatchCall
at your service implementation to make it cleaner anyway.(0,500)
,(500,1000)
,(1000,1400)
? See a pattern there to split the list? Making a call to your service for those views might just return specific results and then you canaddAll
to an emptyList<ServiceReply>
initialized before you start processing.