I have splitted a list in 5 parts, and I want to execute in parallel using restTemplate, I have the following method:
@Async
private CompletableFuture<List<Cidade>> MontarCidadesPorEstado(List<Cidade> listaCidades, List<UF> estado) {
estado.forEach(es -> {
Map<String, String> param = new HashMap<String, String>();
param.put("UF", es.getSigla());
Cidade[] cidades = restTemplate.getForObject(url, Cidade[].class, param);
listaCidades.addAll(Arrays.asList(cidades));
});
return CompletableFuture.completedFuture(listaCidades);
}
So created a List of CompletableFuture
to execute all of them in parallel, but when an add each CompletableFuture
into a list it's already execute it without make it parallel, ex:
List<List<UF>> subSets = Lists.partition(estados, 5);
List<CompletableFuture<List<Cidade>>> lstCompletableFuture = new ArrayList();
subSets.forEach(estado -> {
lstCompletableFuture.add(MontarCidadesPorEstado(listaCidades, estado));
});
CompletableFuture.allOf(lstCompletableFuture.toArray(new CompletableFuture[lstCompletableFuture.size()]));
What I'm doing wrong, I thought it's supposed to execute the method MontarCidadesPorEstado when I call CompletableFuture.allOf
.