Java 指定时间内如果请求没有返回值就重试怎么实现(异步) - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
EddieWang
V2EX    程序员

Java 指定时间内如果请求没有返回值就重试怎么实现(异步)

  •  
  •   EddieWang 2023-03-29 19:13:31 +08:00 2243 次点击
    这是一个创建于 928 天前的主题,其中的信息可能已经有所发展或是发生改变。

    1.有个 spring retry 的重试,但是这个似乎没有办法实现超时之后重试 2.我们使用的是 org.springframework.web.reactive.function.client.WebClient ,我知道有个 retryWhen 的东西,但是由于封装了 sdk 所以我不好改它 所以大佬们有什么其它好的实现呢!!!!!

    10 条回复    2023-03-30 10:41:58 +08:00
    EddieWang
        1
    EddieWang  
    OP
       2023-03-29 19:15:56 +08:00
    也有使用 Mono.defer 似乎好像也不太行?
    dqzcwxb
        2
    dqzcwxb  
       2023-03-29 19:26:22 +08:00
    completablefuture timedGet()可以设置超时时间,超时后会抛出 TimeoutException 异常你捕获该异常进行重试就可以了
    kwh
        3
    kwh  
       2023-03-29 20:17:58 +08:00
    resilience4j ???
    hn16838220
        4
    hn16838220  
       2023-03-29 21:26:46 +08:00
    我记得 guava 的 retry 是可以写条件判断是否重试的
    why1001
        5
    why1001  
       2023-03-29 22:41:36 +08:00
    可以看看 project reactor 文档,webflux 也是在这上边实现的,里边有个 timeout()还有其它的可以实现。
    Helsing
        6
    Helsing  
       2023-03-29 22:45:33 +08:00 via iPhone
    用 reactor 、rxjava 这些框架比较好实现
    shinyruo2020
        7
    shinyruo2020  
       2023-03-29 22:51:56 +08:00
    for 循环 + countDownLatch.await 指定超时时间
    bthulu
        8
    bthulu  
       2023-03-30 10:32:31 +08:00
    You can retry requests using the Java 11 HTTP client by composing the returned future with re-schedules in a loop. Here’s an example:

    public static <T> CompletableFuture<T> retry(Supplier<CompletableFuture<T>> action, int maxRetries) {
    return action.get().handle((result, throwable) -> {
    if (throwable != null && maxRetries > 0) {
    return retry(action, maxRetries - 1);
    } else if (throwable != null) {
    throw new RuntimeException(throwable);
    } else {
    return result;
    }
    }).thenCompose(Function.identity());
    }
    This method takes a Supplier that returns a CompletableFuture, and an integer maxRetries that specifies the maximum number of retries. It returns a new CompletableFuture that retries the action if it fails with an exception.

    Here’s how you can use this method to retry an HTTP request:

    HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("http://example.com"))
    .build();

    CompletableFuture<HttpResponse<String>> respOnse= retry(() -> client.sendAsync(request, HttpResponse.BodyHandlers.ofString()), 3);
    This code retries the HTTP request up to three times if it fails with an exception.

    I hope this helps! Let me know if you have any other questions.
    yazinnnn
        9
    yazinnnn  
       2023-03-30 10:33:34 +08:00
    Mono.defer(() -> {
    System.out.println(LocalDateTime.now());
    val integer = Random.Default.nextInt(100);
    System.out.println(integer);
    if (integer > 80) {
    return Mono.just(100);
    } else {
    return Mono.never();
    }
    })
    .timeout(Duration.ofSeconds(1))
    .retry(10)

    defer 就行
    liuhuan475
        10
    liuhuan475  
       2023-03-30 10:41:58 +08:00
    楼上的回答都有同样的问题,就是应用挂掉重试就不会进行了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5557 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 218ms UTC 07:29 PVG 15:29 LAX 00:29 JFK 03:29
    Do have faith in wat you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86