亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

出錯時再次調用并在 webflux 中重試

出錯時再次調用并在 webflux 中重試

夢里花落0921 2022-11-02 10:44:12
我想使用WebClientfrom spring webflux 執行以下操作:稱呼endpoint1如果它失敗并出現預期錯誤,那么打電話endpoint2和只重試endpoint1一次我已經做到了這一點:webclient.get()  .uri("/endpoint1")  .retrieve()  .bodyToFlux(MyBody.class)  .retry(error -> {     if (error == expectedError) {       webclient.get()         .uri("/endpoint2")         .retrieve().block();       return true;     } else {       false;     });請求時我無法阻止,endpoint2因為我會收到以下錯誤:(block()/blockFirst()/blockLast() are blocking, which is not supported in thread我也不想阻止)。也許我應該使用retryWhen,但我不確定如何使用它。
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

我完成這項工作的唯一方法是retryWhen我無法使用reactor.retry.Retry#doOnRetry,因為它只接受 aConsumer而不是 a Monoor Fluxor Publisher。


片段如下:


webclient.get()

  .uri("/endpoint1")

  .retrieve()

  .bodyToFlux(MyBody.class)

  .retryWhen(errorCurrentAttempt -> errorCurrentAttempt

                .flatMap(currentError -> Mono.subscriberContext().map(ctx -> Tuples.of(currentError, ctx)))

                .flatMap(tp -> {

                    Context ctx = tp.getT2();

                    Throwable error = tp.getT1();

                    int maxAttempts = 3;

                    Integer rl = ctx.getOrDefault("retriesLeft", maxAttempts);

                    if (rl != null && rl > 0 && error == myExpectedError) {

                        // Call endpoint and retry

                        return webclient.get()

                                .uri("/endpoint2")

                                .retrieve()

                                .thenReturn(ctx.put("retriesLeft", rl - 1));

                    } else {

                        // Finish retries

                        return Mono.<Object>error(error);

                    }

                }));


查看完整回答
反對 回復 2022-11-02
  • 1 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號