我有这样的数据流:
Observable
.fromFuture(
CompletableFuture.supplyAsync { // First remote call returns Future<List<Type>>
listOf(1, 2, 3, 57005, 5)
},
Schedulers.computation()
)
.flatMap { it.toObservable() } // I turn that list into a stream of single values to process them one by one
.map {
CompletableFuture.supplyAsync { // This remote call may fail if it does not like the input. I want to skip that failures and continue the stream like the fail never occurred.
if (it == 0xDEAD) {
throw IOException("Dead value!")
}
it
}
}
.flatMap {
Observable.fromFuture(it) // Turn that Futures into a stream of Observables once again
}
.doOnNext {
println(it) // Debug
}
.blockingSubscribe()
我用Future
替换了业务逻辑(实际上返回CompletableFuture.supplyAsync
s)。
而且,是的,这是Kotlin,但我猜你有意图。
当我评论“死”值(57005
,0xDEAD
)时,输出为:
1
4
9
25
但如果流中出现“死”值,则会失败:
1
4
9
Exception in thread "main" java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.io.IOException: Dead value!
at io.reactivex.internal.util.ExceptionHelper.wrapOrThrow(ExceptionHelper.java:45)
at io.reactivex.internal.operators.observable.ObservableBlockingSubscribe.subscribe(ObservableBlockingSubscribe.java:86)
at io.reactivex.Observable.blockingSubscribe(Observable.java:5035)
at by.dev.madhead.rx.TestKt.main(test.kt:41)
Caused by: java.util.concurrent.ExecutionException: java.io.IOException: Dead value!
at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)
...
我是RX的新手,所以很快就用谷歌搜索解决方案:onExceptionResumeNext
:Observable.fromFuture(it)
- &gt; Observable.fromFuture(it).onExceptionResumeNext { Observable.empty<Int>() }
。但现在我的应用程序永远挂起(在产生我期望的输出之后)。
看起来流不会结束。
我应该“关闭”Observable
以某种方式或什么?
或者,更一般地说,使用RX时这是一个好方法吗?
我应该以另一种方式重新考虑吗?
答案 0 :(得分:2)
吞下这样的例外:
{7C945F97-A2CC-4EEB-87AC-3D23D6C490A0};{A9B1787A-32E4-4D51-819D-B3F879574BCE}