1 回答
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);
}
}));
添加回答
举报