Skip to content

Instantly share code, notes, and snippets.

@isyufu
Created July 26, 2021 01:43
Show Gist options
  • Save isyufu/a859f309c96b534d6db7f5930c2f685c to your computer and use it in GitHub Desktop.
Save isyufu/a859f309c96b534d6db7f5930c2f685c to your computer and use it in GitHub Desktop.
java reactor parallel execution by key
import java.util.Arrays;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
Flux.fromIterable(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20))
.groupBy(x -> x.hashCode() % 5)
.parallel(10)
.runOn(Schedulers.parallel())
.flatMap(g -> g.collectMap(y -> {log.info(" g: "+g.key()+" y: "+ y); return y;}).log("subscr"))
.subscribe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment