RxJavaリアクティブプログラミング (CodeZine BOOKS) 新品価格 |
RxJavaでflatmapを使ってみた
の続編
次はretryのやり方
・コード全部記載
FlowableretryFlowable = Flowable. create(new FlowableOnSubscribe () {
@Override
public void subscribe(@NonNull FlowableEmittere) throws Exception {
System.out.println("retry subscribe start");
for (int i = 0; i <= 3; i++) {
if (i == 2) {
throw new Exception("retry subscribe reigai");
}
e.onNext(i);
}
e.onComplete();
System.out.println("retry subscribe end");
}
}, BackpressureStrategy.BUFFER)
.doOnSubscribe(new Consumer() {
@Override
public void accept(Subscription subscription) throws Exception {
System.out.println("retry accept");
}
}).retry(2);
retryFlowable.subscribe(new Subscriber() {
@Override
public void onSubscribe(Subscription s) {
s.request(Integer.MAX_VALUE);
System.out.println("retry onSubscribe onsubscribe");
}
@Override
public void onNext(Integer integer) {
System.out.println("retry onNext onNext:"+integer);
}
@Override
public void onError(Throwable t) {
System.out.println("retry onError error"+t);
}
@Override
public void onComplete() {
System.out.println("retry onComplete complete");
}
});
・コードwithラムダ
FlowableretryFlowable = Flowable. create(e -> {
System.out.println("retry subscribe start");
for (int i = 0; i <= 3; i++) {
if (i == 2) {
throw new Exception("retry subscribe reigai");
}
e.onNext(i);
}
e.onComplete();
System.out.println("retry subscribe end");
}, BackpressureStrategy.BUFFER)
.doOnSubscribe(subscription -> {
System.out.println("retry accept");
}).retry(2);
・結果
nextが0と1が通るけど、その後エラーで繰り返されて
2回目でエラー吐いて終了
retry onSubscribe onsubscribe
retry accept
retry subscribe start
retry onNext onNext:0
retry onNext onNext:1
retry accept
retry subscribe start
retry onNext onNext:0
retry onNext onNext:1
retry accept
retry subscribe start
retry onNext onNext:0
retry onNext onNext:1
retry onError errorjava.lang.Exception: retry subscribe reigai