1

How can i limit threads for task, that being executed in parallel? The issue is simple - while my scheduler working, i can't do anything else (fetch some info using postman etc). Is there any way to solve this problem? Also, i've tryed to set number of threads in flux, for example, using parallel(3).runOn(Schedulers.parallel()) and still my programm is blocked.

@Scheduled(fixedRate = 60000L)
@PostConstruct
public void fillMap() {
    Flux.fromIterable(proxyParserService.getProxyList())
            .parallel()
            .runOn(Schedulers.parallel())
            .flatMap(geoDataService::getData)
//some logic here...

Also worh mentioning, that i have flatmap method with opening connections in parallel:

    public Mono<Address> getData(Address proxy) {
    WebClient webClient = WebClient.builder()
            .baseUrl(String.format(URL, proxy.getHost()))
            .build();
    WebClient.RequestBodyUriSpec request = webClient.method(HttpMethod.GET);
    return request.retrieve()
            .onStatus(HttpStatus::isError, clientResponse -> {
                log.error("Error while calling endpoint {} with status code {}",
                        URL, clientResponse.statusCode());
                throw new RuntimeException("Error while calling geolocation endpoint");
            })
            .bodyToMono(Address.class)
  • Probably, the reason is not in the number of threads but in number of open concurrent connections. What does the `fillMap` method do? How many servers does it open connections to? Can you try the [`limitRate`](https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#limitRate-int-) operator instead of `parallel`? – Denis Zavedeev Jan 09 '20 at 15:17
  • @caco3 This method opening connections with RestTemplate, setting proxy and trying to ping google.com. Full realisation is here - [link](https://stackoverflow.com/questions/59652807/how-to-run-mono-in-the-same-thread-inside-parallel-flux?noredirect=1#comment105509203_59652807) – SageFreke Jan 10 '20 at 07:15

0 Answers0