How to run mono in the same thread, inside parallel flux

与世无争的帅哥 提交于 2020-01-16 08:38:12

问题


I'm trying to fill objects inside Flux with values from Mono. When i'm trying to do so, it's just ignoring my "set" operation. I assume that it's because Flux is working in parallel, while Mono is not. How can i solve this problem?

Flux.fromIterable(proxyParserService.getProxyList())
            .parallel()
            .runOn(Schedulers.parallel())
            .filter(proxy -> proxy.getCorrupted() == null || !proxy.getCorrupted())
            .subscribe(proxy -> {
                        try {
                            RestTemplate restTemplate = getProxiedTemplate(proxy.getHost(), proxy.getPort());
                            restTemplate.exchange(URI, HttpMethod.GET, HttpEntity.EMPTY, String.class);
                            geoDataService.getData(proxy.getHost()) // Here comes the Mono object, that contains needed value to set into "proxy"
                                    .subscribe(geoData ->
                                    {
                                        log.info("GEODATA: {} ", geoData);
                                        proxy.setCountryCode(geoData.getCountryCode()); // ignored somehow
                                    });
                            proxy.setCorrupted(false);
                            addresses.add(proxy);
                            log.info("IP {}:{} is OK", proxy.getHost(), proxy.getPort());
                            log.info("Final result: {}", proxy.toString());
                        } catch (ResourceAccessException e) {
                            log.info("IP {}:{} is corrupted!", proxy.getHost(), proxy.getPort());
                            proxy.setCorrupted(true);
                            addresses.add(proxy);
                        }
                    },
                    throwable -> log.error(String.format("Exception caught while trying to fill map: %s", throwable.getCause())));

}

Here's some logs

As you can see i'm trying to set country code into proxy.


回答1:


Solved. Added that Mono object in "flatMap" operator. Example:

Flux.fromIterable(proxyParserService.getProxyList())
            .parallel()
            .runOn(Schedulers.parallel())
            .filter(poxy -> !valueExist(addresses.values(), poxy))
            .flatMap(geoDataService::getData) // Now it runs in parallel threads
            .subscribe(proxy -> {
                        try {
                            RestTemplate restTemplate = getProxiedTemplate(proxy.getHost(), proxy.getPort());
                            restTemplate.exchange(URI, HttpMethod.GET, HttpEntity.EMPTY, String.class);
                            proxy.setCorrupted(false);
                            addresses.put(proxy.getCountryCode(), proxy);
                            log.info("IP {}:{} is OK", proxy.getHost(), proxy.getPort());
                            log.info("Final result: {}", proxy.toString());
                        } catch (ResourceAccessException e) {
                            log.info("IP {}:{} is corrupted!", proxy.getHost(), proxy.getPort());
                            proxy.setCorrupted(true);
                            addresses.put(proxy.getCountryCode(), proxy);
                        }
                    },
                    throwable -> log.error(String.format("Exception caught while trying to fill map: %s", throwable.getCause())));


来源:https://stackoverflow.com/questions/59652807/how-to-run-mono-in-the-same-thread-inside-parallel-flux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!