Skip to content

Instantly share code, notes, and snippets.

@umit
Last active April 11, 2018 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umit/454b4e8e68a5ee70724428bc8c6bb46f to your computer and use it in GitHub Desktop.
Save umit/454b4e8e68a5ee70724428bc8c6bb46f to your computer and use it in GitHub Desktop.
Flux Mono
List<String> images = IntStream.range(0, 100).mapToObj(each -> "https://picsum.photos/1024/1024/?random").collect(Collectors.toList());
Scheduler subWorker = Schedulers.fromExecutor(Executors.newFixedThreadPool(20));
Scheduler pubWorker = Schedulers.newParallel("aaaa", 5);
RestTemplate restTemplate = new RestTemplate();
Function<String, Publisher<byte[]>> stringPublisherFunction = each -> Mono.defer(() -> {
System.out.println("flux:Mono executor: " + Thread.currentThread().getName() + "- " + Thread.currentThread().getId());
return Mono.just(restTemplate.getForObject(each, byte[].class));
}).subscribeOn(subWorker);
Flux<byte[]> flux = Flux.fromIterable(images).publishOn(pubWorker).flatMap(stringPublisherFunction);
flux.subscribe(new Consumer<byte[]>() {
@Override
public void accept(byte[] bytes) {
System.out.println("flux:subscribe executor: " + Thread.currentThread().getName() + "- " + Thread.currentThread().getId());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment