V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
EddieWang
V2EX  ›  程序员

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

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

    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   ·   我们的愿景   ·   实用小工具   ·   2264 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:51 · PVG 08:51 · LAX 17:51 · JFK 20:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.