2016-10-19 4 views
0

私のコードは次のようである:rxjava:retry私のコードでは動作しませんか?

出力は次のようになります。

It just come in 

number is: 6 

Boo! 

私の期待は、例外がFooExceptionであれば、それだけで一度購読するようにしかし、それは見えます、再サブスクライブする必要があります。

私のコードで何が問題なのか分かりません。

もう一つ質問、最大再試行を設定することができますどのようにあなたのコードと間違って何もありません

 public class ErrorEmitter implements Observable.OnSubscribe<Integer> { 
    private int throwAnErrorCounter = 7; 
    public void call(Subscriber<? super Integer> subscriber) { 

     System.out.println("It just come in"); 
     if (throwAnErrorCounter > 4) { 
      throwAnErrorCounter--; 
      System.out.println("number is: "+throwAnErrorCounter); 
      subscriber.onError(new FooException()); 
      return; 
     } 
     if (throwAnErrorCounter > 0) { 
      throwAnErrorCounter--; 
      System.out.println("number is: "+throwAnErrorCounter); 
      subscriber.onError(new BooException()); 
      return; 
     } 

     System.out.println("number is will complete"); 
     subscriber.onCompleted(); 
    } 
} 



     Observable.create(new ErrorEmitter()).retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() { 
     public Observable<?> call(Observable<? extends Throwable> attempts) { 
      return attempts.flatMap(new Func1<Throwable, Observable<?>>() { 
             public Observable<?> call(Throwable error) { 
              if (error instanceof FooException) { 
               return Observable.timer(1L, TimeUnit.SECONDS); 
              } 
              return Observable.error(error); 
             } 
            } 
      ); 
     } 
    }).subscribe(new Subscriber<Integer>() { 
     public void onCompleted() { 
      System.out.println("complete"); 
     } 

     public void onError(Throwable throwable) { 
      System.out.println(throwable.getMessage()); 
     } 

     public void onNext(Integer integer) { 

      System.out.println("it is in next "); 
     } 
    }); 

答えて

1

(例えば、でもFooExceptionを返す、私は2回だけ再サブスクライブしたいです)。 timerObservable.just(null)に置き換え、何がうまくいかないかを確認してください。

関連する問題