如何使用SpringWebClient进行非阻塞呼叫,并在所有呼叫完成后发送电子邮件?

7cjasjjr  于 2021-07-15  发布在  Java
关注(0)|答案(3)|浏览(396)

我使用spring的“webclient”和projectreactor对url列表进行非阻塞调用。我的要求是:
对URL列表异步调用get
在调用每个url时记录url
记录导致异常的调用的url
记录成功呼叫的url
记录导致非2xx http状态的调用的url
发送一封电子邮件,其中包含调用导致异常或非2xx http状态的URL列表
以下是我的尝试:

List<Mono<ClientResponse>> restCalls = new ArrayList<>();
List<String> failedUrls = new ArrayList<>();    
for (String serviceUrl : serviceUrls.getServiceUrls()) {

        restCalls.add(
                webClientBuilder
                    .build()
                    .get()
                    .uri(serviceUrl)
                    .exchange()
                    .doOnSubscribe(c -> log.info("calling service URL {}", serviceUrl))
                    .doOnSuccess(response -> log.info("{} success status {}", serviceUrl, response.statusCode().toString()))
                    .doOnError(response -> {log.info("{} error status {}", serviceUrl, response); failedUrls.add(serviceUrl);}));
    }

    Flux.fromIterable(restCalls)
        .map((data) -> data.subscribe())
        .onErrorContinue((throwable, e) -> {

            log.info("Exception for URL {}", ((WebClientResponseException) throwable).getRequest().getURI());

            failedUrls.add(serviceUrl);
        })
        .collectList()
        .subscribe((data) -> {
            log.info("all called");
            email.send("Failed URLs are {}", failedUrls);
    });

问题是电子邮件是在电话回复之前发送的。如何在调用之前等待所有URL调用完成 email.send ?

idfiyjo8

idfiyjo81#

如comment中所述,您的示例中的主要错误是使用了“subscribe”,即启动查询,但与主流量无关,因此您无法返回错误或结果。
subscribe是管道上的一种触发器操作,它不用于链接。
下面是一个完整的示例(电子邮件除外,替换为日志记录):

package fr.amanin.stackoverflow;

import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class WebfluxURLProcessing {

    private static final Logger LOGGER = Logger.getLogger("example");

    public static void main(String[] args) {

        final List<String> urls = Arrays.asList("https://www.google.com", "https://kotlinlang.org/kotlin/is/wonderful/", "https://stackoverflow.com", "http://doNotexists.blabla");

        final Flux<ExchangeDetails> events = Flux.fromIterable(urls)
                // unwrap request async operations
                .flatMap(url -> request(url))
                // Add a side-effect to log results
                .doOnNext(details -> log(details))
                // Keep only results that show an error
                .filter(details -> details.status < 0 || !HttpStatus.valueOf(details.status).is2xxSuccessful());

        sendEmail(events);
    }

    /**
     * Mock emails by collecting all events in a text and logging it.
     * @param report asynchronous flow of responses
     */
    private static void sendEmail(Flux<ExchangeDetails> report) {
        final String formattedReport = report
                .map(details -> String.format("Error on %s. status: %d. Reason: %s", details.url, details.status, details.error.getMessage()))
                // collecting (or reducing, folding, etc.) allows to gather all upstream results to use them as a single value downstream.
                .collect(Collectors.joining(System.lineSeparator(), "REPORT:"+System.lineSeparator(), ""))
                // In a real-world scenario, replace this with a subscribe or chaining to another reactive operation.
                .block();
        LOGGER.info(formattedReport);
    }

    private static void log(ExchangeDetails details) {
        if (details.status >= 0 && HttpStatus.valueOf(details.status).is2xxSuccessful()) {
            LOGGER.info("Success on: "+details.url);
        } else {
            LOGGER.log(Level.WARNING,
                    "Status {0} on {1}. Reason: {2}",
                    new Object[]{
                            details.status,
                            details.url,
                            details.error == null ? "None" : details.error.getMessage()
                    });
        }
    }

    private static Mono<ExchangeDetails> request(String url) {
        return WebClient.create(url).get()
                .retrieve()
                // workaround to counter fail-fast behavior: create a special error that will be converted back to a result
                .onStatus(status -> !status.is2xxSuccessful(), cr -> cr.createException().map(err -> new RequestException(cr.statusCode(), err)))
                .toBodilessEntity()
                .map(response -> new ExchangeDetails(url, response.getStatusCode().value(), null))
                // Convert back custom error to result
                .onErrorResume(RequestException.class, err -> Mono.just(new ExchangeDetails(url, err.status.value(), err.cause)))
                // Convert errors that shut connection before server response (cannot connect, etc.) to a result
                .onErrorResume(Exception.class, err -> Mono.just(new ExchangeDetails(url, -1, err)));
    }

    public static class ExchangeDetails {
        final String url;
        final int status;
        final Exception error;

        public ExchangeDetails(String url, int status, Exception error) {
            this.url = url;
            this.status = status;
            this.error = error;
        }
    }

    private static class RequestException extends RuntimeException {
        final HttpStatus status;
        final Exception cause;

        public RequestException(HttpStatus status, Exception cause) {
            this.status = status;
            this.cause = cause;
        }
    }
}
efzxgjgh

efzxgjgh2#

我还没测试过这个,但应该有用

public void check() {

    List<Flux<String>> restCalls = new ArrayList<>();

    for (String serviceUrl : serviceUrls.getServiceUrls()) {
        restCalls.add(rest.getForEntity(serviceUrl, String.class));
    }

   Flux.fromIterable(restCalls)
            .map((data) -> data.blockFirst())
            .onErrorContinue((throwable, e) -> {
                ((WebClientResponseException) throwable).getRequest().getURI(); // get the failing URI
                // do whatever you need with the failed service

            })
            .collectList() // Collects all the results into a list
            .subscribe((data) -> {
                // from here do whatever is needed from the results
            });
}

因此,如果您还没有这样做,您的服务调用必须是非阻塞的,因此您应该将类型转换为flux。所以在rest服务中,您的方法应该是这样的

public Flux<String> getForEntity(String name) {
    return this.webClient.get().uri("url", name)
            .retrieve().bodyToFlux(String.class);
}

希望有帮助

hpcdzsge

hpcdzsge3#

restCalls.add(
            webClientBuilder
                .build()
                .get()
                .uri(serviceUrl)
                .exchange()
                .doOnSubscribe(c -> log.info("calling service URL {}", serviceUrl))
                .doOnSuccess(response -> log.info("{} success status {}", serviceUrl, response.statusCode().toString()))
                .doOnError(response -> {log.info("{} error status {}", serviceUrl, response); failedUrls.add(serviceUrl);}));

                Flux.fromIterable(restCalls)
                .map((data) -> data.subscribe())
                .onErrorContinue((throwable, e) -> {
                 log.info("Exception for URL {}", ((WebClientResponseException)                       throwable).getRequest().getURI());
                 failedUrls.add(serviceUrl);
                 })
                .collectList()
                .subscribeOn(Schedulers.elastic())
                .subscribe((data) -> {
                log.info("all called");
                email.send("Failed URLs are {}", failedUrls);
                });

相关问题