title: RxJava : Retrying a subscriber using retryWhen()
body: <p>I am consuming a REST api using Vertx RxJava subscriber. I want to retry a request when there is a specific error, below is my code.
  When I run this, the retry keeps happening infinitely, I'd rather have it retry just thrice.</p>
  
  <p>I am new to RxJava and still getting the hang of doing things in Rx way.
  Can someone tell if I am missing something? Thanks!</p>
  
  <pre><code>responseSingle.map(response -&gt; {
              //do some stuff
              return responseBody;
  
          }).retryWhen(errors -&gt; {
                      errors.flatMap(error -&gt; {
                          log.info("==================== caught error ==================== ");
                          // For IOExceptions, we  retry
                          if (error instanceof MySpecificException) {
                              log.info("==================== retrying request ==================== ");
                              return Observable.just(null);
                          }
  
                          // For anything else, don't retry
                          return errors;
                      });
                      return errors;
                  }
          ).onErrorReturn(t -&gt; {
              //do some stuff
              return responseBody;
          });
  </code></pre>