0
votes

I am trying to call a rest api & accumulating result by using Flux & Webclient. I want to understand the execution of flatMap operator for below code.

  • list - Contains 50000 element.
  • restClient - It is a WebClient. WebClient.builder().baseUrl(url).filter(ExchangeFilterFunctions.basicAuthentication(userId, pwd)).build()
  • restClient.getResult - return a Mono. client.get().uri(uri).retrieve().bodyToMono(returnType)

First log is printing thread name as main for certain no. of element then it started to print reactor-http-nio-(thread-no). Second log is printing thread name as reactor-http-nio-(thread-no). Pls explain how does the thread execution & switching is happening here.

        Flux.fromIterable(list)
            .log()
            .map(e -> e.trim())
            .flatMap(e -> restClient.getResult(client, e, String.class))//e is being used to create uri
            .log()
            .subscribe(e -> result.add(e));
1

1 Answers

0
votes

flatMap has a concurrency parameter, which defaults to 256. As soon as it is subscribed, it requests 256 elements from the source (the fromIterable) and maps them to inner publishers (the WebClient calls).

This is done in the subscription thread, which here is the main thread.

Once it has 256 elements "in flight", flatMap will replenish as needed whenever an inner completes, by requesting one more source element from the iterable, then mapping it to a new WebClient inner.

This is done in the thread of the inner that just finished, which here is the netty http-nio-x thread.