本文整理了Java中io.reactivex.Observable.onErrorResumeNext()
方法的一些代码示例,展示了Observable.onErrorResumeNext()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.onErrorResumeNext()
方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:onErrorResumeNext
[英]Instructs an ObservableSource to pass control to another ObservableSource rather than invoking Observer#onError if it encounters an error.
By default, when an ObservableSource encounters an error that prevents it from emitting the expected item to its Observer, the ObservableSource invokes its Observer's onError method, and then quits without invoking any more of its Observer's methods. The onErrorResumeNext method changes this behavior. If you pass another ObservableSource ( resumeSequence) to an ObservableSource's onErrorResumeNext method, if the original ObservableSource encounters an error, instead of invoking its Observer's onError method, it will instead relinquish control to resumeSequence which will invoke the Observer's Observer#onNext method if it is able to do so. In such a case, because no ObservableSource necessarily invokes onError, the Observer may never know that an error happened.
You can use this to prevent errors from propagating or to supply fallback data should errors be encountered. Scheduler: onErrorResumeNext does not operate by default on a particular Scheduler.
[中]指示一个ObservateSource将控制权传递给另一个ObservateSource,而不是在遇到错误时调用Observator#onError。
默认情况下,当ObservateSource遇到一个错误,阻止它向其观察者发送预期的项时,ObservateSource调用其观察者的OneError方法,然后退出,不再调用任何观察者的方法。下一个方法会更改此行为。如果您将另一个ObservateSource(resumeSequence)传递给ObservateSource的OneErrorResumeNext方法,如果原始ObservateSource遇到错误,它将放弃对resumeSequence的控制,resumeSequence将调用观察者的Observator#onNext方法(如果它能够这样做)。在这种情况下,由于ObservateSource不一定调用onError,因此观察者可能永远不会知道发生了错误。
您可以使用它来防止错误传播,或者在遇到错误时提供回退数据。调度器:OneRorResumeNext默认情况下不会在特定的调度器上运行。
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void onErrorResumeNextFunctionNull() {
just1.onErrorResumeNext((Function<Throwable, Observable<Integer>>)null);
}
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void onErrorResumeNextObservableNull() {
just1.onErrorResumeNext((Observable<Integer>)null);
}
代码示例来源:origin: ReactiveX/RxJava
@Override
public Object apply(Observable<Integer> o) throws Exception {
return Observable.error(new IOException())
.onErrorResumeNext(Functions.justFunction(o));
}
}, false, 1, 1, 1);
代码示例来源:origin: ReactiveX/RxJava
@Test
@Ignore("Publishers should not throw")
public void testResumeNextWithFailureOnSubscribe() {
Observable<String> testObservable = Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(Observer<? super String> t1) {
throw new RuntimeException("force failure");
}
});
Observable<String> resume = Observable.just("resume");
Observable<String> observable = testObservable.onErrorResumeNext(resume);
Observer<String> observer = TestHelper.mockObserver();
observable.subscribe(observer);
verify(observer, Mockito.never()).onError(any(Throwable.class));
verify(observer, times(1)).onComplete();
verify(observer, times(1)).onNext("resume");
}
代码示例来源:origin: ReactiveX/RxJava
public final Observable<T> onErrorResumeNext(final ObservableSource<? extends T> next) {
ObjectHelper.requireNonNull(next, "next is null");
return onErrorResumeNext(Functions.justFunction(next));
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testResumeNext() {
Disposable upstream = mock(Disposable.class);
// Trigger failure on second element
TestObservable f = new TestObservable(upstream, "one", "fail", "two", "three");
Observable<String> w = Observable.unsafeCreate(f);
Observable<String> resume = Observable.just("twoResume", "threeResume");
Observable<String> observable = w.onErrorResumeNext(resume);
Observer<String> observer = TestHelper.mockObserver();
observable.subscribe(observer);
try {
f.t.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
verify(observer, Mockito.never()).onError(any(Throwable.class));
verify(observer, times(1)).onComplete();
verify(observer, times(1)).onNext("one");
verify(observer, Mockito.never()).onNext("two");
verify(observer, Mockito.never()).onNext("three");
verify(observer, times(1)).onNext("twoResume");
verify(observer, times(1)).onNext("threeResume");
}
代码示例来源:origin: ReactiveX/RxJava
@Test
@Ignore("Publishers should not throw")
public void testResumeNextWithFailureOnSubscribeAsync() {
Observable<String> testObservable = Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(Observer<? super String> t1) {
throw new RuntimeException("force failure");
}
});
Observable<String> resume = Observable.just("resume");
Observable<String> observable = testObservable.subscribeOn(Schedulers.io()).onErrorResumeNext(resume);
Observer<String> observer = TestHelper.mockObserver();
TestObserver<String> to = new TestObserver<String>(observer);
observable.subscribe(to);
to.awaitTerminalEvent();
verify(observer, Mockito.never()).onError(any(Throwable.class));
verify(observer, times(1)).onComplete();
verify(observer, times(1)).onNext("resume");
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testOnErrorCalledOnScheduler() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Thread> thread = new AtomicReference<Thread>();
Observable.<String>error(new Exception())
.delay(0, TimeUnit.MILLISECONDS, Schedulers.newThread())
.doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
thread.set(Thread.currentThread());
latch.countDown();
}
})
.onErrorResumeNext(Observable.<String>empty())
.subscribe();
latch.await();
assertNotEquals(Thread.currentThread(), thread.get());
}
代码示例来源:origin: ReactiveX/RxJava
/**
* Test that when a function throws an exception this is propagated through onError.
*/
@Test
public void testFunctionThrowsError() {
Subscription s = mock(Subscription.class);
TestObservable w = new TestObservable(s, "one");
Function<Throwable, Observable<String>> resume = new Function<Throwable, Observable<String>>() {
@Override
public Observable<String> apply(Throwable t1) {
throw new RuntimeException("exception from function");
}
};
Observable<String> o = Observable.unsafeCreate(w).onErrorResumeNext(resume);
Observer<String> observer = TestHelper.mockObserver();
o.subscribe(observer);
try {
w.t.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
// we should get the "one" value before the error
verify(observer, times(1)).onNext("one");
// we should have received an onError call on the Observer since the resume function threw an exception
verify(observer, times(1)).onError(any(Throwable.class));
verify(observer, times(0)).onComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testResumeNextWithAsyncExecution() {
final AtomicReference<Throwable> receivedException = new AtomicReference<Throwable>();
Subscription s = mock(Subscription.class);
TestObservable w = new TestObservable(s, "one");
Function<Throwable, Observable<String>> resume = new Function<Throwable, Observable<String>>() {
@Override
public Observable<String> apply(Throwable t1) {
receivedException.set(t1);
return Observable.just("twoResume", "threeResume");
}
};
Observable<String> o = Observable.unsafeCreate(w).onErrorResumeNext(resume);
Observer<String> observer = TestHelper.mockObserver();
o.subscribe(observer);
try {
w.t.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
verify(observer, Mockito.never()).onError(any(Throwable.class));
verify(observer, times(1)).onComplete();
verify(observer, times(1)).onNext("one");
verify(observer, Mockito.never()).onNext("two");
verify(observer, Mockito.never()).onNext("three");
verify(observer, times(1)).onNext("twoResume");
verify(observer, times(1)).onNext("threeResume");
assertNotNull(receivedException.get());
}
代码示例来源:origin: ReactiveX/RxJava
Observable<String> observable = w.onErrorResumeNext(resume);
代码示例来源:origin: ReactiveX/RxJava
Observable<String> observable = w.onErrorResumeNext(resume);
代码示例来源:origin: ReactiveX/RxJava
Observable<String> o = w.onErrorResumeNext(new Function<Throwable, Observable<String>>() {
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testBackpressure() {
TestObserver<Integer> to = new TestObserver<Integer>();
Observable.range(0, 100000)
.onErrorResumeNext(new Function<Throwable, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Throwable t1) {
return Observable.just(1);
}
})
.observeOn(Schedulers.computation())
.map(new Function<Integer, Integer>() {
int c;
@Override
public Integer apply(Integer t1) {
if (c++ <= 1) {
// slow
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return t1;
}
})
.subscribe(to);
to.awaitTerminalEvent();
to.assertNoErrors();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testBackpressure() {
TestObserver<Integer> to = new TestObserver<Integer>();
Observable.range(0, 100000)
.onErrorResumeNext(Observable.just(1))
.observeOn(Schedulers.computation())
.map(new Function<Integer, Integer>() {
int c;
@Override
public Integer apply(Integer t1) {
if (c++ <= 1) {
// slow
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return t1;
}
})
.subscribe(to);
to.awaitTerminalEvent();
to.assertNoErrors();
}
}
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void onErrorResumeNextFunctionReturnsNull() {
Observable.error(new TestException()).onErrorResumeNext(new Function<Throwable, Observable<Object>>() {
@Override
public Observable<Object> apply(Throwable e) {
return null;
}
}).blockingSubscribe();
}
代码示例来源:origin: redisson/redisson
public final Observable<T> onErrorResumeNext(final ObservableSource<? extends T> next) {
ObjectHelper.requireNonNull(next, "next is null");
return onErrorResumeNext(Functions.justFunction(next));
代码示例来源:origin: ReactiveX/RxJava
}).onErrorResumeNext(new Function<Throwable, Observable<String>>() {
代码示例来源:origin: ReactiveX/RxJava
/**
* Test that we receive the onError if an exception is thrown from an operator that
* does not have manual try/catch handling like map does.
*/
@Test
@Ignore("Failed operator may leave the child Observer in an inconsistent state which prevents further error delivery.")
public void testOnErrorResumeReceivesErrorFromPreviousNonProtectedOperator() {
TestObserver<String> to = new TestObserver<String>();
Observable.just(1).lift(new ObservableOperator<String, Integer>() {
@Override
public Observer<? super Integer> apply(Observer<? super String> t1) {
throw new RuntimeException("failed");
}
}).onErrorResumeNext(new Function<Throwable, Observable<String>>() {
@Override
public Observable<String> apply(Throwable t1) {
if (t1.getMessage().equals("failed")) {
return Observable.just("success");
} else {
return Observable.error(t1);
}
}
}).subscribe(to);
to.assertTerminated();
System.out.println(to.values());
to.assertValue("success");
}
代码示例来源:origin: ReactiveX/RxJava
.onErrorResumeNext(new Function<Throwable, Observable<String>>() {
@Override
public Observable<String> apply(Throwable t1) {
内容来源于网络,如有侵权,请联系作者删除!