I'm using Spring reactive as a server to make an expensive generation and return the results in Flux one by one. This has the advantage of stopping the generation if the request is cancelled (in cas the constraints and are too tight for example). My code look like this:
public Flux<Entity> generate(int nbrOfEntitiesToGenerate, Constaints constraints) {
return Flux.range(0, nbrOfEntitiesToGenerate)
.map(x -> Generator.expensiveGeneration(constraints)
// .subscribeOn(Schedulers.parallel())
;
}
This only does half of what I want, I doesn't make the next call expensiveGeneration
when cancelled, but doesn't stop currently running expensive generation that might never finish if the constraints are too tight. How can I do that please.
Extra question if you know, how can I generate x entities in parralel to maximize the use of my threads, (of course without starting ALL the generations at once).
Thanks in advance.