0

I am trying to implement a retry on specific exception but I could not make it work using the following:

    return client
        .sendWebhook(request, url)
        .exchangeToMono(
            response -> {
              final HttpStatus status = response.statusCode();
              return response
                  .bodyToMono(String.class)
                  .defaultIfEmpty(StringUtils.EMPTY)
                  .map(
                      body -> {
                        if (status.is2xxSuccessful()) {
                          log.info("HTTP_SUCCESS[{}][{}] body[{}]", functionName, company, body);
                          return ResponseEntity.ok().body(body);
                        } else {
                          log.warn(
                              format(
                                  "HTTP_ERROR[%s][%s] status[%s] body[%s]",
                                  functionName, company, status, body));
                          return status.is4xxClientError()
                              ? ResponseEntity.badRequest().body(body)
                              : ResponseEntity.internalServerError().body(body);
                        }
                      });
            })
        .retryWhen(
            Retry.backoff(1, Duration.ofSeconds(1))
                .filter(
                    err -> {
                      if (err instanceof PrematureCloseException) {
                        log.warn("PrematureCloseException detected retrying.");
                        return true;
                      }
                      return false;
                    }))
        .onErrorResume(
            ex -> {
              log.warn(
                  format(
                      "HTTP_ERROR[%s][%s] errorInternal[%s]",
                      functionName, company, ex.getMessage()));
              return Mono.just(ResponseEntity.internalServerError().body(ex.getMessage()));
            });

It seems that the retry is never getting called on PrematureCloseException.

Jonathan Chevalier
  • 993
  • 1
  • 9
  • 18

1 Answers1

0

Resolved, it was not working because of rootCause

Retry.backoff(3, Duration.ofMillis(500))
        .filter(
            ex -> {
              if (ExceptionUtils.getRootCause(ex) instanceof PrematureCloseException) {
                log.info(
                    "HTTP_RETRY[{}][{}] PrematureClose detected retrying", functionName, company);
                return true;
              }
              return false;
            });
Jonathan Chevalier
  • 993
  • 1
  • 9
  • 18