简体   繁体   English

Java 通量异常处理?

[英]Java Flux Exception Handling?

Can someone please guide me as I am new to the flux and trying to understand how to handle this scenario?有人可以指导我吗,因为我是新手并试图了解如何处理这种情况?

Issue: I am getting readTimeout exception in one of the flux responses from getResp() method below and then all the prior successful responses are ignored and the exception error is returned.问题:我在下面的getResp()方法的通量响应之一中收到 readTimeout 异常,然后忽略所有先前的成功响应并返回异常错误。

Instead, I want to return all the successful responses I received prior to the exception.相反,我想返回异常之前收到的所有成功响应。

public Flux<CustomObject1> getInfo(List<RequestObj> requestObjList) {
return requestObjList.stream()
       .parallel()
       .map(this::getResp)
       .reduce(Flux::Merge)
       .orElse(Flux.empty());
}

public Flux<CustomObject1> getResp(RequestObj requestObj){
// process the request and return ...        

}

Please let me know if this is not clear, happy to provide more details.如果不清楚,请告诉我,很乐意提供更多详细信息。

There are several ways to handle errors in flatMap flatMap中有几种处理错误的方法

Use flatMapDelayError that will delay any error until all elements are processed使用flatMapDelayError将延迟任何错误,直到处理完所有元素

public Flux<CustomObject1> getInfo(List<RequestObj> requestObjList) {
    return Flux.fromIterable(requestObjList)
            .flatMapDelayError(this::getResp, Queues.SMALL_BUFFER_SIZE, Queues.XS_BUFFER_SIZE)
            .onErrorResume(e -> {
                // log error
                return Mono.empty();
            });
}

Handle error for every element处理每个元素的错误

public Flux<CustomObject1> getInfo(List<RequestObj> requestObjList) {
    return Flux.fromIterable(requestObjList)
            .flatMap(request -> 
                    getResp(request)
                            .onErrorResume(e -> {
                                // log error
                                return Mono.empty();
                            })
            );

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

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