Webclient重试和Onerrorsume互斥?

发布于 2025-02-10 19:56:56 字数 1979 浏览 0 评论 0原文

我正在尝试针对特定例外进行重试,但是我无法使用以下内容使其起作用:

    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()));
            });

似乎重试永远不会被提交过早的closeexception。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

解决了,由于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;
            });

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;
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文