Vic*_*tor 22 java reactive-programming project-reactor
我试图找出在Flux中映射元素时如何处理错误.
例如,我将CSV字符串解析为我的一个业务POJO:
myflux.map(stock -> converter.convertHistoricalCSVToStockQuotation(stock));
Run Code Online (Sandbox Code Playgroud)
其中一些行可能包含错误,因此我在日志中得到的是:
reactor.core.publisher.FluxLog: onNext([SOME_BOGUS_QUOTE]@38.09 (Fri Apr 08 00:00:00 CEST 2016) H(38.419998)/L(37.849998)/O(37.970001))
reactor.core.publisher.FluxLog: onNext([SOME_BOGUS_QUOTE]@38.130001 (Thu Apr 07 00:00:00 CEST 2016) H(38.189999)/L(37.610001)/O(37.799999))
reactor.core.publisher.FluxLog: onError(java.lang.IllegalArgumentException: Invalid CSV stock quotation: SOME_BOGUS_QUOTE,trololo)
reactor.core.publisher.FluxLog: java.lang.IllegalArgumentException: Invalid CSV stock quotation: SOME_BOGUS_QUOTE,trololo
Run Code Online (Sandbox Code Playgroud)
我在API中读到了一些错误处理方法,但大多数都提到返回"错误值"或使用回退Flux,如下所示:
Flux.onErrorResumeWith(myflux, x -> Mono.fromCallable(() -> ... do stuff);
Run Code Online (Sandbox Code Playgroud)
然而,用我的myflux意思是再次处理整个通量.
那么,有没有办法在处理特定元素时处理错误(即忽略它们/记录它们)并继续处理其余的通量?
使用@akarnokd解决方法进行更新
public Flux<StockQuotation> getQuotes(List<String> tickers)
{
Flux<StockQuotation> processingFlux = Flux.fromIterable(tickers)
// Get each set of quotes in a separate thread
.flatMap(s -> Mono.fromCallable(() -> feeder.getCSVQuotes(s)))
// Convert each list of raw quotes string in a new Flux<String>
.flatMap(list -> Flux.fromIterable(list))
// Convert the string to POJOs
.flatMap(x -> {
try {
return Flux.just(converter.convertHistoricalCSVToStockQuotation(x));
}
catch (IllegalArgumentException ex){
System.out.println("Error decoding stock quotation: " + x);
return Flux.empty();
}
});
return processingFlux;
}
Run Code Online (Sandbox Code Playgroud)
然而,这可以作为一种魅力,因为你可以看到代码不像以前那么优雅.Flux API是否有任何方法可以执行此代码的操作?
retry(...)
retryWhen(...)
onErrorResumeWith(...)
onErrorReturn(...)
Run Code Online (Sandbox Code Playgroud)
aka*_*okd 17
flatMap如果处理失败,您需要返回一个空序列:
myflux.flatMap(v -> {
try {
return Flux.just(converter.convertHistoricalCSVToStockQuotation(stock));
} catch (IllegalArgumentException ex) {
return Flux.empty();
}
});
Run Code Online (Sandbox Code Playgroud)
Tsv*_*ski 13
如果您想使用Reactor 3的方法来处理异常,您可以使用Mono.fromCallable.
flatMap(x ->
Mono.fromCallable(() -> converter.convertHistoricalCSVToStockQuotation(x))
.flux()
.flatMap(Flux::fromIterable)
.onErrorResume(Flux::empty)
)
Run Code Online (Sandbox Code Playgroud)
不幸的是没有Flux.fromCallable,所以假设callable返回一个列表,你必须手动将它转换为Flux.
在 Reactor 3 的当前版本中,添加了相当多的方法。所以我们可以这样做:
Flux.onErrorResume(error -> {
System.out.println("Error decoding stock quotation: " + e);
return Flux.empty();
});
Run Code Online (Sandbox Code Playgroud)
在此处查看有关如何处理错误的更多信息
| 归档时间: |
|
| 查看次数: |
10331 次 |
| 最近记录: |