简体   繁体   English

如何在同一线程中运行单声道,在平行通量内

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

I'm trying to fill objects inside Flux with values from Mono.我正在尝试用 Mono 中的值填充 Flux 中的对象。 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.我认为这是因为 Flux 是并行工作的,而 Mono 不是。 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.如您所见,我正在尝试将国家/地区代码设置为代理。

Solved.解决了。 Added that Mono object in "flatMap" operator.在“flatMap”操作符中添加了 Mono 对象。 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())));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM